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_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__ from "../../../block-provider/hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_useFormActiveFields_mjs__ from "../../../block-provider/hooks/useFormActiveFields.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_TableSelectorProvider_mjs__ from "../../../block-provider/TableSelectorProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/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_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__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__action_index_mjs__ from "../action/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__table_v2_Table_mjs__ from "../table-v2/Table.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_mjs__ from "./hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__InternalPicker_mjs__ from "./InternalPicker.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SubTabs_InternalCollapse_mjs__ from "./SubTabs/InternalCollapse.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__util_mjs__ from "./util.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      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, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        addNew: css(_templateObject()),
        select: css(_templateObject1()),
        table: css(_templateObject2()),
        container: css(_templateObject3()),
        input: css(_templateObject4())
    };
});
const SubTable = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const { openSize } = props;
    const { styles } = useStyles();
    const { field, options: collectionField } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useAssociationFieldContext)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const [visibleSelector, setVisibleSelector] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [selectedRows, setSelectedRows] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const fieldNames = (0, __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useFieldNames)(props);
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const labelUiSchema = (0, __WEBPACK_EXTERNAL_MODULE__util_mjs__.useLabelUiSchema)(collectionField, (null == fieldNames ? void 0 : fieldNames.label) || 'label');
    const recordV2 = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.useCollectionRecord)();
    const [fieldValue, setFieldValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const move = (fromIndex, toIndex)=>{
        if (void 0 === toIndex) return;
        if (!(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.isArr)(field.value)) return;
        if (fromIndex === toIndex) return;
        return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.action)(()=>{
            const fromItem = field.value[fromIndex];
            field.value.splice(fromIndex, 1);
            field.value.splice(toIndex, 0, fromItem);
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.exchangeArrayState)(field, {
                fromIndex,
                toIndex
            });
            return field.onInput(field.value);
        });
    };
    field.move = move;
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__util_mjs__.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, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useActionContext)();
        const { selectedRows, options, collectionField } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.RecordPickerContext);
        return {
            onClick () {
                const selectData = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.unionBy)(selectedRows, options, (null == collectionField ? void 0 : collectionField.targetKey) || 'id');
                const data = field.value || [];
                field.value = (0, __WEBPACK_EXTERNAL_MODULE_lodash__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        className: styles.container,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__flag_provider_index_mjs__.FlagProvider, {
                isInSubTable: true,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.CollectionRecordProvider, {
                    record: null,
                    parentRecord: recordV2,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__block_provider_hooks_useFormActiveFields_mjs__.FormActiveFieldsProvider, {
                        name: "nester",
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SubTabs_InternalCollapse_mjs__.InternalCollapse, {
                                ...tabsProps
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__table_v2_Table_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                                        children: [
                                            (null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : _field_componentProps.allowAddnew) !== false && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                                                type: 'text',
                                                block: true,
                                                className: styles.addNew,
                                                onClick: ()=>{
                                                    field.value = field.value || [];
                                                    field.value.push((0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_isNewRecord_mjs__.markRecordAsNew)({}));
                                                    setFieldValue([
                                                        ...field.value
                                                    ]);
                                                },
                                                children: t('Add new')
                                            }),
                                            (null === (_field_componentProps1 = field.componentProps) || void 0 === _field_componentProps1 ? void 0 : _field_componentProps1.allowSelectExistingRecord) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                                                type: 'text',
                                                block: true,
                                                className: styles.select,
                                                onClick: ()=>{
                                                    setVisibleSelector(true);
                                                },
                                                children: t('Select')
                                            })
                                        ]
                                    });
                                },
                                isSubTable: true
                            })
                        ]
                    })
                })
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__action_index_mjs__.ActionContextProvider, {
                value: {
                    openSize,
                    openMode: 'drawer',
                    visible: visibleSelector,
                    setVisible: setVisibleSelector
                },
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.RecordPickerProvider, {
                    ...pickerProps,
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.CollectionProvider_deprecated, {
                        name: null == collectionField ? void 0 : collectionField.target,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.FormProvider, {
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__block_provider_TableSelectorProvider_mjs__.TableSelectorParamsProvider, {
                                params: {
                                    filter: getFilter()
                                },
                                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.SchemaComponentOptions, {
                                    scope: {
                                        usePickActionProps,
                                        useTableSelectorProps: __WEBPACK_EXTERNAL_MODULE__InternalPicker_mjs__.useTableSelectorProps,
                                        useCreateActionProps: __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.useCreateActionProps
                                    },
                                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                                        onlyRenderProperties: true,
                                        basePath: field.address,
                                        schema: fieldSchema.parent,
                                        filterProperties: (s)=>'AssociationField.Selector' === s['x-component']
                                    })
                                })
                            })
                        })
                    })
                })
            })
        ]
    });
}, {
    displayName: 'SubTable'
});
export { SubTable };
