import { kT as FieldType, ln as TextFilter, lo as NumberFilter, lp as DateFilter, O as map, U as find, u as useStaticCallback, kF as ActionKeys, aM as dialogApi, kD as SelectPosEnum, lq as SortOrderEnum, a6 as MenuItem, a5 as Menu, I as Icon, W as Dropmenu, d as __rest, lr as SubMenu } from './MapEditView-f6381866.js';
import React__default, { useMemo } from 'react';
import { observer } from 'mobx-react';
import classNames from 'classnames';
import { M as MyDataTableContext, p as prefixCls, i as i18n, D as DISABLE_EDIT_HEADER_KEYS } from './index-0b8bebb4.js';
import { max, min, lowerCase, isString, omit } from 'lodash';
import { Divider } from 'rc-menu';
import 'maptalks';
import 'uuid';
import 'mobx';
import 'lz-string';
import 'dayjs';
import 'react-dom';
import 'react-is';
import '@loadable/component';
import 'js-beautify';
import 'axios';
import 'superstruct';
import '@tanem/react-nprogress';
import 'jquery';
import 'chroma-js';
import 'js-cookie';
import 'is-email';
import 'is-uuid';
import 'is-url';
import 'anymatch';
import '@syncpoint/wkx';
import 'buffer';
import '@turf/turf';
import '@mapbox/polylabel';
import 'turf-multipolygon';
import 'turf-polygon';
import 'gcoord';
import 'eventemitter2';
import 'geobuf';
import 'localforage';
import 'perfect-scrollbar';
import 'ahooks';
import 'jszip';
import 'file-saver';
import 'screenfull';
import 'sql-formatter-fork';
import 'array-move';
import 'turf-multilinestring';
import 'copy-to-clipboard';
import 'maptalks.three';
import 'ajv';
import 'spark-md5';
import 'react-draggable';
import 'react-virtualized';
import 'turf-point';
import 'decamelize';
import 'rbush';
import 'qs';
import 'maptalks.snap';
import 'react-use';
import 'react-rnd';
import 'react-beautiful-dnd';
import 'react-contextmenu';
import 'coordtransform';
import './compact-f89fbdd0.js';
import './useDataValue-6bd3790f.js';
import 'react-dnd-html5-backend';
import 'ali-react-table-fork';
import './Transfer-b219601e.js';
import 'ismobilejs';

