const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_mergeWith = require("../../../../utils/mergeWith.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_VariablesContext = require("./VariablesContext.js");
const require_utils = require("../../dataSourceForm/utils.js");
const require_utils$1 = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let ahooks = require("ahooks");
//#region src/components/dataSourceComponents/provider/variables/VariablesProvider.tsx
const defaultCustomizer = (objValue, srcValue) => {
	if ((0, _pisell_utils.isArr)(srcValue)) return srcValue;
};
const VARIABLE_PATTERN = /{{([^}]+)}}/g;
const VariablesProvider = (props) => {
	var _context$engine, _context$appHelper;
	const { children, variables, config } = props;
	const context = require_useEngineContext.default();
	const disabledHistoryListener = config !== null && config !== void 0 ? config : context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 || (_context$engine = _context$engine.variablesConfig) === null || _context$engine === void 0 ? void 0 : _context$engine.disabledHistoryListener;
	const { getApp } = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const initialQuery = (0, react.useMemo)(() => {
		var _getApp;
		if (disabledHistoryListener) return {};
		const currentLocation = getApp === null || getApp === void 0 || (_getApp = getApp()) === null || _getApp === void 0 || (_getApp = _getApp.history) === null || _getApp === void 0 || (_getApp = _getApp.instance) === null || _getApp === void 0 ? void 0 : _getApp.location;
		if (currentLocation === null || currentLocation === void 0 ? void 0 : currentLocation.search) {
			const searchParams = new URLSearchParams(currentLocation.search);
			const query = {};
			searchParams.forEach((value, key) => {
				query[key] = value;
			});
			return query;
		}
		return {};
	}, [disabledHistoryListener]);
	const variablesRef = (0, react.useRef)(disabledHistoryListener ? variables || {} : require_mergeWith.mergeWith(variables, { global: {
		value: { query: initialQuery },
		query: initialQuery
	} }, defaultCustomizer));
	const listenersRef = (0, react.useRef)(/* @__PURE__ */ new Set());
	const setQueryVariable = (0, ahooks.useMemoizedFn)((search) => {
		if (disabledHistoryListener) return;
		const searchParams = new URLSearchParams(search);
		const query = {};
		searchParams.forEach((value, key) => {
			query[key] = value;
		});
		registerVariable({ global: query });
	});
	(0, react.useEffect)(() => {
		var _getApp2;
		if (disabledHistoryListener) return;
		const unlisten = getApp === null || getApp === void 0 || (_getApp2 = getApp()) === null || _getApp2 === void 0 || (_getApp2 = _getApp2.history) === null || _getApp2 === void 0 || (_getApp2 = _getApp2.instance) === null || _getApp2 === void 0 ? void 0 : _getApp2.listen((location) => {
			setQueryVariable(location.search);
		});
		return () => {
			unlisten === null || unlisten === void 0 || unlisten();
		};
	}, [disabledHistoryListener]);
	const registerVariable = (0, ahooks.useMemoizedFn)((value) => {
		variablesRef.current = require_mergeWith.mergeWith(variablesRef.current, value, defaultCustomizer);
		listenersRef.current.forEach((listener) => listener(variablesRef.current, value));
		console.log("注册变量", value, "全部变量", variablesRef.current);
	});
	const subscribeVariables = (0, ahooks.useMemoizedFn)((listener) => {
		listenersRef.current.add(listener);
		return () => {
			listenersRef.current.delete(listener);
		};
	});
	const registerValueVariable = (0, ahooks.useMemoizedFn)((componentId, value) => {
		const variable = require_utils$1.genComponentValueVariable(componentId, value);
		registerVariable === null || registerVariable === void 0 || registerVariable(variable);
	});
	const registerFunctionVariable = (0, ahooks.useMemoizedFn)((componentId, value) => {
		const variable = require_utils$1.genComponentFunctionVariable(componentId, value);
		registerVariable === null || registerVariable === void 0 || registerVariable(variable);
	});
	const parseVariable = (0, ahooks.useMemoizedFn)((str, localVariables = {}) => {
		var _str$match;
		if (!(0, _pisell_utils.isString)(str)) return str;
		if (((_str$match = str.match(VARIABLE_PATTERN)) === null || _str$match === void 0 ? void 0 : _str$match[0]) === str) {
			const key = require_utils$1.getVariableKey(str);
			if (!key) return str;
			const localValue = require_utils.getNestedValue(localVariables, key);
			if (!(0, _pisell_utils.isUndefined)(localValue)) return localValue;
			const globalValue = require_utils.getNestedValue(variablesRef.current, key);
			return (0, _pisell_utils.isUndefined)(globalValue) ? str : globalValue;
		}
		return str.replace(VARIABLE_PATTERN, (match, key) => {
			const localValue = require_utils.getNestedValue(localVariables, key);
			if (!(0, _pisell_utils.isUndefined)(localValue)) return localValue;
			const globalValue = require_utils.getNestedValue(variablesRef.current, key);
			return (0, _pisell_utils.isUndefined)(globalValue) ? match : globalValue;
		});
	});
	const value = (0, react.useMemo)(() => ({
		variables: variablesRef.current,
		registerVariable,
		parseVariable,
		registerValueVariable,
		registerFunctionVariable,
		subscribeVariables
	}), [
		registerVariable,
		parseVariable,
		registerValueVariable,
		registerFunctionVariable,
		subscribeVariables
	]);
	return /* @__PURE__ */ react.default.createElement(require_VariablesContext.default.Provider, { value }, children);
};
//#endregion
exports.default = VariablesProvider;
