"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__, {
    useFormFieldProps: ()=>useFormFieldProps,
    useFormFieldContext: ()=>useFormFieldContext,
    FormFieldProvider: ()=>FormFieldProvider,
    FormFieldContext: ()=>FormFieldContext,
    WithoutFormFieldResource: ()=>WithoutFormFieldResource
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const external_antd_namespaceObject = require("antd");
const CollectionRecordProvider_js_namespaceObject = require("../data-source/collection-record/CollectionRecordProvider.js");
const index_js_namespaceObject = require("../record-provider/index.js");
const external_BlockProvider_js_namespaceObject = require("./BlockProvider.js");
const external_FormBlockProvider_js_namespaceObject = require("./FormBlockProvider.js");
const FormFieldContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)({});
FormFieldContext.displayName = 'FormFieldContext';
const InternalFormFieldProvider = (props)=>{
    var _formBlockCtx_updateAssociationValues, _service_data;
    const { action, readPretty, fieldName } = props;
    const formBlockCtx = (0, external_FormBlockProvider_js_namespaceObject.useFormBlockContext)();
    const parentRecordData = (0, CollectionRecordProvider_js_namespaceObject.useCollectionParentRecordData)();
    if (!(null == formBlockCtx ? void 0 : null === (_formBlockCtx_updateAssociationValues = formBlockCtx.updateAssociationValues) || void 0 === _formBlockCtx_updateAssociationValues ? void 0 : _formBlockCtx_updateAssociationValues.includes(fieldName))) {
        var _formBlockCtx_updateAssociationValues1;
        null == formBlockCtx || null === (_formBlockCtx_updateAssociationValues1 = formBlockCtx.updateAssociationValues) || void 0 === _formBlockCtx_updateAssociationValues1 || _formBlockCtx_updateAssociationValues1.push(fieldName);
    }
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.createForm)({
            effects () {
                (0, schema_namespaceObject.onFormValuesChange)((form)=>{
                    var _formBlockCtx_form;
                    null == formBlockCtx || null === (_formBlockCtx_form = formBlockCtx.form) || void 0 === _formBlockCtx_form || _formBlockCtx_form.setValuesIn(fieldName, form.values);
                });
            },
            readPretty
        }), []);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const dispose = (0, schema_namespaceObject.autorun)(()=>{
            var _formBlockCtx_form;
            const data = (null == formBlockCtx ? void 0 : null === (_formBlockCtx_form = formBlockCtx.form) || void 0 === _formBlockCtx_form ? void 0 : _formBlockCtx_form.values[fieldName]) || {};
            form.reset();
            (0, client_namespaceObject.forEach)(data, (value, key)=>{
                if (value) form.values[key] = value;
            });
        });
        return dispose;
    }, []);
    const { resource, service } = (0, external_BlockProvider_js_namespaceObject.useBlockRequestContext)();
    if (service.loading) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {});
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.RecordProvider, {
        record: null == service ? void 0 : null === (_service_data = service.data) || void 0 === _service_data ? void 0 : _service_data.data,
        parent: parentRecordData,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FormFieldContext.Provider, {
            value: {
                action,
                form,
                field,
                service,
                resource,
                fieldName
            },
            children: props.children
        })
    });
};
const WithoutFormFieldResource = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
WithoutFormFieldResource.displayName = 'WithoutFormFieldResource';
const FormFieldProvider = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(WithoutFormFieldResource.Provider, {
        value: false,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.BlockProvider, {
            name: "form-field",
            block: 'FormField',
            ...props,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalFormFieldProvider, {
                ...props
            })
        })
    });
const useFormFieldContext = ()=>(0, external_react_namespaceObject.useContext)(FormFieldContext);
const useFormFieldProps = ()=>{
    const ctx = useFormFieldContext();
    (0, external_react_namespaceObject.useEffect)(()=>{
        var _ctx_service_data, _ctx_service, _ctx_form;
        null == ctx || null === (_ctx_form = ctx.form) || void 0 === _ctx_form || _ctx_form.setInitialValues(null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : null === (_ctx_service_data = _ctx_service.data) || void 0 === _ctx_service_data ? void 0 : _ctx_service_data.data);
    }, []);
    return {
        form: ctx.form
    };
};
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
});
