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_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_v2_Templates_mjs__ from "../schema-component/antd/form-v2/Templates.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__ from "./BlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useFormActiveFields_mjs__ from "./hooks/useFormActiveFields.mjs";
const FormBlockContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({});
FormBlockContext.displayName = 'FormBlockContext';
const InternalFormBlockProvider = (props)=>{
    var _Object_keys, _record_parentRecord;
    const cm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionManager)();
    const ctx = useFormBlockContext();
    const { action, readPretty, params, collection, association } = props;
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)({
            readPretty
        }), [
        readPretty
    ]);
    const { resource, service, updateAssociationValues } = (0, __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.useBlockRequestContext)();
    const formBlockRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionRecord)();
    const formBlockValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {});
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FormBlockContext.Provider, {
        value: formBlockValue,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                ref: formBlockRef,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RenderChildrenWithDataTemplates, {
                    form: form
                })
            })
        })
    });
};
const useFormBlockType = ()=>{
    const ctx = useFormBlockContext() || {};
    const res = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            type: ctx.type
        }), [
        ctx.type
    ]);
    return res;
};
const useIsDetailBlock = ()=>{
    const ctx = useFormBlockContext();
    const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((props)=>{
    const parentRecordData = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionParentRecordData)();
    const { parentRecord } = props;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.BlockProvider, {
        name: props.name || 'form',
        ...props,
        block: 'form',
        parentRecord: parentRecord || parentRecordData,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__hooks_useFormActiveFields_mjs__.FormActiveFieldsProvider, {
            name: "form",
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalFormBlockProvider, {
                ...props
            })
        })
    });
});
const useFormBlockContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { fieldSchema } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useActionContext)();
    const addChild = null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.addChild;
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { findComponent } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useDesignable)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const Component = findComponent(null === (_field_component = field.component) || void 0 === _field_component ? void 0 : _field_component[0]) || __WEBPACK_EXTERNAL_MODULE_react__["default"].Fragment;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(Component, {
        ...field.componentProps,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_v2_Templates_mjs__.Templates, {
                style: {
                    marginBottom: 18
                },
                form: form
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                schema: FieldSchema,
                onlyRenderProperties: true
            })
        ]
    });
};
const findFormBlock = (schema)=>{
    while(schema){
        if ('FormBlockProvider' === schema['x-decorator']) return schema;
        schema = schema.parent;
    }
    return null;
};
export { FormBlockContext, FormBlockProvider, findFormBlock, useFormBlockContext, useFormBlockProps, useFormBlockType, useIsDetailBlock };
