"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__, {
    SubTable: ()=>SubTable
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_antd_style_namespaceObject = require("antd-style");
const external_lodash_namespaceObject = require("lodash");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_index_js_namespaceObject = require("../../index.js");
const index_js_namespaceObject = require("../../../block-provider/hooks/index.js");
const useFormActiveFields_js_namespaceObject = require("../../../block-provider/hooks/useFormActiveFields.js");
const TableSelectorProvider_js_namespaceObject = require("../../../block-provider/TableSelectorProvider.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_data_source_index_js_namespaceObject = require("../../../data-source/index.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_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_action_index_js_namespaceObject = require("../action/index.js");
const Table_js_namespaceObject = require("../table-v2/Table.js");
const external_hooks_js_namespaceObject = require("./hooks.js");
const external_InternalPicker_js_namespaceObject = require("./InternalPicker.js");
const InternalCollapse_js_namespaceObject = require("./SubTabs/InternalCollapse.js");
const external_util_js_namespaceObject = require("./util.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      display: block;\n      border-radius: 0px;\n      border-right: 1px solid rgba(0, 0, 0, 0.06);\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      display: block;\n      border-radius: 0px;\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
function _templateObject2() {
    const data = _tagged_template_literal([
        "\n      .ant-formily-item.ant-formily-item-feedback-layout-loose {\n        margin-bottom: 0px !important;\n      }\n      .ant-formily-editable {\n        vertical-align: sub;\n      }\n      .ant-table-footer {\n        display: flex;\n      }\n    "
    ]);
    _templateObject2 = function() {
        return data;
    };
    return data;
}
function _templateObject3() {
    const data = _tagged_template_literal([
        "\n      .ant-table-footer {\n        padding: 0 !important;\n      }\n      .ant-formily-item-error-help {\n        display: none;\n      }\n      .ant-description-textarea {\n        line-height: 34px;\n      }\n      .ant-table-cell .ant-formily-item-error-help {\n        display: block;\n        position: absolute;\n        font-size: 12px;\n        top: 100%;\n        background: #fff;\n        width: 100%;\n        margin-top: -15px;\n        padding: 3px;\n        z-index: 1;\n        border-radius: 3px;\n        box-shadow: 0 0 10px #eee;\n        animation: none;\n        transform: translateY(0);\n        opacity: 1;\n      }\n    "
    ]);
    _templateObject3 = function() {
        return data;
    };
    return data;
}
function _templateObject4() {
    const data = _tagged_template_literal([
        "\n      position: relative;\n      .ant-input {\n        width: 100%;\n      }\n    "
    ]);
    _templateObject4 = function() {
        return data;
    };
    return data;
}
const useStyles = (0, external_antd_style_namespaceObject.createStyles)((param)=>{
    let { css } = param;
    return {
        addNew: css(_templateObject()),
        select: css(_templateObject1()),
        table: css(_templateObject2()),
        container: css(_templateObject3()),
        input: css(_templateObject4())
    };
});
const SubTable = (0, schema_namespaceObject.observer)((props)=>{
    const { openSize } = props;
    const { styles } = useStyles();
    const { field, options: collectionField } = (0, external_hooks_js_namespaceObject.useAssociationFieldContext)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const [visibleSelector, setVisibleSelector] = (0, external_react_namespaceObject.useState)(false);
    const [selectedRows, setSelectedRows] = (0, external_react_namespaceObject.useState)([]);
    const fieldNames = (0, external_hooks_js_namespaceObject.useFieldNames)(props);
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const labelUiSchema = (0, external_util_js_namespaceObject.useLabelUiSchema)(collectionField, (null == fieldNames ? void 0 : fieldNames.label) || 'label');
    const recordV2 = (0, external_data_source_index_js_namespaceObject.useCollectionRecord)();
    const [fieldValue, setFieldValue] = (0, external_react_namespaceObject.useState)([]);
    const move = (fromIndex, toIndex)=>{
        if (void 0 === toIndex) return;
        if (!(0, schema_namespaceObject.isArr)(field.value)) return;
        if (fromIndex === toIndex) return;
        return (0, schema_namespaceObject.action)(()=>{
            const fromItem = field.value[fromIndex];
            field.value.splice(fromIndex, 1);
            field.value.splice(toIndex, 0, fromItem);
            (0, schema_namespaceObject.exchangeArrayState)(field, {
                fromIndex,
                toIndex
            });
            return field.onInput(field.value);
        });
    };
    field.move = move;
    const options = (0, external_react_namespaceObject.useMemo)(()=>{
        if (field.value && Object.keys(field.value).length > 0) {
            const opts = (Array.isArray(field.value) ? field.value : field.value ? [
                field.value
            ] : []).filter(Boolean).map((option)=>{
                const label = null == option ? void 0 : option[fieldNames.label];
                return {
                    ...option,
                    [fieldNames.label]: (0, external_util_js_namespaceObject.getLabelFormatValue)(compile(labelUiSchema), compile(label))
                };
            });
            return opts;
        }
        return [];
    }, [
        field.value,
        null == fieldNames ? void 0 : fieldNames.label
    ]);
    const pickerProps = {
        size: 'small',
        fieldNames: field.componentProps.fieldNames,
        multiple: true,
        association: {
            target: null == collectionField ? void 0 : collectionField.target
        },
        options,
        onChange: null == props ? void 0 : props.onChange,
        selectedRows,
        setSelectedRows,
        collectionField
    };
    const usePickActionProps = ()=>{
        const { setVisible } = (0, external_index_js_namespaceObject.useActionContext)();
        const { selectedRows, options, collectionField } = (0, external_react_namespaceObject.useContext)(external_index_js_namespaceObject.RecordPickerContext);
        return {
            onClick () {
                const selectData = (0, external_lodash_namespaceObject.unionBy)(selectedRows, options, (null == collectionField ? void 0 : collectionField.targetKey) || 'id');
                const data = field.value || [];
                field.value = (0, external_lodash_namespaceObject.uniqBy)(data.concat(selectData), (null == collectionField ? void 0 : collectionField.targetKey) || 'id');
                field.onInput(field.value);
                setVisible(false);
            }
        };
    };
    const getFilter = ()=>{
        const targetKey = (null == collectionField ? void 0 : collectionField.targetKey) || 'id';
        const list = options.map((option)=>option[targetKey]).filter(Boolean);
        const filter = list.length ? {
            $and: [
                {
                    ["".concat(targetKey, ".$ne")]: list
                }
            ]
        } : {};
        return filter;
    };
    const tabsProps = {
        ...props,
        fieldValue,
        setFieldValue
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        className: styles.container,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_flag_provider_index_js_namespaceObject.FlagProvider, {
                isInSubTable: true,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_data_source_index_js_namespaceObject.CollectionRecordProvider, {
                    record: null,
                    parentRecord: recordV2,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(useFormActiveFields_js_namespaceObject.FormActiveFieldsProvider, {
                        name: "nester",
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalCollapse_js_namespaceObject.InternalCollapse, {
                                ...tabsProps
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Table_js_namespaceObject.Table, {
                                className: styles.table,
                                bordered: true,
                                size: 'small',
                                field: field,
                                showIndex: true,
                                dragSort: field.editable,
                                showDel: field.editable,
                                setFieldValue: setFieldValue,
                                pagination: !!field.componentProps.pagination,
                                rowSelection: {
                                    type: 'none',
                                    hideSelectAll: true
                                },
                                footer: ()=>{
                                    var _field_componentProps, _field_componentProps1;
                                    return field.editable && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                                        children: [
                                            (null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : _field_componentProps.allowAddnew) !== false && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                                                type: 'text',
                                                block: true,
                                                className: styles.addNew,
                                                onClick: ()=>{
                                                    field.value = field.value || [];
                                                    field.value.push((0, isNewRecord_js_namespaceObject.markRecordAsNew)({}));
                                                    setFieldValue([
                                                        ...field.value
                                                    ]);
                                                },
                                                children: t('Add new')
                                            }),
                                            (null === (_field_componentProps1 = field.componentProps) || void 0 === _field_componentProps1 ? void 0 : _field_componentProps1.allowSelectExistingRecord) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                                                type: 'text',
                                                block: true,
                                                className: styles.select,
                                                onClick: ()=>{
                                                    setVisibleSelector(true);
                                                },
                                                children: t('Select')
                                            })
                                        ]
                                    });
                                },
                                isSubTable: true
                            })
                        ]
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_action_index_js_namespaceObject.ActionContextProvider, {
                value: {
                    openSize,
                    openMode: 'drawer',
                    visible: visibleSelector,
                    setVisible: setVisibleSelector
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.RecordPickerProvider, {
                    ...pickerProps,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_collection_manager_index_js_namespaceObject.CollectionProvider_deprecated, {
                        name: null == collectionField ? void 0 : collectionField.target,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.FormProvider, {
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(TableSelectorProvider_js_namespaceObject.TableSelectorParamsProvider, {
                                params: {
                                    filter: getFilter()
                                },
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.SchemaComponentOptions, {
                                    scope: {
                                        usePickActionProps,
                                        useTableSelectorProps: external_InternalPicker_js_namespaceObject.useTableSelectorProps,
                                        useCreateActionProps: index_js_namespaceObject.useCreateActionProps
                                    },
                                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                                        onlyRenderProperties: true,
                                        basePath: field.address,
                                        schema: fieldSchema.parent,
                                        filterProperties: (s)=>'AssociationField.Selector' === s['x-component']
                                    })
                                })
                            })
                        })
                    })
                })
            })
        ]
    });
}, {
    displayName: 'SubTable'
});
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
});
