"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__, {
    FileManageReadPretty: ()=>FileManageReadPretty,
    InternalFileManager: ()=>InternalFileManager
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
const external_index_js_namespaceObject = require("../../index.js");
const TableSelectorProvider_js_namespaceObject = require("../../../block-provider/TableSelectorProvider.js");
const index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_action_index_js_namespaceObject = require("../action/index.js");
const external_input_index_js_namespaceObject = require("../input/index.js");
const external_preview_index_js_namespaceObject = require("../preview/index.js");
const external_hooks_js_namespaceObject = require("./hooks.js");
const external_schema_js_namespaceObject = require("./schema.js");
var external_schema_js_default = /*#__PURE__*/ __webpack_require__.n(external_schema_js_namespaceObject);
const external_util_js_namespaceObject = require("./util.js");
const useTableSelectorProps = ()=>{
    const field = (0, schema_namespaceObject.useField)();
    const { multiple, options = [], setSelectedRows, selectedRows: rcSelectRows = [], onChange } = (0, external_react_namespaceObject.useContext)(external_index_js_namespaceObject.RecordPickerContext);
    const { onRowSelectionChange, rowKey = 'id', ...others } = (0, TableSelectorProvider_js_namespaceObject.useTableSelectorProps)();
    const { setVisible } = (0, external_index_js_namespaceObject.useActionContext)();
    return {
        ...others,
        rowKey,
        rowSelection: {
            type: multiple ? 'checkbox' : 'radio',
            selectedRowKeys: null == rcSelectRows ? void 0 : rcSelectRows.filter((item)=>options.every((row)=>row[rowKey] !== item[rowKey])).map((item)=>item[rowKey])
        },
        onRowSelectionChange (selectedRowKeys, selectedRows) {
            if (multiple) {
                const scopeRows = (0, external_util_js_namespaceObject.flatData)(field.value) || [];
                const allSelectedRows = rcSelectRows || [];
                const otherRows = (0, external_lodash_namespaceObject.differenceBy)(allSelectedRows, scopeRows, rowKey);
                const unionSelectedRows = (0, external_lodash_namespaceObject.unionBy)(otherRows, selectedRows, rowKey);
                const unionSelectedRowKeys = unionSelectedRows.map((item)=>item[rowKey]);
                null == setSelectedRows || setSelectedRows(unionSelectedRows);
                null == onRowSelectionChange || onRowSelectionChange(unionSelectedRowKeys, unionSelectedRows);
            } else {
                null == setSelectedRows || setSelectedRows(selectedRows);
                null == onRowSelectionChange || onRowSelectionChange(selectedRowKeys, selectedRows);
                onChange((null == selectedRows ? void 0 : selectedRows[0]) || null);
                setVisible(false);
            }
        }
    };
};
const InternalFileManager = (props)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1;
    const { value, multiple, onChange, ...others } = props;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const [visibleSelector, setVisibleSelector] = (0, external_react_namespaceObject.useState)(false);
    const [selectedRows, setSelectedRows] = (0, external_react_namespaceObject.useState)([]);
    const insertSelector = (0, external_hooks_js_namespaceObject.useInsertSchema)('Selector');
    const fieldNames = (0, external_hooks_js_namespaceObject.useFieldNames)(props);
    const field = (0, schema_namespaceObject.useField)();
    const [options, setOptions] = (0, external_react_namespaceObject.useState)([]);
    const { getField } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const collectionField = getField(field.props.name);
    const labelUiSchema = (0, external_util_js_namespaceObject.useLabelUiSchema)(null == collectionField ? void 0 : collectionField.target, (null == fieldNames ? void 0 : fieldNames.label) || 'label');
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const { modalProps } = (0, external_index_js_namespaceObject.useActionContext)();
    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 handleSelect = ()=>{
        insertSelector(external_schema_js_default().Selector);
        setVisibleSelector(true);
        setSelectedRows([]);
    };
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (value && Object.keys(value).length > 0) {
            const opts = (Array.isArray(value) ? value : value ? [
                value
            ] : []).filter(Boolean).map((option)=>{
                const label = option[fieldNames.label];
                return {
                    ...option,
                    [fieldNames.label]: (0, external_util_js_namespaceObject.getLabelFormatValue)(compile(labelUiSchema), compile(label))
                };
            });
            setOptions(opts);
        } else setOptions([]);
    }, [
        value,
        null == fieldNames ? void 0 : fieldNames.label
    ]);
    const handleRemove = (file)=>{
        const newOptions = options.filter((option)=>option.id !== file.id);
        setOptions(newOptions);
        if (0 === newOptions.length) return onChange(null);
        onChange(newOptions);
    };
    const pickerProps = {
        size: 'small',
        fieldNames,
        multiple: [
            'o2m',
            'm2m'
        ].includes(null == collectionField ? void 0 : collectionField.interface) && multiple,
        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 { multiple, selectedRows, onChange, options, collectionField } = (0, external_react_namespaceObject.useContext)(external_index_js_namespaceObject.RecordPickerContext);
        return {
            onClick () {
                multiple ? onChange((0, external_lodash_namespaceObject.unionBy)(selectedRows, options, (null == collectionField ? void 0 : collectionField.targetKey) || 'id')) : onChange((null == selectedRows ? void 0 : selectedRows[0]) || null);
                setVisible(false);
            }
        };
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        style: {
            width: '100%',
            overflow: 'auto'
        },
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_preview_index_js_namespaceObject.FileSelector, {
                value: options,
                multiple: multiple,
                quickUpload: (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.quickUpload) !== false,
                selectFile: (null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : _fieldSchema_xcomponentprops1.selectFile) !== false,
                action: "".concat(null == collectionField ? void 0 : collectionField.target, ":create"),
                onSelect: handleSelect,
                onRemove: handleRemove,
                onChange: (changed)=>{
                    if (changed.every((file)=>'uploading' !== file.status)) {
                        changed = changed.filter((file)=>'done' === file.status).map((file)=>file.response.data);
                        multiple ? onChange([
                            ...options,
                            ...changed
                        ]) : onChange(changed[0]);
                    }
                }
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_action_index_js_namespaceObject.ActionContextProvider, {
                value: {
                    openMode: 'drawer',
                    visible: visibleSelector,
                    setVisible: setVisibleSelector,
                    modalProps: {
                        getContainer: (null == others ? void 0 : others.getContainer) || (null == modalProps ? void 0 : modalProps.getContainer)
                    },
                    formValueChanged: false
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.RecordPickerProvider, {
                    ...pickerProps,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(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
                                    },
                                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(schema_namespaceObject.RecursionField, {
                                        onlyRenderProperties: true,
                                        basePath: field.address,
                                        schema: fieldSchema,
                                        filterProperties: (s)=>'AssociationField.Selector' === s['x-component']
                                    })
                                })
                            })
                        })
                    })
                })
            })
        ]
    });
};
const FileManageReadPretty = (0, schema_namespaceObject.connect)((props)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { getField } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const collectionField = getField(fieldSchema.name) || getCollectionJoinField(fieldSchema['x-collection-field']);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_input_index_js_namespaceObject.EllipsisWithTooltip, {
        ellipsis: true,
        children: collectionField ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_preview_index_js_namespaceObject.Preview, {
            ...props
        }) : null
    });
});
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
});
