import * as React from 'react';
import { size, map, get } from 'lodash';
import { observer } from 'mobx-react';
import { P as PackageWrapper } from './PackageWrapper-2f19b461.js';
import { p as i18n$2, q as appContext, r as Popconfirm, I as Icon, s as IconButton, F as FieldItem, u as useStaticCallback, v as MAP_DRAG_PACKAGE, G as GEO_TYPES, w as GeometryType, D as DataSource, x as SortList, C as CheckList, y as Scrollbar, z as MapStateSet } from './MapEditView-f6381866.js';
import 'classnames';
import 'perfect-scrollbar';
import $ from 'jquery';
import { useUpdate } from 'ahooks';
import 'react-use';
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 '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 'rc-menu';
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-rnd';
import 'react-beautiful-dnd';

const i18n$1 = i18n$2.mapEdit.packageConfig;
const prefixCls$1 = 'map-package-config-header';
const MapPackageConfigHeader = observer(() => {
    const { routeStore } = React.useContext(appContext.getContext());
    const { SuppFiltersStore, GeoFiltersStore, onClearAll, MapSettingStore, onSearch,
    // MapStatusStore,
     } = routeStore;
    const { changePackageConfigShow } = MapSettingStore;
    const { suppFilters } = SuppFiltersStore;
    const { geoFilters } = GeoFiltersStore;
    const hasData = React.useMemo(() => {
        return !!(size(suppFilters) || size(geoFilters));
    }, [suppFilters, geoFilters]);
    return (React.createElement("div", { className: `${prefixCls$1}-flex` },
        React.createElement("div", { className: `${prefixCls$1}-title` },
            React.createElement("span", { className: `${prefixCls$1}-data-title` }, i18n$1.dataTitle),
            size(suppFilters) > 0 && (React.createElement("div", { className: `${prefixCls$1}-search` },
                i18n$1.click,
                React.createElement("div", { onClick: onSearch, className: `${prefixCls$1}-search-click` }, i18n$2.mapEdit.startAnalysis),
                i18n$1.addToMap))),
        hasData && (React.createElement(React.Fragment, null,
            React.createElement(Popconfirm, { placement: "bottomRight", okText: i18n$2.mapEdit.okText, cancelText: i18n$2.mapEdit.cancelText, message: `${i18n$2.mapEdit.confirmClearData}${i18n$2.mapEdit.questionMark}`, trigger: "click", emotion: 'help-2', onConfirm: onClearAll },
                React.createElement("div", { className: "flex_center" },
                    React.createElement(Icon, { icon: "delete-2", className: `${prefixCls$1}-clear-icon`, size: 12 }),
                    React.createElement("div", { className: `${prefixCls$1}-clear-text mr12` }, i18n$1.clearText))))),
        React.createElement(IconButton, { icon: "close", type: "only-icon", onClick: changePackageConfigShow })));
});

