import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "../context.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SchemaComponentOptions_mjs__ from "./SchemaComponentOptions.mjs";
const randomString = function() {
    let prefix = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : '';
    return "".concat(prefix).concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
};
__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.silent(true);
const results = {};
const Registry = {
    silent: true,
    compile (expression) {
        let scope = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
        const fn = ()=>{
            if (!Registry.silent) return new Function('$root', "with($root) { return (".concat(expression, "); }"))(scope);
            try {
                return new Function('$root', "with($root) { return (".concat(expression, "); }"))(scope);
            } catch  {
                return "{{".concat(expression, "}}");
            }
        };
        if (results[expression]) return results[expression];
        if (expression.trim().startsWith('t(')) {
            results[expression] = fn();
            return results[expression];
        }
        return fn();
    }
};
__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.registerCompiler(Registry.compile);
const SchemaComponentProvider = (props)=>{
    const { designable, onDesignableChange, components, children } = props;
    const [uidValue, setUid] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
    const [formId, setFormId] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>props.form || (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)(), [
        formId
    ]);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const scope = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            ...props.scope,
            t,
            randomString
        }), [
        props.scope,
        t
    ]);
    const [active, setActive] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(designable);
    const schemaComponentContextValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            scope,
            components,
            reset: ()=>setFormId((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)()),
            refresh: ()=>{
                setUid((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
            },
            designable: 'boolean' == typeof designable ? designable : active,
            setDesignable: (value)=>{
                if ('boolean' != typeof designable) setActive(value);
                null == onDesignableChange || onDesignableChange(value);
            }
        }), [
        uidValue,
        scope,
        components,
        designable,
        active
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.SchemaComponentContext.Provider, {
        value: schemaComponentContextValue,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormProvider, {
            form: form,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SchemaComponentOptions_mjs__.SchemaComponentOptions, {
                inherit: true,
                scope: scope,
                components: components,
                children: children
            })
        })
    });
};
SchemaComponentProvider.displayName = 'SchemaComponentProvider';
export { SchemaComponentProvider };
