import { jsx, jsxs } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo } from "react";
import { FormLayout } from "@tachybase/components";
import { FieldContext, FormContext, RecursionField, createForm, observer, useField, useFieldSchema } from "@tachybase/schema";
import { ConfigProvider, Spin } from "antd";
import { useAttach, useComponent } from "../../index.mjs";
import { useRequest } from "../../../api-client/index.mjs";
import { useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { GeneralSchemaDesigner, SchemaSettingsDivider, SchemaSettingsRemove, SchemaSettingsTemplate } from "../../../schema-settings/index.mjs";
import { useSchemaTemplate } from "../../../schema-templates/index.mjs";
const FormComponent = (props)=>{
    const { form, children, ...others } = props;
    const field = useField();
    const fieldSchema = useFieldSchema();
    const f = useAttach(form.createVoidField({
        ...field.props,
        basePath: ''
    }));
    return /*#__PURE__*/ jsx(FieldContext.Provider, {
        value: void 0,
        children: /*#__PURE__*/ jsx(FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ jsx(FormLayout, {
                layout: 'vertical',
                ...others,
                children: /*#__PURE__*/ jsx(RecursionField, {
                    basePath: f.address,
                    schema: fieldSchema,
                    onlyRenderProperties: true
                })
            })
        })
    });
};
const Def = (props)=>props.children;
const FormDecorator = (props)=>{
    const { form, children, disabled, ...others } = props;
    const field = useField();
    const fieldSchema = useFieldSchema();
    const f = useAttach(form.createVoidField({
        ...field.props,
        basePath: ''
    }));
    const Component = useComponent(fieldSchema['x-component'], Def);
    useEffect(()=>{
        form.disabled = disabled || field.disabled;
    }, [
        disabled,
        field.disabled
    ]);
    return /*#__PURE__*/ jsx(ConfigProvider, {
        componentDisabled: disabled,
        children: /*#__PURE__*/ jsx(FieldContext.Provider, {
            value: void 0,
            children: /*#__PURE__*/ jsx(FormContext.Provider, {
                value: form,
                children: /*#__PURE__*/ jsx(FormLayout, {
                    layout: 'vertical',
                    ...others,
                    children: /*#__PURE__*/ jsx(FieldContext.Provider, {
                        value: f,
                        children: /*#__PURE__*/ jsx(Component, {
                            ...field.componentProps,
                            children: /*#__PURE__*/ jsx(RecursionField, {
                                basePath: f.address,
                                schema: fieldSchema,
                                onlyRenderProperties: true
                            })
                        })
                    })
                })
            })
        })
    });
};
const getRequestParams = (props)=>{
    const { request, initialValue } = props;
    if (request) return request;
    return ()=>Promise.resolve({
            data: initialValue
        });
};
const useDefaultValues = function() {
    let opts = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, props = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
    return useRequest(getRequestParams(props), opts);
};
const FormBlockContext = /*#__PURE__*/ createContext(null);
FormBlockContext.displayName = 'FormBlockContext';
const Form = observer((props)=>{
    const { request, effects, initialValue, useValues = useDefaultValues, ...others } = props;
    const fieldSchema = useFieldSchema();
    const field = useField();
    const form = useMemo(()=>createForm({
            effects
        }), []);
    const result = useValues({
        uid: fieldSchema['x-uid'],
        async onSuccess (data) {
            await form.reset();
            form.setValues(null == data ? void 0 : data.data);
            form.setInitialValues(null == data ? void 0 : data.data);
        }
    }, props);
    const parent = useContext(FormBlockContext);
    return /*#__PURE__*/ jsx(FormBlockContext.Provider, {
        value: {
            parent,
            form,
            result,
            field,
            fieldSchema
        },
        children: /*#__PURE__*/ jsx(Spin, {
            spinning: (null == result ? void 0 : result.loading) || false,
            children: 'Form' === fieldSchema['x-decorator'] ? /*#__PURE__*/ jsx(FormDecorator, {
                form: form,
                ...others
            }) : /*#__PURE__*/ jsx(FormComponent, {
                form: form,
                ...others
            })
        })
    });
}, {
    displayName: 'Form'
});
Form.Designer = function() {
    const { name, title } = useCollection_deprecated();
    const template = useSchemaTemplate();
    return /*#__PURE__*/ jsxs(GeneralSchemaDesigner, {
        template: template,
        title: title || name,
        children: [
            /*#__PURE__*/ jsx(SchemaSettingsTemplate, {
                componentName: 'Form',
                collectionName: name
            }),
            /*#__PURE__*/ jsx(SchemaSettingsDivider, {}),
            /*#__PURE__*/ jsx(SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            })
        ]
    });
};
export { Form };