const { useMemo, useCallback } = React;
const prefixCls = 'dm-map-packages-config';
const removeClass = (target) => {
    $(target).removeClass(`${prefixCls}-success-border`);
    $(target).removeClass(`${prefixCls}-warning-border`);
};
const _onDragLeave = (event) => {
    const target = event.currentTarget;
    if ($(target).hasClass('geo-filters') || $(target).hasClass('supp-filters')) {
        removeClass(target);
    }
};
const i18n = i18n$2.mapEdit.packageConfig;
const MapGeoPackageTitle = observer(() => {
    const { routeStore } = React.useContext(appContext.getContext());
    const { GeoFiltersStore } = routeStore;
    const { geoFilters } = GeoFiltersStore;
    return (React.createElement("div", { className: `${prefixCls}-flex mt12` },
        React.createElement("div", { className: `${prefixCls}-title` },
            React.createElement("span", { className: `${prefixCls}-fence-title` }, i18n.fenceTitle),
            size(geoFilters) > 0 && React.createElement("div", { className: `${prefixCls}-search` }, i18n.afterAdd))));
});
const MapPackageConfig = observer(() => {
    var _a, _b, _c;
    const { routeStore } = React.useContext(appContext.getContext());
    const { SuppFiltersStore, GeoFiltersStore, MapStatusStore } = routeStore;
    const { setColAlias, deleteSuppFilter, aliasSuppFilter, setSuppFilters, pushSuppFilter, currentSuppFilter, setCurrentSuppFilter, 
    // getColList: getSuppColList,
    // getColSelectedList: getSuppColSelectedList,
    setColSelectedList: setSuppColSelectedList, getColUuid: getSuppColUuid, getColClassification: getSuppColClassification, getColAlias, getSuppPackage, } = SuppFiltersStore;
    const suppFilters = (_a = SuppFiltersStore === null || SuppFiltersStore === void 0 ? void 0 : SuppFiltersStore.suppFilters) === null || _a === void 0 ? void 0 : _a.slice();
    const { deleteGeoFilter, setGeoFilters, aliasGeoFilter, pushGeoFilter, currentGeoFilter, setCurrentGeoFilter, getColList: getGeoColList, getColClassification: getGeoColClassification, getColSelectedList: getGeoColSelectedList, setColSelectedList: setGeoColSelectedList, getColUuid: getGeoColUuid, getGeoPackage, } = GeoFiltersStore;
    const update = useUpdate();
    const { setMapState } = MapStatusStore;
    const geoFilters = (_b = GeoFiltersStore === null || GeoFiltersStore === void 0 ? void 0 : GeoFiltersStore.geoFilters) === null || _b === void 0 ? void 0 : _b.slice();
    const currentSuppPackage = getSuppPackage(currentSuppFilter);
    const currentGeoPackage = getGeoPackage(currentGeoFilter);
    // 当前supp数据包 Columns
    const { list: suppColList } = (currentSuppPackage === null || currentSuppPackage === void 0 ? void 0 : currentSuppPackage._getColList) || {};
    // 当前geo数据包 Columns
    (currentGeoPackage === null || currentGeoPackage === void 0 ? void 0 : currentGeoPackage._getColList) || {};
    console.log(suppColList, 'suppColList');
    const currentSuppColumns = map(suppColList, (i) => {
        return {
            label: i,
            value: i,
            type: getSuppColClassification({ uuid: currentSuppFilter, col: i }),
        };
    });
    const currentGeoColumns = (_c = getGeoColList({ uuid: currentGeoFilter })) === null || _c === void 0 ? void 0 : _c.map((i) => ({
        label: i,
        value: i,
        type: getGeoColClassification({ uuid: currentGeoFilter, col: i }),
    })).filter((i) => i.type === 'text');
    const currentSuppSelected = currentSuppPackage === null || currentSuppPackage === void 0 ? void 0 : currentSuppPackage.getColSelectedList;
    const currentGeoSelected = currentGeoPackage === null || currentGeoPackage === void 0 ? void 0 : currentGeoPackage.getColSelectedList;
    const geoFilterRender = useCallback((provided, item, index) => {
        const { geometryType, objectType, uuid, alias } = item;
        const _onClick = () => {
            setCurrentGeoFilter(uuid);
            setMapState(MapStateSet.FENCE_SELECT);
        };
        const _deleteGeoFilter = () => {
            deleteGeoFilter(uuid);
        };
        const _aliasGeoFilter = (alias) => {
            aliasGeoFilter(uuid, alias);
        };
        return (React.createElement("div", Object.assign({ ref: provided.innerRef }, provided.draggableProps, { key: index }),
            React.createElement(PackageWrapper, { isCurrent: uuid === currentGeoFilter, key: index, type: geometryType, text: objectType, alias: alias, onClick: _onClick, onDelete: _deleteGeoFilter, onChangeText: _aliasGeoFilter, provided: provided })));
    }, [currentGeoFilter, setCurrentGeoFilter, setMapState, deleteGeoFilter, aliasGeoFilter]);
    const suppFilterRender = useCallback((provided, item, index) => {
        const { geometryType, objectType, uuid, alias } = item;
        const _onClick = () => {
            setCurrentSuppFilter(uuid);
        };
        const _deleteSuppFilter = () => {
            deleteSuppFilter(uuid);
        };
        const _aliasSuppFilter = (alias) => {
            aliasSuppFilter(uuid, alias);
        };
        return (React.createElement("div", Object.assign({ ref: provided.innerRef }, provided.draggableProps, { key: index }),
            React.createElement(PackageWrapper, { isCurrent: uuid === currentSuppFilter, key: index, type: geometryType, text: objectType, alias: alias, onClick: _onClick, onDelete: _deleteSuppFilter, onChangeText: _aliasSuppFilter, deleteTitle: i18n.deleteTitle, provided: provided })));
    }, [currentSuppFilter, setCurrentSuppFilter, deleteSuppFilter, aliasSuppFilter]);
    const suppFiledItemRender = useCallback((provided, item, index) => {
        const { type, value } = item;
        const _onClose = () => {
            currentSuppSelected.splice(index, 1);
            setSuppColSelectedList({
                uuid: currentSuppFilter,
                selected: currentSuppSelected,
            });
        };
        const changeAlias = (value) => {
            setColAlias({
                uuid: currentSuppFilter,
                colUuid: get(item, 'uuid'),
                columnAlias: value,
            }).then(() => {
                requestAnimationFrame(() => {
                    update();
                });
            });
        };
        return (React.createElement("div", Object.assign({ ref: provided.innerRef }, provided.draggableProps, { key: index }),
            React.createElement("div", { style: { display: 'flex', alignItems: 'center', marginBottom: 4, width: 180 } },
                React.createElement(IconButton, Object.assign({ type: "only-icon", icon: "drag-handle", className: `${prefixCls}-drag-handle` }, provided === null || provided === void 0 ? void 0 : provided.dragHandleProps)),
                React.createElement(FieldItem, { key: index, type: type, name: get(item, 'label') || value, checked: true, onClose: _onClose, editable: true, changeAlias: changeAlias }))));
    }, [currentSuppFilter, currentSuppSelected, setColAlias, setSuppColSelectedList, update]);
    const geoFiledItemRender = useCallback((provided, item, index) => {
        const { type, value } = item;
        const _onClose = () => {
            currentGeoSelected.splice(index, 1);
            setGeoColSelectedList({
                uuid: currentGeoFilter,
                selected: currentGeoSelected,
            });
        };
        return (React.createElement("div", Object.assign({ ref: provided.innerRef }, provided.draggableProps, { key: index }),
            React.createElement("div", { style: { display: 'flex', alignItems: 'center', marginBottom: 4, width: 180 } },
                React.createElement(IconButton, Object.assign({ type: "only-icon", icon: "drag-handle", className: `${prefixCls}-drag-handle` }, provided === null || provided === void 0 ? void 0 : provided.dragHandleProps)),
                React.createElement(FieldItem, { key: index, type: type, name: value, checked: true, onClose: _onClose }))));
    }, [currentGeoFilter, currentGeoSelected, setGeoColSelectedList]);
    const _changeSuppColumnsSelect = useStaticCallback((selected) => {
        setSuppColSelectedList({ uuid: currentSuppFilter, selected });
    });
    const _changeGeoColumnsSelect = useStaticCallback((selected) => {
        setGeoColSelectedList({ uuid: currentGeoFilter, selected });
    });
    const _suppValue = () => {
        return map(currentSuppSelected, (value) => {
            const uuid = getSuppColUuid({ uuid: currentSuppFilter, col: value });
            const type = getSuppColClassification({ uuid: currentSuppFilter, col: value });
            const alias = getColAlias({ uuid: currentSuppFilter, col: value });
            return {
                label: alias,
                value: value,
                uuid: uuid,
                type,
            };
        });
    };
    const suppValue = _suppValue();
    const _geoValue = () => {
        return map(currentGeoSelected, (value) => {
            const uuid = getGeoColUuid({ uuid: currentGeoFilter, col: value });
            const type = getGeoColClassification({ uuid: currentGeoFilter, col: value });
            return {
                label: value,
                value: value,
                uuid: uuid,
                type,
            };
        });
    };
    const geoValue = _geoValue();
    const _changeSuppQuotes = useStaticCallback((arr) => {
        const selected = map(arr, (a) => {
            return getSuppColUuid({ uuid: currentSuppFilter, col: a.value });
        });
        setSuppColSelectedList({ uuid: currentSuppFilter, selected, direct: true });
    });
    const _changeGeoQuotes = useStaticCallback((arr) => {
        const selected = map(arr, (a) => {
            return getGeoColUuid({ uuid: currentGeoFilter, col: a.label });
        });
        setGeoColSelectedList({ uuid: currentGeoFilter, selected, direct: true });
    });
    return (React.createElement("div", { className: prefixCls },
        React.createElement("div", { className: `${prefixCls}-content` },
            React.createElement(MapPackageConfigHeader, null),
            React.createElement("div", { className: `${prefixCls}-list` },
                React.createElement("div", { "data-test": "map-supp-pkg-filter-drop-target", className: `${prefixCls}-list-wrapper supp-filters`, onDrop: (event) => {
                        const target = event.currentTarget;
                        $(target).removeClass(`${prefixCls}-success-border`);
                        let item = event.dataTransfer.getData(MAP_DRAG_PACKAGE);
                        if (!item)
                            return;
                        if (item)
                            item = JSON.parse(item);
                        if ($(target).hasClass('supp-filters')) {
                            removeClass(target);
                            const { source, packageUuid, geometryType, geoType, objectType } = item;
                            if ((geometryType || geoType) === GEO_TYPES[GeometryType.polygon] ||
                                (geometryType || geoType) === GEO_TYPES[GeometryType.pointToPoint] ||
                                (geometryType || geoType) === GEO_TYPES[GeometryType.line] ||
                                (geometryType || geoType) === GEO_TYPES[GeometryType.point]) {
                                pushSuppFilter({
                                    source: source === DataSource.private ? DataSource.market : source,
                                    packageUuid,
                                    objectType,
                                    geometryType: geometryType || geoType,
                                });
                            }
                        }
                    }, onDragOver: (event) => {
                        event.preventDefault();
                        const target = event.currentTarget;
                        const item = sessionStorage.getItem(MAP_DRAG_PACKAGE);
                        if (!item) {
                            return;
                        }
                        const _item = JSON.parse(item);
                        const { geometryType, geoType } = _item;
                        if ($(target).hasClass('supp-filters')) {
                            if ((geometryType || geoType) === GEO_TYPES[GeometryType.polygon] ||
                                (geometryType || geoType) === GEO_TYPES[GeometryType.pointToPoint] ||
                                (geometryType || geoType) === GEO_TYPES[GeometryType.line] ||
                                (geometryType || geoType) === GEO_TYPES[GeometryType.point]) {
                                $(target).addClass(`${prefixCls}-success-border`);
                            }
                            else {
                                $(target).addClass(`${prefixCls}-warning-border`);
                            }
                        }
                    }, onDragLeave: _onDragLeave },
                    React.createElement("div", { className: `${prefixCls}-list-wrapper-auto` },
                        React.createElement(SortList, { list: suppFilters, renderItem: suppFilterRender, onChange: setSuppFilters, valueKey: "uuid" })),
                    size(suppFilters) === 0 && React.createElement("div", { className: `${prefixCls}-no-data` }, i18n.noSupp)),
                (size(currentSuppColumns) > 0 || size(suppFilters) > 0) && (React.createElement("div", { className: `${prefixCls}-list-wrapper`, "data-test": "map-supp-pkg-col-check-list" },
                    React.createElement(CheckList, { mode: "plain", dynamicRowHeight: true, size: "compact", text: i18n$2.mapEdit.selectFieldsToBeAddedToMapFilter, listDescriptionClassName: `${prefixCls}-list-wrapper-checklist-title`, flexHeight: true, rowHeight: 30, options: currentSuppColumns, value: currentSuppSelected, onChange: _changeSuppColumnsSelect }))),
                size(suppValue) > 0 && (React.createElement("div", { className: `${prefixCls}-list-wrapper`, key: Date.now() },
                    React.createElement("div", { style: { height: '100%' }, "data-test": "map-supp-pkg-col-sort-list" },
                        React.createElement(Scrollbar, null,
                            React.createElement(SortList, { list: suppValue, renderItem: suppFiledItemRender, onChange: _changeSuppQuotes, valueKey: "uuid" }))))))),
        React.createElement("div", { className: `${prefixCls}-content` },
            React.createElement(MapGeoPackageTitle, null),
            React.createElement("div", { className: `${prefixCls}-list` },
                React.createElement("div", { "data-test": "map-geo-pkg-filter-drop-target", className: `${prefixCls}-list-wrapper geo-filters`, onDrop: (event) => {
                        const target = event.currentTarget;
                        $(target).removeClass(`${prefixCls}-success-border`);
                        const item = event.dataTransfer.getData(MAP_DRAG_PACKAGE);
                        if (!item)
                            return;
                        const _item = JSON.parse(item);
                        if ($(target).hasClass('geo-filters')) {
                            removeClass(target);
                            const { source, packageUuid, geometryType, geoType, objectType } = _item;
                            if (source !== 'sql' &&
                                ((geometryType || geoType) === GEO_TYPES[GeometryType.polygon] ||
                                    (geometryType || geoType) === GEO_TYPES[GeometryType.pointToPolygon])) {
                                pushGeoFilter({
                                    source: source === DataSource.private ? DataSource.market : source,
                                    packageUuid,
                                    objectType,
                                    geometryType: geometryType || geoType,
                                });
                            }
                        }
                    }, onDragOver: (event) => {
                        event.preventDefault();
                        const target = event.currentTarget;
                        const item = sessionStorage.getItem(MAP_DRAG_PACKAGE);
                        if (!item) {
                            return;
                        }
                        const _item = JSON.parse(item);
                        const { geometryType, geoType, source } = _item;
                        if ($(target).hasClass('geo-filters')) {
                            if (source !== 'sql' &&
                                ((geometryType || geoType) === GEO_TYPES[GeometryType.polygon] ||
                                    (geometryType || geoType) === GEO_TYPES[GeometryType.pointToPolygon])) {
                                $(target).addClass(`${prefixCls}-success-border`);
                            }
                            else {
                                $(target).addClass(`${prefixCls}-warning-border`);
                            }
                        }
                    }, onDragLeave: _onDragLeave },
                    React.createElement("div", { className: `${prefixCls}-list-wrapper-auto` },
                        React.createElement(SortList, { list: geoFilters, renderItem: geoFilterRender, onChange: setGeoFilters, valueKey: "uuid" })),
                    size(geoFilters) === 0 && React.createElement("div", { className: `${prefixCls}-no-data` }, i18n.noGeo)),
                (size(currentGeoColumns) > 0 || size(geoFilters) > 0) && (React.createElement("div", { className: `${prefixCls}-list-wrapper`, "data-test": "map-geo-pkg-col-check-list" },
                    React.createElement(CheckList, { mode: "plain", dynamicRowHeight: true, size: "compact", text: i18n$2.mapEdit.selectFieldsToBeAddedToMapFilter, listDescriptionClassName: `${prefixCls}-list-wrapper-checklist-title`, flexHeight: true, rowHeight: 30, options: currentGeoColumns, value: currentGeoSelected, onChange: _changeGeoColumnsSelect }))),
                size(geoValue) > 0 && (React.createElement("div", { className: `${prefixCls}-list-wrapper` },
                    React.createElement("div", { style: { height: '100%' }, "data-test": "map-geo-pkg-col-sort-list" },
                        React.createElement(Scrollbar, null,
                            React.createElement(SortList, { list: geoValue, renderItem: geoFiledItemRender, onChange: _changeGeoQuotes, valueKey: "uuid" })))))))));
});
MapPackageConfig.whyDidYouRender = true;
MapPackageConfig.displayName = 'MapPackageConfig';
var MapPackageConfig$1 = MapPackageConfig;

export { MapPackageConfig$1 as default };
