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_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__ from "../../../schema-templates/index.mjs";
const FormComponent = (props)=>{
    const { form, children, ...others } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const f = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAttach)(form.createVoidField({
        ...field.props,
        basePath: ''
    }));
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FieldContext.Provider, {
        value: void 0,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                layout: 'vertical',
                ...others,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                    basePath: f.address,
                    schema: fieldSchema,
                    onlyRenderProperties: true
                })
            })
        })
    });
};
const Def = (props)=>props.children;
const FormDecorator = (props)=>{
    const { form, children, disabled, ...others } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const f = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useAttach)(form.createVoidField({
        ...field.props,
        basePath: ''
    }));
    const Component = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useComponent)(fieldSchema['x-component'], Def);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        form.disabled = disabled || field.disabled;
    }, [
        disabled,
        field.disabled
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.ConfigProvider, {
        componentDisabled: disabled,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FieldContext.Provider, {
            value: void 0,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FormContext.Provider, {
                value: form,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                    layout: 'vertical',
                    ...others,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.FieldContext.Provider, {
                        value: f,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Component, {
                            ...field.componentProps,
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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 (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(getRequestParams(props), opts);
};
const FormBlockContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
FormBlockContext.displayName = 'FormBlockContext';
const Form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { request, effects, initialValue, useValues = useDefaultValues, ...others } = props;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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 = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(FormBlockContext);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FormBlockContext.Provider, {
        value: {
            parent,
            form,
            result,
            field,
            fieldSchema
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {
            spinning: (null == result ? void 0 : result.loading) || false,
            children: 'Form' === fieldSchema['x-decorator'] ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FormDecorator, {
                form: form,
                ...others
            }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FormComponent, {
                form: form,
                ...others
            })
        })
    });
}, {
    displayName: 'Form'
});
Form.Designer = function() {
    const { name, title } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const template = (0, __WEBPACK_EXTERNAL_MODULE__schema_templates_index_mjs__.useSchemaTemplate)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.GeneralSchemaDesigner, {
        template: template,
        title: title || name,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsTemplate, {
                componentName: 'Form',
                collectionName: name
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            })
        ]
    });
};
export { Form };
