import { jsx } from "react/jsx-runtime";
import { useMemo, useState } from "react";
import { FormProvider, Schema, createForm, uid } from "@tachybase/schema";
import { useTranslation } from "react-i18next";
import { SchemaComponentContext } from "../context.mjs";
import { SchemaComponentOptions } from "./SchemaComponentOptions.mjs";
const randomString = function() {
    let prefix = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : '';
    return "".concat(prefix).concat(uid());
};
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();
    }
};
Schema.registerCompiler(Registry.compile);
const SchemaComponentProvider = (props)=>{
    const { designable, onDesignableChange, components, children } = props;
    const [uidValue, setUid] = useState(uid());
    const [formId, setFormId] = useState(uid());
    const form = useMemo(()=>props.form || createForm(), [
        formId
    ]);
    const { t } = useTranslation();
    const scope = useMemo(()=>({
            ...props.scope,
            t,
            randomString
        }), [
        props.scope,
        t
    ]);
    const [active, setActive] = useState(designable);
    const schemaComponentContextValue = useMemo(()=>({
            scope,
            components,
            reset: ()=>setFormId(uid()),
            refresh: ()=>{
                setUid(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__*/ jsx(SchemaComponentContext.Provider, {
        value: schemaComponentContextValue,
        children: /*#__PURE__*/ jsx(FormProvider, {
            form: form,
            children: /*#__PURE__*/ jsx(SchemaComponentOptions, {
                inherit: true,
                scope: scope,
                components: components,
                children: children
            })
        })
    });
};
SchemaComponentProvider.displayName = 'SchemaComponentProvider';
export { SchemaComponentProvider };
