import { l0 as FieldType, lW as TextFilter, lX as NumberFilter, lY as DateFilter, U as map, X as find, s as __assign, h as __spreadArray, i as __read, z as useStaticCallback, kU as ActionKeys, aO as dialogApi, kS as SelectPosEnum, lZ as SortOrderEnum, a8 as MenuItem, a7 as Menu, I as Icon, Y as Dropmenu, j as __rest, l_ as SubMenu } from './MapEditView-67426f04.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-0d47f21f.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 'perf-request';
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 './orderBy-e2803ab1.js';
import 'coordtransform';
import './compact-8e87287d.js';
import './useDataValue-bfcabd04.js';
import 'react-dnd-html5-backend';
import 'ali-react-table-fork';
import './Transfer-20410a61.js';
import 'ismobilejs';

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

export { adjustZIndexStyle, ColDropMenu as default };
