import * as React from 'react';
import React__default, { useState, useEffect } from 'react';
import { cloneDeep, get, set, reduce, size } from 'lodash';
import { a as __awaiter, h as getSignatureUrl, ay as jsxRuntimeExports, ap as Checkbox, s as IconButton, O as map, p as i18n, q as appContext, i as getOssUrl, S as STATICS_URL } from './MapEditView-f6381866.js';
import { observer } from 'mobx-react';
import classNames from 'classnames';
import { useCreation } from 'ahooks';
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 'rc-menu';
import 'perfect-scrollbar';
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';

function useOssByListV2(originList, sourceKey, targetKey, thumb, quality) {
    const sKey = sourceKey;
    const tKey = targetKey !== null && targetKey !== void 0 ? targetKey : sourceKey;
    const [list, setList] = useState(() => {
        const list = targetKey !== undefined ? [] : [];
        return list;
    });
    const timeRef = React__default.useRef(null);
    useEffect(() => {
        (() => __awaiter(this, void 0, void 0, function* () {
            const time = +new Date();
            timeRef.current = time;
            const newList = cloneDeep(originList);
            const length = originList.length;
            const resp = yield Promise.all(originList.map((item) => {
                const url = get(item, sKey);
                return getSignatureUrl(url, thumb, quality);
            }));
            if (time !== timeRef.current) {
                return;
            }
            for (let index = 0; index < length; index++) {
                const signUrl = resp[index];
                set(newList, `${[index]}.${tKey}`, signUrl);
            }
            setList(newList);
        }))();
    }, [originList, sKey, tKey, thumb, quality]);
    return [list];
}

var __assign = undefined && undefined.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = undefined && undefined.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
var prefixCls$1 = 'dmc-map-base-change';
var DIVIDER_ID = 'divider';
var GroupItem = /*#__PURE__*/React.memo(function (props) {
  var id = props.id,
      className = props.className,
      onClick = props.onClick,
      img = props.img,
      name = props.name,
      restProps = __rest(props, ["id", "className", "onClick", "img", "name"]);

  var style = {
    backgroundImage: "url(" + img + ")"
  };
  var isDark = id === 'dark';
  var isLight = id === 'light';

  var _onClick = function _onClick(checked) {
    var key = id;

    if (isDark && checked) {
      key = 'darklabel';
    }

    if (isLight && checked) {
      key = 'lightlabel';
    }

    onClick && onClick(key);
  };

  var _select = function _select() {
    _onClick();
  };

  return jsxRuntimeExports.jsxs("div", __assign({}, restProps, {
    className: classNames(prefixCls$1 + "-content-style", className),
    style: style,
    title: name
  }, {
    children: [jsxRuntimeExports.jsx("div", {
      onClick: _select,
      style: {
        width: '100%',
        height: '100%'
      }
    }, void 0), jsxRuntimeExports.jsx("div", __assign({
      className: prefixCls$1 + "-content-style-label"
    }, {
      children: jsxRuntimeExports.jsx("div", {
        children: name
      }, void 0)
    }), void 0)]
  }), void 0);
});
var MapBaseChange = /*#__PURE__*/React.memo(function (props) {
  var className = props.className,
      headerTitle = props.headerTitle,
      hideBase = props.hideBase,
      _a = props.groupOptions,
      groupOptions = _a === void 0 ? [] : _a,
      onClose = props.onClose,
      currentStyle = props.currentStyle,
      onChange = props.onChange,
      changeHideBase = props.changeHideBase,
      restProps = __rest(props, ["className", "headerTitle", "hideBase", "groupOptions", "onClose", "currentStyle", "onChange", "changeHideBase"]);

  return jsxRuntimeExports.jsxs("div", __assign({}, restProps, {
    className: classNames(prefixCls$1, className)
  }, {
    children: [jsxRuntimeExports.jsxs("div", __assign({
      className: classNames(prefixCls$1 + "-header")
    }, {
      children: [jsxRuntimeExports.jsx(Checkbox, {
        title: headerTitle !== null && headerTitle !== void 0 ? headerTitle : '-',
        checked: hideBase,
        onChange: changeHideBase
      }, void 0), jsxRuntimeExports.jsx(IconButton, {
        icon: "close",
        type: 'only-icon',
        onClick: onClose
      }, void 0)]
    }), void 0), groupOptions.length > 0 && jsxRuntimeExports.jsx("div", __assign({
      className: classNames(prefixCls$1 + "-content")
    }, {
      children: map(groupOptions, function (options) {
        var _a;

        var id = options.id,
            name = options.name,
            img = options.img;

        if (id === DIVIDER_ID) {
          return jsxRuntimeExports.jsx("div", {
            className: classNames(prefixCls$1 + "-line")
          }, id);
        }

        var selected = currentStyle === options.id;
        var cls = classNames(prefixCls$1 + "-group-item", (_a = {}, _a[prefixCls$1 + "-selected"] = selected, _a));
        return jsxRuntimeExports.jsx(GroupItem, {
          id: id,
          name: name,
          img: img,
          className: cls,
          onClick: onChange
        }, id);
      })
    }), void 0)]
  }), void 0);
});

