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__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__ from "../../../block-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../../data-source/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__ from "../../../data-source/collection-record/CollectionRecordProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__ from "../../../data-source/collection-record/isNewRecord.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__ from "../../../flag-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__ from "../../../schema-settings/hooks/useIsAllowToSetDefaultValue.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "./context.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_mjs__ from "./hooks.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      position: relative;\n      .ant-input {\n        width: 100%;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      > .ant-card-body > .ant-divider:last-child {\n        display: none;\n      }\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
function _templateObject2() {
    const data = _tagged_template_literal([
        "\n      border: 1px solid #f0f0f0 !important;\n      box-shadow: none;\n    "
    ]);
    _templateObject2 = function() {
        return data;
    };
    return data;
}
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        input: css(_templateObject()),
        card: css(_templateObject1()),
        button: css(_templateObject2())
    };
});
const Nester = (props)=>{
    const { options } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.AssociationFieldContext);
    if ([
        'hasOne',
        'belongsTo'
    ].includes(options.type)) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.FlagProvider, {
        isInSubForm: true,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ToOneNester, {
            ...props
        })
    });
    if ([
        'hasMany',
        'belongsToMany'
    ].includes(options.type)) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.FlagProvider, {
        isInSubForm: true,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ToManyNester, {
            ...props
        })
    });
    return null;
};
const ToOneNester = (props)=>{
    const { field } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useAssociationFieldContext)();
    const recordV2 = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__.useCollectionRecord)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollection)();
    const isAllowToSetDefaultValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
        let { form, fieldSchema, collectionField, getInterface, formBlockType } = param;
        var _fieldSchema_xcomponentprops;
        if (!collectionField) return false;
        if (collectionField.target && (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.mode) && ![
            'Picker',
            'Select'
        ].includes(fieldSchema['x-component-props'].mode)) return false;
        if ('update' === formBlockType) return false;
        return !(null == form ? void 0 : form.readPretty) && !(0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.isPatternDisabled)(fieldSchema) && !__WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__.interfacesOfUnsupportedDefaultValue.includes(null == collectionField ? void 0 : collectionField.interface) && !(0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.isSystemField)(collectionField, getInterface);
    }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.FormActiveFieldsProvider, {
        name: "nester",
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__hooks_mjs__.SubFormProvider, {
            value: {
                value: field.value,
                collection
            },
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
                isNew: null == recordV2 ? void 0 : recordV2.isNew,
                record: field.value,
                parent: null == recordV2 ? void 0 : recordV2.data,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__.DefaultValueProvider, {
                    isAllowToSetDefaultValue: isAllowToSetDefaultValue,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Card, {
                        bordered: true,
                        children: props.children
                    })
                })
            })
        })
    });
};
const ToManyNester = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { options, field, allowMultiple, allowDissociate } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useAssociationFieldContext)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const recordData = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__.useCollectionRecordData)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollection)();
    if (!Array.isArray(field.value)) field.value = [];
    const isAllowToSetDefaultValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((param)=>{
        let { form, fieldSchema, collectionField, getInterface } = param;
        var _fieldSchema_xcomponentprops;
        if (!collectionField) return false;
        if (collectionField.target && (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.mode) && ![
            'Picker',
            'Select'
        ].includes(fieldSchema['x-component-props'].mode)) return false;
        return !(null == form ? void 0 : form.readPretty) && !(0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.isPatternDisabled)(fieldSchema) && !__WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__.interfacesOfUnsupportedDefaultValue.includes(null == collectionField ? void 0 : collectionField.interface) && !(0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.isSystemField)(collectionField, getInterface);
    }, []);
    const { styles } = useStyles();
    return field.value.length > 0 ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Card, {
        bordered: true,
        style: {
            position: 'relative'
        },
        className: styles.card,
        children: field.value.map((value, index)=>{
            let allowed = allowDissociate;
            if (!allowDissociate) allowed = !(null == value ? void 0 : value[options.targetKey]);
            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react__["default"].Fragment, {
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                        style: {
                            textAlign: 'right'
                        },
                        children: [
                            field.editable && allowMultiple && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tooltip, {
                                title: t('Add new'),
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {
                                    style: {
                                        zIndex: 1000,
                                        marginRight: '10px',
                                        color: '#a8a3a3'
                                    },
                                    onClick: ()=>{
                                        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.action)(()=>{
                                            if (!Array.isArray(field.value)) field.value = [];
                                            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.spliceArrayState)(field, {
                                                startIndex: index + 1,
                                                insertCount: 1
                                            });
                                            field.value.splice(index + 1, 0, (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__.markRecordAsNew)({}));
                                            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.each)(field.form.fields, (targetField, key)=>{
                                                if (!targetField) delete field.form.fields[key];
                                            });
                                            return field.onInput(field.value);
                                        });
                                    }
                                })
                            }, 'add'),
                            !field.readPretty && allowed && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tooltip, {
                                title: t('Remove'),
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.CloseOutlined, {
                                    style: {
                                        zIndex: 1000,
                                        color: '#a8a3a3'
                                    },
                                    onClick: ()=>{
                                        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.action)(()=>{
                                            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.spliceArrayState)(field, {
                                                startIndex: index,
                                                deleteCount: 1
                                            });
                                            field.value.splice(index, 1);
                                            return field.onInput(field.value);
                                        });
                                    }
                                })
                            }, 'remove')
                        ]
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_index_mjs__.FormActiveFieldsProvider, {
                        name: "nester",
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__hooks_mjs__.SubFormProvider, {
                            value: {
                                value,
                                collection
                            },
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
                                isNew: (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__.isNewRecord)(value),
                                record: value,
                                parent: recordData,
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordIndexProvider, {
                                    index: index,
                                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_settings_hooks_useIsAllowToSetDefaultValue_mjs__.DefaultValueProvider, {
                                        isAllowToSetDefaultValue: isAllowToSetDefaultValue,
                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                                            onlyRenderProperties: true,
                                            basePath: field.address.concat(index),
                                            schema: fieldSchema
                                        })
                                    })
                                })
                            })
                        })
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Divider, {})
                ]
            }, index);
        })
    }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: field.editable && allowMultiple && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tooltip, {
            title: t('Add new'),
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                type: 'default',
                className: styles.button,
                block: true,
                icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {}),
                onClick: ()=>{
                    const result = field.value;
                    result.push({});
                    field.value = result;
                }
            })
        }, 'add')
    });
}, {
    displayName: 'ToManyNester'
});
export { Nester };
