Options = (spec, sourceOptions, opts) => {
	return new Proxy(sourceOptions, {
		/** @returns {(string|symbol)[]} */
		ownKeys() {
			const output = [];
			for (const [specKey, specEntry] of Object.entries(spec)) {
				if (specEntry.virtual) continue;
				const valueKey = specEntry.children && specEntry.nest ? `_${specKey}` : specKey;
				if (
					!opts?.specMode /* Value Mode */ &&
					!(valueKey in sourceOptions) /* Value is not defined */
				)
					continue;
				output.push(specKey);
			}

			return output.sort((a, b) => a.localeCompare(b));
		},
		get(self, p) {
			// Mode Toggles
			if (p === IsOptions) return true;
			if (p === OptionSpecMode)
				return Options(spec, sourceOptions, { ...opts, specMode: !opts?.specMode });
			if (p === OptionSecretMode)
				return Options(spec, sourceOptions, {
					...opts,
					secretsMode: !opts?.secretsMode,
					optionsMode: false
				});
			if (p === OptionSafeMode)
				return Options(spec, sourceOptions, {
					...opts,
					optionsMode: !opts?.optionsMode,
					secretsMode: false
				});
			if (p === OptionParentMode)
				return Options(spec, sourceOptions, { ...opts, parentMode: !opts?.parentMode });
			if (p === OptionGetSpec) return spec;
			// Special Cases
			if (p === OptionDebug) return { spec, sourceOptions, opts };
			if (p === 'toJSON') return () => sourceOptions;
			if (p === 'toString' || p === Symbol.toStringTag) {
				return () => opts?.value;
			}

			const prop = p.toString();

			const fieldSpec = spec[prop];

			if (!fieldSpec) {
				throw new EvidenceError('Internal Error', [
					'Attempted to get property that is not in spec',
					JSON.stringify({ prop }),
					JSON.stringify({ spec })
				]);
			}

			const { specMode, parentMode, secretsMode, optionsMode } = opts ?? {};
			const hasChildren = Boolean(fieldSpec.children);
			const secret = Boolean(fieldSpec.secret);
			const allMode = Boolean(!secretsMode && !optionsMode);
			const validSecret = Boolean(secret && secretsMode);
			const validOption = Boolean(!secret && optionsMode);

			let returnValue;
			let returnSpec;

			if (hasChildren) {
				const valueKey = fieldSpec.nest ? `_${prop}` : prop;

				if (parentMode) {
					// Never resolveChildOptions
					if (allMode || validSecret || validOption) returnValue = self[valueKey];
					returnSpec = fieldSpec;
				} else {
					// We are in child mode, we resolveChildOptions if needed
					returnValue = resolveChildOptions(fieldSpec, prop, sourceOptions, {
						...opts,
						parent: self
					});
					returnSpec = fieldSpec?.children?.[self[valueKey]];
				}
			} else {
				returnSpec = fieldSpec;

				if (allMode || validSecret || validOption) returnValue = self[prop];
				else returnValue = undefined;
			}

			if (specMode) return returnSpec;
			else return returnValue;
		},
		set(self, p, setVal) {
			if (opts?.specMode) return false;
			const prop = p.toString();

			const fieldSpec = spec[prop];
			let target = prop;
			if (fieldSpec.children) {
				if (fieldSpec.nest) {
					if (typeof setVal !== 'object') target = `_${prop}`;
				}
			}
			if (typeof setVal === 'object') {
				const child = resolveChildOptions(spec[prop], prop, self, { ...opts, parent: self });
				for (const [k, v] of Object.entries(setVal)) {
					// This checks against spec using .get
					child[k];

					child[k] = v;
				}
			}

			self[target] = setVal;
			return true;
		}
	});
}
@param {import('../../Datasources.js').Datasource["options"]} spec
@param {*} sourceOptions
@param {OptionsOpts} [opts]
@returns {OptionsObj}