const styleIds = Object.keys(i18n.mapEdit.mapStyles);
const baseUrl = STATICS_URL + '/images/base-map/';
const imgs = {
    base: baseUrl + 'image-base.jpg',
    dark: baseUrl + 'image-dark.jpg',
    darkLabel: baseUrl + 'image-darklabel.jpg',
    light: baseUrl + 'image-light.jpg',
    black: baseUrl + 'image-black.jpg',
    white: baseUrl + 'image-white.jpg',
    lightfresh: baseUrl + 'image-lightfresh.jpg',
    lightlabel: baseUrl + 'image-lightlabel.jpg',
    lightLabel: baseUrl + 'image-lightlabel.jpg',
    graishblue: baseUrl + 'image-graishblue.jpg',
    wxt: baseUrl + 'image-wxt.jpg',
    wxtLabel: baseUrl + 'image-wxt-label.jpg',
    wxtAmap: baseUrl + 'image-wxt-amap.jpg',
    wxtLabelAmap: baseUrl + 'image-wxt-amap-label.jpg',
    tiandituLabel: baseUrl + 'image-tianditu-label.jpg',
    tiandituWxtLabel: baseUrl + 'image-tianditu-wxt.jpg',
};
const prefixCls = 'dm-map-styles-group';
const MapStylesGroup = observer((props) => {
    const { currentStyle, changeMapStyle, onClose } = props;
    const { routeStore } = React.useContext(appContext.getContext());
    const { MapSettingStore, isEmptyType } = routeStore;
    const { mapStyles, mapCustomStyles, hideBaseLayer, changeHideBaseLayer } = MapSettingStore;
    const [stylesList] = useOssByListV2(mapStyles, 'img', 'displayUrl', true, 80);
    const originList = React.useMemo(() => {
        return isEmptyType ? [] : mapCustomStyles;
    }, [isEmptyType, mapCustomStyles]);
    const [stylesCustomList] = useOssByListV2(originList, 'img', 'displayUrl', true, 80);
    const _mapStyles = reduce(stylesList, (acc, m) => {
        const { id, displayUrl } = m;
        const name = styleIds.includes(id) ? get(i18n.mapEdit.mapStyles, id) : '';
        if (m.checked) {
            acc.push({
                name: name ? name : m.content,
                id,
                img: displayUrl ? getOssUrl(displayUrl) : get(imgs, id),
            });
        }
        return acc;
    }, []);
    const _mapCustomStyles = reduce(stylesCustomList, (acc, m) => {
        if (isEmptyType)
            return [];
        const { id, displayUrl } = m;
        const name = styleIds.includes(id) ? get(i18n.mapEdit.mapStyles, id) : '';
        if (m.checked) {
            acc.push({
                name: name ? name : m.content,
                id,
                img: displayUrl ? getOssUrl(displayUrl) : get(imgs, id),
            });
        }
        return acc;
    }, []);
    const allList = useCreation(() => {
        const _all = [..._mapStyles];
        if (size(_mapCustomStyles) > 0) {
            _all.push({
                id: 'divider',
            });
            _all.push(..._mapCustomStyles);
        }
        return _all;
    }, [_mapStyles, _mapCustomStyles]);
    return (React.createElement("div", { className: prefixCls },
        React.createElement(MapBaseChange, { headerTitle: i18n.mapEdit.baseCustom.showBase, currentStyle: currentStyle, onChange: changeMapStyle, groupOptions: allList, hideBase: !hideBaseLayer, changeHideBase: () => changeHideBaseLayer(), onClose: onClose })));
});
MapStylesGroup.defaultProps = {
    currentStyle: 'dark',
};
MapStylesGroup.whyDidYouRender = true;
MapStylesGroup.displayName = 'MapStylesGroup';
var MapStylesGroup$1 = MapStylesGroup;

export { MapStylesGroup$1 as default };
