import { jsx } from "react/jsx-runtime";
import { useContext, useMemo } from "react";
import { FormProvider, SchemaExpressionScopeContext, SchemaOptionsContext, createForm } from "@tachybase/schema";
import { SchemaComponentOptions } from "./SchemaComponentOptions.mjs";
const WithForm = (props)=>{
    const { children, form, ...others } = props;
    const options = useContext(SchemaOptionsContext);
    const expressionScope = useContext(SchemaExpressionScopeContext);
    const scope = {
        ...null == options ? void 0 : options.scope,
        ...expressionScope
    };
    const components = {
        ...null == options ? void 0 : options.components
    };
    return /*#__PURE__*/ jsx(FormProvider, {
        ...others,
        form: form,
        children: /*#__PURE__*/ jsx(SchemaComponentOptions, {
            components: components,
            scope: scope,
            children: children
        })
    });
};
const WithoutForm = (props)=>{
    const { children, ...others } = props;
    const options = useContext(SchemaOptionsContext);
    const expressionScope = useContext(SchemaExpressionScopeContext);
    const scope = {
        ...null == options ? void 0 : options.scope,
        ...expressionScope
    };
    const components = {
        ...null == options ? void 0 : options.components
    };
    const form = useMemo(()=>createForm(), []);
    return /*#__PURE__*/ jsx(FormProvider, {
        ...others,
        form: form,
        children: /*#__PURE__*/ jsx(SchemaComponentOptions, {
            components: components,
            scope: scope,
            children: children
        })
    });
};
const FormProvider_FormProvider = (props)=>props.form ? /*#__PURE__*/ jsx(WithForm, {
        ...props
    }) : /*#__PURE__*/ jsx(WithoutForm, {
        ...props
    });
export { FormProvider_FormProvider as FormProvider };
