require("../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let react = require("react");
//#region src/hooks/useLowCode.ts
const useLowCode = (props) => {
	const { onChange, onReady } = props;
	const lowcodeRef = (0, react.useRef)(null);
	const cacheState = (0, react.useRef)(null);
	const cacheFunc = (0, react.useRef)(null);
	/**
	* @title: lowcode 页面初始化
	* @description:
	* @param {any} e
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2023-12-06 09:40
	*/
	const _onReady = (e) => {
		try {
			lowcodeRef.current = e;
			onReady && onReady(e);
			if (cacheState.current) {
				_setState(cacheState.current.values, cacheState.current.callback);
				cacheState.current = null;
			}
			if (cacheFunc.current) {
				cacheFunc.current();
				cacheFunc.current = null;
			}
		} catch (err) {
			console.error(err);
		}
	};
	const _onChange = (e) => {
		onChange && onChange(e);
	};
	const _setState = (values, callback) => {
		try {
			if (lowcodeRef.current) {
				lowcodeRef.current.setState(values, callback);
				return;
			}
			if (cacheState.current) cacheState.current = {
				values: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cacheState.current.values), values),
				callback
			};
			else cacheState.current = {
				values,
				callback
			};
		} catch (err) {
			console.error(err);
		}
	};
	const pageFunc = (functionName, functionParams) => {
		if (lowcodeRef.current) return lowcodeRef.current[functionName](functionParams);
		else cacheFunc.current = () => lowcodeRef.current[functionName](functionParams);
	};
	return {
		lowcodeRef: {
			onReady: _onReady,
			onChange: _onChange
		},
		setState: _setState,
		pageFunc
	};
};
//#endregion
exports.default = useLowCode;
