"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    Form: ()=>Form
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../../api-client/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_schema_settings_index_js_namespaceObject = require("../../../schema-settings/index.js");
const external_schema_templates_index_js_namespaceObject = require("../../../schema-templates/index.js");
const FormComponent = (props)=>{
    const { form, children, ...others } = props;
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const f = (0, external_index_js_namespaceObject.useAttach)(form.createVoidField({
        ...field.props,
        basePath: ''
    }));
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FieldContext.Provider, {
        value: void 0,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.FormLayout, {
                layout: 'vertical',
                ...others,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.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, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const f = (0, external_index_js_namespaceObject.useAttach)(form.createVoidField({
        ...field.props,
        basePath: ''
    }));
    const Component = (0, external_index_js_namespaceObject.useComponent)(fieldSchema['x-component'], Def);
    (0, external_react_namespaceObject.useEffect)(()=>{
        form.disabled = disabled || field.disabled;
    }, [
        disabled,
        field.disabled
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.ConfigProvider, {
        componentDisabled: disabled,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FieldContext.Provider, {
            value: void 0,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FormContext.Provider, {
                value: form,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.FormLayout, {
                    layout: 'vertical',
                    ...others,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.FieldContext.Provider, {
                        value: f,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Component, {
                            ...field.componentProps,
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.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, index_js_namespaceObject.useRequest)(getRequestParams(props), opts);
};
const FormBlockContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
FormBlockContext.displayName = 'FormBlockContext';
const Form = (0, schema_namespaceObject.observer)((props)=>{
    const { request, effects, initialValue, useValues = useDefaultValues, ...others } = props;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.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, external_react_namespaceObject.useContext)(FormBlockContext);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FormBlockContext.Provider, {
        value: {
            parent,
            form,
            result,
            field,
            fieldSchema
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {
            spinning: (null == result ? void 0 : result.loading) || false,
            children: 'Form' === fieldSchema['x-decorator'] ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FormDecorator, {
                form: form,
                ...others
            }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FormComponent, {
                form: form,
                ...others
            })
        })
    });
}, {
    displayName: 'Form'
});
Form.Designer = function() {
    const { name, title } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const template = (0, external_schema_templates_index_js_namespaceObject.useSchemaTemplate)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_schema_settings_index_js_namespaceObject.GeneralSchemaDesigner, {
        template: template,
        title: title || name,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsTemplate, {
                componentName: 'Form',
                collectionName: name
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsDivider, {}),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_settings_index_js_namespaceObject.SchemaSettingsRemove, {
                removeParentsIfNoChildren: true,
                breakRemoveOn: {
                    'x-component': 'Grid'
                }
            })
        ]
    });
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
