/**
 * get current context ref
 *
 * @param target
 * @param  {{refs:{}, enableColorScheme:Boolean}} [options]
 * @returns {Object.<*, function|string|number|Object.<*, *>>}
 */
export default function ctxRef(target, options) {
	if (options !== null && typeof options === "object") {
		// refs
		if (typeof options.refs === "object") {
			target.$refs = options.refs
		}

		// color-scheme
		if (options.enableColorScheme) {
			target.$colorScheme = localStorage.getItem("color-scheme")
			if (typeof target.$colorScheme === "string" && target.$colorScheme.length > 0) {
				document.querySelector("html").className = target.$colorScheme
			}
		}
	}
	const targetRef = target.$ref

	for (const k in target) {
		const f = target[k]
		if (typeof f !== "function") {
			continue
		}

		target[k] = function (...allArgs) {
			return f.call(targetRef, ...allArgs)
		}
	}
	return targetRef
}