const adjustZIndexStyle = { zIndex: 1111 };
const IconMap = {
    [FieldType.text]: 'text',
    [FieldType.number]: 'num',
    [FieldType.datetime]: 'time',
    [FieldType.lnglat]: 'geo-global',
    [FieldType.image]: 'image',
};
const filterMap = {
    [FieldType.text]: TextFilter,
    [FieldType.number]: NumberFilter,
    [FieldType.datetime]: DateFilter,
    [FieldType.lnglat]: NumberFilter,
    [FieldType.image]: NumberFilter,
};
const select = (actived) => classNames(`dmc-dropmenu-menu-item`, {
    'dmc-dropmenu-menu-item-actived': actived,
});
const renderPlainItem = (_a) => {
    var { title } = _a, itemProps = __rest(_a, ["title"]);
    return React__default.createElement(MenuItem, Object.assign({}, itemProps), title);
};
const renderSubMenu = (it, children) => {
    return (React__default.createElement(SubMenu, Object.assign({ key: it.title }, omit(it, 'children')), children));
};
const renderChild = (value, title) => {
    return (React__default.createElement("div", { className: select(value) },
        React__default.createElement(Icon, { icon: 'done-check', className: `dmc-dropmenu-drop-icon` }),
        title));
};
const loop = (arrs) => {
    return map(arrs, (it, i) => {
        const isDivider = isString(it);
        if (isDivider) {
            return React__default.createElement(Divider, { key: `${it}-${i}` });
        }
        const isSubMenu = it.children;
        if (isSubMenu) {
            return renderSubMenu(it, loop(it.children));
        }
        else {
            return renderPlainItem(it);
        }
    });
};
const ColDropMenu = observer(({ index, item }) => {
    var _a, _b;
    const { store, options, isFromMyData } = React__default.useContext(MyDataTableContext);
    const { locked, changeVisibleIndex, selectedPos, changeSelectedPos, tableData, changeFilterValues: onOk, changeColIndex, changeCurrentAction, changeAddCol, updateCol, updateItem, isPlain, changeColumnResize, fieldValueObj, fieldLoading, getTotalValue, isSourcePrivateV2, getTotalValueV2, filters, isSourcePrivate, isSourceMarket, isCollaboratedV2, } = store.cTableStore;
    const { type, key, sort, format, name } = item;
    const Filter = filterMap[type];
    const isDisableEdit = DISABLE_EDIT_HEADER_KEYS.includes(key);
    const afterVisibleChange = React__default.useCallback(() => (isSourcePrivateV2 ? getTotalValueV2(key) : getTotalValue(key)), [getTotalValue, getTotalValueV2, isSourcePrivateV2, key]);
    const commonOptions = React__default.useMemo(() => ({
        overlayClassName: `${prefixCls}-adjust-zIndex`,
        onOk,
        emptyValText: i18n.common.emptyVal,
        allSelectLabel: i18n.common.allCheck,
        afterVisibleChange,
        disabled: isSourcePrivate || isSourceMarket,
    }), [afterVisibleChange, isSourceMarket, isSourcePrivate, onOk]);
    const allStringVal = map(tableData, (item) => item[key]);
    // eslint-disable-next-line react-hooks/exhaustive-deps
    const stringVals = (_b = (_a = find(filters, { column: key })) === null || _a === void 0 ? void 0 : _a.values) !== null && _b !== void 0 ? _b : [];
    const maxVal = max(stringVals);
    const minVal = min(stringVals);
    const textOptions = React__default.useMemo(() => {
        return Object.assign(Object.assign({}, commonOptions), { values: stringVals, list: fieldValueObj[key], loading: fieldLoading });
    }, [commonOptions, fieldLoading, fieldValueObj, key, stringVals]);
    const numberOptions = React__default.useMemo(() => {
        return Object.assign(Object.assign({}, commonOptions), { labelMax: i18n.dataTable.maxValue, labelMin: i18n.dataTable.minValue, statisticMin: Math.min(...allStringVal), statisticMax: Math.max(...allStringVal), max: maxVal, min: minVal });
    }, [allStringVal, commonOptions, maxVal, minVal]);
    const datetimeOptions = React__default.useMemo(() => {
        return Object.assign(Object.assign({}, commonOptions), { value: { start: minVal, end: maxVal } });
    }, [commonOptions, maxVal, minVal]);
    const handleColChange = useStaticCallback((info) => {
        const firstIndex = info.key.indexOf('-');
        const type = info.key.substr(0, firstIndex);
        const title = type === ActionKeys.ClearCol
            ? i18n.dataTable.confirmClearCol
            : i18n.dataTable.confirmDeleteCol;
        dialogApi.warning({
            title,
            okText: i18n.dataTable.ok,
            onOk: (e, onClose) => {
                onClose && onClose();
                updateCol(info);
            },
            cancelText: i18n.dataTable.cancel,
            onCancel: (e, onClose) => {
                onClose && onClose();
            },
            wrapClassName: `${prefixCls}-adjust-zIndex`,
            // maskStyle: adjustZIndexStyle,
        });
    });
    const menuItemProps = useMemo(() => {
        return [
            //排序
            {
                title: i18n.dataTable.sort,
                popupClassName: `${prefixCls}-adjust-zIndex`,
                disabled: selectedPos.colIndex === index && selectedPos.status === SelectPosEnum.Filter,
                children: [
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `${ActionKeys.Sort}-`,
                        title: renderChild(!sort, i18n.dataTable.noSorting),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `${ActionKeys.Sort}-${SortOrderEnum.Asc}`,
                        title: renderChild(sort === SortOrderEnum.Asc, i18n.dataTable.ascOrder),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `${ActionKeys.Sort}-${SortOrderEnum.Desc}`,
                        title: renderChild(sort === SortOrderEnum.Desc, i18n.dataTable.descOrder),
                    },
                ],
            },
            'divider',
            //字段类型
            {
                title: i18n.dataTable.fieldType,
                popupClassName: `${prefixCls}-adjust-zIndex`,
                disabled: locked || isDisableEdit || isSourcePrivateV2,
                children: [
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `${ActionKeys.Transform}-${FieldType.text}`,
                        title: renderChild(type === FieldType.text, i18n.dataTable.str),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `${ActionKeys.Transform}-${FieldType.number}`,
                        title: renderChild(type === FieldType.number, i18n.dataTable.num),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `transform-datetime/YYYY`,
                        title: renderChild(type === FieldType.datetime && format === 'YYYY', `${i18n.dataTable.time}：YYYY`),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `transform-datetime/YYYY-MM`,
                        title: renderChild(type === FieldType.datetime && format === 'YYYY-MM', `${i18n.dataTable.time}：YYYY-MM`),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `transform-datetime/YYYY-MM-DD`,
                        title: renderChild(type === FieldType.datetime && format === 'YYYY-MM-DD', `${i18n.dataTable.time}：YYYY-MM-DD`),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `transform-datetime/HH:mm`,
                        title: renderChild(type === FieldType.datetime && format === 'HH:mm', `${i18n.dataTable.time}：HH:mm`),
                    },
                    {
                        selectable: false,
                        onClick: updateCol,
                        key: `transform-datetime/YYYY-MM-DD HH:mm`,
                        title: renderChild(type === FieldType.datetime && format === 'YYYY-MM-DD HH:mm', `${i18n.dataTable.time}：YYYY-MM-DD HH:mm`),
                    },
                ],
            },
            'divider',
            //添加行
            {
                selectable: false,
                disabled: locked || (isPlain && !isSourcePrivateV2),
                onClick: () => updateItem({ key: ActionKeys.AddRow }, undefined, options === null || options === void 0 ? void 0 : options.startAddRow),
                title: i18n.dataTable.addRow,
                key: ActionKeys.AddRow,
            },
            //添加列
            {
                selectable: false,
                //锁定或者被协同
                disabled: locked || isCollaboratedV2,
                onClick: () => {
                    changeVisibleIndex(index);
                    changeCurrentAction(ActionKeys.AddCol);
                    changeAddCol('type', item.type);
                    changeSelectedPos({
                        value: '',
                        rowIndex: -1,
                        colIndex: -1,
                        status: SelectPosEnum.DropVis,
                    });
                },
                title: i18n.dataTable.addCol,
                key: ActionKeys.AddCol,
            },
            'divider',
            //复制列
            {
                selectable: false,
                disabled: locked || isDisableEdit || isCollaboratedV2,
                onClick: () => {
                    changeAddCol('name', `${item.name}-${i18n.dataTable.copy}`);
                    changeVisibleIndex(index);
                    changeCurrentAction(ActionKeys.CopyCol);
                    changeAddCol('type', item.type);
                    changeSelectedPos({
                        value: '',
                        rowIndex: -1,
                        colIndex: -1,
                        status: SelectPosEnum.DropVis,
                    });
                },
                title: i18n.dataTable.copyCol,
                key: ActionKeys.CopyCol,
            },
            'divider',
            //清空列
            {
                selectable: false,
                disabled: locked || isDisableEdit,
                onClick: handleColChange,
                title: i18n.dataTable.clearCol,
                key: `${ActionKeys.ClearCol}-`,
            },
            //删除列
            {
                selectable: false,
                disabled: locked || isDisableEdit || isCollaboratedV2,
                onClick: handleColChange,
                title: i18n.dataTable.deleteCol,
                key: `${ActionKeys.DeleteCol}-`,
            },
        ];
    }, [
        changeAddCol,
        changeCurrentAction,
        changeSelectedPos,
        changeVisibleIndex,
        format,
        handleColChange,
        index,
        isCollaboratedV2,
        isDisableEdit,
        isPlain,
        isSourcePrivateV2,
        item.name,
        item.type,
        locked,
        options === null || options === void 0 ? void 0 : options.startAddRow,
        selectedPos.colIndex,
        selectedPos.status,
        sort,
        type,
        updateCol,
        updateItem,
    ]);
    const filterNode = React__default.useMemo(() => {
        let opts = {};
        if (type === FieldType.text) {
            opts = textOptions;
        }
        else if (type === FieldType.number) {
            opts = numberOptions;
        }
        else if (type === FieldType.datetime) {
            opts = datetimeOptions;
        }
        return (React__default.createElement(MenuItem, { disabled: opts.disabled, 
            // onItemHover={() => undefined}
            onClick: () => undefined, selectable: false }, opts.disabled ? (React__default.createElement("div", null, i18n.dataTable.filter)) : (React__default.createElement(Filter, Object.assign({}, opts),
            React__default.createElement("div", null, i18n.dataTable.filter)))));
    }, [Filter, datetimeOptions, numberOptions, textOptions, type]);
    const renderOverlay = React__default.useCallback(() => {
        return (React__default.createElement(Menu, null,
            filterNode,
            loop(menuItemProps)));
    }, [filterNode, menuItemProps]);
    const overlayStyle = React__default.useMemo(() => {
        return {
            // ...adjustZIndexStyle,
            visibility: `${selectedPos.colIndex === index && selectedPos.status === SelectPosEnum.Filter
                ? 'hidden'
                : 'visible'}`,
        };
    }, [index, selectedPos.colIndex, selectedPos.status]);
    const dropnodeCls = classNames(`${prefixCls}-dropmenu-dropnode`, {
        [`${prefixCls}-dropmenu-dropnode-${lowerCase(type)}`]: type,
    });
    const nameNode = React__default.useMemo(() => {
        return (React__default.createElement("div", { className: `${prefixCls}-dropmenu-dropnode-content` },
            React__default.createElement(Icon, { icon: IconMap[type] }),
            name));
    }, [name, type]);
    const onVisibleChange = React__default.useCallback((visible) => {
        changeSelectedPos({
            value: '',
            rowIndex: -1,
            colIndex: visible ? index : -1,
            status: SelectPosEnum.DropVis,
        });
        changeColIndex(index);
        changeColumnResize(visible);
    }, [changeColIndex, changeColumnResize, changeSelectedPos, index]);
    const visible = React__default.useMemo(() => selectedPos.colIndex === index &&
        [SelectPosEnum.ColActive, SelectPosEnum.DropVis, SelectPosEnum.Filter].includes(selectedPos.status), [index, selectedPos.colIndex, selectedPos.status]);
    return (React__default.createElement("div", { className: dropnodeCls },
        nameNode,
        item.key !== 'index' && isFromMyData && (React__default.createElement(Dropmenu, { overlayStyle: overlayStyle, overlayClassName: classNames(`${prefixCls}-col-dropmenu`), values: [`sort-${item.sort}`, `fieldType-${item.type}`], visible: visible, onVisibleChange: onVisibleChange, renderOverlay: renderOverlay, renderDropNode: () => React__default.createElement(Icon, { icon: "arrow-down" }) }))));
});
ColDropMenu.displayName = 'ColDropMenu';

export { adjustZIndexStyle, ColDropMenu as default };
