"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    FormBlockProvider: ()=>FormBlockProvider,
    useIsDetailBlock: ()=>useIsDetailBlock,
    findFormBlock: ()=>findFormBlock,
    useFormBlockContext: ()=>useFormBlockContext,
    useFormBlockProps: ()=>useFormBlockProps,
    FormBlockContext: ()=>FormBlockContext,
    useFormBlockType: ()=>useFormBlockType
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const withDynamicSchemaProps_js_namespaceObject = require("../application/hoc/withDynamicSchemaProps.js");
const index_js_namespaceObject = require("../data-source/index.js");
const external_record_provider_index_js_namespaceObject = require("../record-provider/index.js");
const external_schema_component_index_js_namespaceObject = require("../schema-component/index.js");
const Templates_js_namespaceObject = require("../schema-component/antd/form-v2/Templates.js");
const external_BlockProvider_js_namespaceObject = require("./BlockProvider.js");
const useFormActiveFields_js_namespaceObject = require("./hooks/useFormActiveFields.js");
const FormBlockContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)({});
FormBlockContext.displayName = 'FormBlockContext';
const InternalFormBlockProvider = (props)=>{
    var _Object_keys, _record_parentRecord;
    const cm = (0, index_js_namespaceObject.useCollectionManager)();
    const ctx = useFormBlockContext();
    const { action, readPretty, params, collection, association } = props;
    const field = (0, schema_namespaceObject.useField)();
    const form = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.createForm)({
            readPretty
        }), [
        readPretty
    ]);
    const { resource, service, updateAssociationValues } = (0, external_BlockProvider_js_namespaceObject.useBlockRequestContext)();
    const formBlockRef = (0, external_react_namespaceObject.useRef)();
    const record = (0, index_js_namespaceObject.useCollectionRecord)();
    const formBlockValue = (0, external_react_namespaceObject.useMemo)(()=>{
        var _cm_getCollectionField;
        return {
            ...ctx,
            params,
            action,
            form,
            type: 'get' === action ? 'update' : 'create',
            field,
            service,
            resource,
            updateAssociationValues,
            formBlockRef,
            collectionName: collection || (null === (_cm_getCollectionField = cm.getCollectionField(association)) || void 0 === _cm_getCollectionField ? void 0 : _cm_getCollectionField.target),
            formRecord: record
        };
    }, [
        action,
        association,
        cm,
        collection,
        ctx,
        field,
        form,
        params,
        record,
        resource,
        service,
        updateAssociationValues
    ]);
    if (service.loading && (null === (_Object_keys = Object.keys(null == form ? void 0 : form.initialValues)) || void 0 === _Object_keys ? void 0 : _Object_keys.length) === 0 && action) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {});
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FormBlockContext.Provider, {
        value: formBlockValue,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
            isNew: null == record ? void 0 : record.isNew,
            parent: null == record ? void 0 : null === (_record_parentRecord = record.parentRecord) || void 0 === _record_parentRecord ? void 0 : _record_parentRecord.data,
            record: null == record ? void 0 : record.data,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                ref: formBlockRef,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(RenderChildrenWithDataTemplates, {
                    form: form
                })
            })
        })
    });
};
const useFormBlockType = ()=>{
    const ctx = useFormBlockContext() || {};
    const res = (0, external_react_namespaceObject.useMemo)(()=>({
            type: ctx.type
        }), [
        ctx.type
    ]);
    return res;
};
const useIsDetailBlock = ()=>{
    const ctx = useFormBlockContext();
    const { fieldSchema } = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    return 'create' !== ctx.type && (null == fieldSchema ? void 0 : fieldSchema['x-acl-action']) !== 'create' && (null == fieldSchema ? void 0 : fieldSchema['x-action']) !== 'create';
};
const FormBlockProvider = (0, withDynamicSchemaProps_js_namespaceObject.withDynamicSchemaProps)((props)=>{
    const parentRecordData = (0, index_js_namespaceObject.useCollectionParentRecordData)();
    const { parentRecord } = props;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.BlockProvider, {
        name: props.name || 'form',
        ...props,
        block: 'form',
        parentRecord: parentRecord || parentRecordData,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(useFormActiveFields_js_namespaceObject.FormActiveFieldsProvider, {
            name: "form",
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalFormBlockProvider, {
                ...props
            })
        })
    });
});
const useFormBlockContext = ()=>(0, external_react_namespaceObject.useContext)(FormBlockContext);
const useFormBlockProps = function() {
    arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
    var _fieldSchema_xcomponentprops, _ctx_service;
    const ctx = useFormBlockContext();
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const { fieldSchema } = (0, external_schema_component_index_js_namespaceObject.useActionContext)();
    const addChild = null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.addChild;
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (addChild) {
            var _ctx_form;
            null === (_ctx_form = ctx.form) || void 0 === _ctx_form || _ctx_form.query('parent').take((field)=>{
                field.disabled = true;
                field.value = new Proxy({
                    ...null == record ? void 0 : record.__parent
                }, {});
            });
        }
    });
    (0, external_react_namespaceObject.useEffect)(()=>{
        var _ctx_service;
        if (!(null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : _ctx_service.loading)) {
            var _ctx_service_data, _ctx_service1, _ctx_form;
            null === (_ctx_form = ctx.form) || void 0 === _ctx_form || _ctx_form.setInitialValues(null === (_ctx_service1 = ctx.service) || void 0 === _ctx_service1 ? void 0 : null === (_ctx_service_data = _ctx_service1.data) || void 0 === _ctx_service_data ? void 0 : _ctx_service_data.data);
        }
    }, [
        null == ctx ? void 0 : null === (_ctx_service = ctx.service) || void 0 === _ctx_service ? void 0 : _ctx_service.loading
    ]);
    return {
        form: ctx.form
    };
};
const RenderChildrenWithDataTemplates = (param)=>{
    let { form } = param;
    var _field_component;
    const FieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { findComponent } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
    const field = (0, schema_namespaceObject.useField)();
    const Component = findComponent(null === (_field_component = field.component) || void 0 === _field_component ? void 0 : _field_component[0]) || external_react_default().Fragment;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(Component, {
        ...field.componentProps,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Templates_js_namespaceObject.Templates, {
                style: {
                    marginBottom: 18
                },
                form: form
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                schema: FieldSchema,
                onlyRenderProperties: true
            })
        ]
    });
};
const findFormBlock = (schema)=>{
    while(schema){
        if ('FormBlockProvider' === schema['x-decorator']) return schema;
        schema = schema.parent;
    }
    return null;
};
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
});
