"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__, {
    Nester: ()=>Nester
});
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 icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../block-provider/index.js");
const external_data_source_index_js_namespaceObject = require("../../../data-source/index.js");
const CollectionRecordProvider_js_namespaceObject = require("../../../data-source/collection-record/CollectionRecordProvider.js");
const isNewRecord_js_namespaceObject = require("../../../data-source/collection-record/isNewRecord.js");
const external_flag_provider_index_js_namespaceObject = require("../../../flag-provider/index.js");
const external_record_provider_index_js_namespaceObject = require("../../../record-provider/index.js");
const external_schema_settings_index_js_namespaceObject = require("../../../schema-settings/index.js");
const useIsAllowToSetDefaultValue_js_namespaceObject = require("../../../schema-settings/hooks/useIsAllowToSetDefaultValue.js");
const external_context_js_namespaceObject = require("./context.js");
const external_hooks_js_namespaceObject = require("./hooks.js");
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, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        input: css(_templateObject()),
        card: css(_templateObject1()),
        button: css(_templateObject2())
    };
});
const Nester = (props)=>{
    const { options } = (0, external_react_namespaceObject.useContext)(external_context_js_namespaceObject.AssociationFieldContext);
    if ([
        'hasOne',
        'belongsTo'
    ].includes(options.type)) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_flag_provider_index_js_namespaceObject.FlagProvider, {
        isInSubForm: true,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ToOneNester, {
            ...props
        })
    });
    if ([
        'hasMany',
        'belongsToMany'
    ].includes(options.type)) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_flag_provider_index_js_namespaceObject.FlagProvider, {
        isInSubForm: true,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ToManyNester, {
            ...props
        })
    });
    return null;
};
const ToOneNester = (props)=>{
    const { field } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const recordV2 = (0, CollectionRecordProvider_js_namespaceObject.useCollectionRecord)();
    const collection = (0, external_data_source_index_js_namespaceObject.useCollection)();
    const isAllowToSetDefaultValue = (0, external_react_namespaceObject.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, external_schema_settings_index_js_namespaceObject.isPatternDisabled)(fieldSchema) && !useIsAllowToSetDefaultValue_js_namespaceObject.interfacesOfUnsupportedDefaultValue.includes(null == collectionField ? void 0 : collectionField.interface) && !(0, external_schema_settings_index_js_namespaceObject.isSystemField)(collectionField, getInterface);
    }, []);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.FormActiveFieldsProvider, {
        name: "nester",
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_hooks_js_namespaceObject.SubFormProvider, {
            value: {
                value: field.value,
                collection
            },
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
                isNew: null == recordV2 ? void 0 : recordV2.isNew,
                record: field.value,
                parent: null == recordV2 ? void 0 : recordV2.data,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(useIsAllowToSetDefaultValue_js_namespaceObject.DefaultValueProvider, {
                    isAllowToSetDefaultValue: isAllowToSetDefaultValue,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Card, {
                        bordered: true,
                        children: props.children
                    })
                })
            })
        })
    });
};
const ToManyNester = (0, schema_namespaceObject.observer)((props)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { options, field, allowMultiple, allowDissociate } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const recordData = (0, CollectionRecordProvider_js_namespaceObject.useCollectionRecordData)();
    const collection = (0, external_data_source_index_js_namespaceObject.useCollection)();
    if (!Array.isArray(field.value)) field.value = [];
    const isAllowToSetDefaultValue = (0, external_react_namespaceObject.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, external_schema_settings_index_js_namespaceObject.isPatternDisabled)(fieldSchema) && !useIsAllowToSetDefaultValue_js_namespaceObject.interfacesOfUnsupportedDefaultValue.includes(null == collectionField ? void 0 : collectionField.interface) && !(0, external_schema_settings_index_js_namespaceObject.isSystemField)(collectionField, getInterface);
    }, []);
    const { styles } = useStyles();
    return field.value.length > 0 ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsxs)(external_react_default().Fragment, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                        style: {
                            textAlign: 'right'
                        },
                        children: [
                            field.editable && allowMultiple && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tooltip, {
                                title: t('Add new'),
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {
                                    style: {
                                        zIndex: 1000,
                                        marginRight: '10px',
                                        color: '#a8a3a3'
                                    },
                                    onClick: ()=>{
                                        (0, schema_namespaceObject.action)(()=>{
                                            if (!Array.isArray(field.value)) field.value = [];
                                            (0, schema_namespaceObject.spliceArrayState)(field, {
                                                startIndex: index + 1,
                                                insertCount: 1
                                            });
                                            field.value.splice(index + 1, 0, (0, isNewRecord_js_namespaceObject.markRecordAsNew)({}));
                                            (0, schema_namespaceObject.each)(field.form.fields, (targetField, key)=>{
                                                if (!targetField) delete field.form.fields[key];
                                            });
                                            return field.onInput(field.value);
                                        });
                                    }
                                })
                            }, 'add'),
                            !field.readPretty && allowed && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tooltip, {
                                title: t('Remove'),
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.CloseOutlined, {
                                    style: {
                                        zIndex: 1000,
                                        color: '#a8a3a3'
                                    },
                                    onClick: ()=>{
                                        (0, schema_namespaceObject.action)(()=>{
                                            (0, schema_namespaceObject.spliceArrayState)(field, {
                                                startIndex: index,
                                                deleteCount: 1
                                            });
                                            field.value.splice(index, 1);
                                            return field.onInput(field.value);
                                        });
                                    }
                                })
                            }, 'remove')
                        ]
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.FormActiveFieldsProvider, {
                        name: "nester",
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_hooks_js_namespaceObject.SubFormProvider, {
                            value: {
                                value,
                                collection
                            },
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordProvider, {
                                isNew: (0, isNewRecord_js_namespaceObject.isNewRecord)(value),
                                record: value,
                                parent: recordData,
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_record_provider_index_js_namespaceObject.RecordIndexProvider, {
                                    index: index,
                                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(useIsAllowToSetDefaultValue_js_namespaceObject.DefaultValueProvider, {
                                        isAllowToSetDefaultValue: isAllowToSetDefaultValue,
                                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                                            onlyRenderProperties: true,
                                            basePath: field.address.concat(index),
                                            schema: fieldSchema
                                        })
                                    })
                                })
                            })
                        })
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Divider, {})
                ]
            }, index);
        })
    }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
        children: field.editable && allowMultiple && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tooltip, {
            title: t('Add new'),
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                type: 'default',
                className: styles.button,
                block: true,
                icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                onClick: ()=>{
                    const result = field.value;
                    result.push({});
                    field.value = result;
                }
            })
        }, 'add')
    });
}, {
    displayName: 'ToManyNester'
});
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
});
