"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = _default;
exports.getIcon = void 0;
var _react = _interopRequireWildcard(require("react"));
var _icons = require("@ant-design/icons");
var _useProps2 = _interopRequireDefault(require("../_util/useProps"));
var _classnames = _interopRequireDefault(require("classnames"));
var _renderer = require("../renderer");
var _lodash = require("lodash");
var _excluded = ["icon", "text"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var getIcon = exports.getIcon = function getIcon(v) {
  var _ref = (0, _lodash.isString)(v) ? {
      icon: v
    } : v,
    icon = _ref.icon,
    text = _ref.text,
    other = _objectWithoutProperties(_ref, _excluded);
  return (0, _renderer.getImage)(_objectSpread({
    color: '#007FF9',
    icon: icon,
    hideFooter: true
  }, other));
};
var getStyleByObject = function getStyleByObject(_style) {
  var type = _style.type,
    style = _style.style;
  if (type) {
    if (type === 'Marker') {
      return getIcon(style);
    } else if (type === 'Polyline') {
      return getIcon(_objectSpread({
        icon: 'GD_CSK'
      }, _style));
    } else if (type === 'Polygon') {
      return getIcon(_objectSpread({
        icon: 'AREA'
      }, _style));
    }
  } else if (_style.icon) {
    return (0, _renderer.getImage)(_objectSpread({
      hideFooter: true
    }, _style));
  } else if (_style.image) {
    return _style.image;
  } else {
    return (0, _renderer.getImage)(_style);
  }
};
var getStyle = function getStyle(v) {
  if (v) {
    if ((0, _lodash.isString)(v)) {
      return getIcon(v);
    } else if ((0, _lodash.isObject)(v)) {
      return getStyleByObject(v);
    }
  }
};
var getItem = function getItem(v) {
  var imgList = [];
  if (v.legend) {
    if ((0, _lodash.isArray)(v.legend)) {
      v.legend.map(function (l) {
        var icon = getStyle(l);
        icon && imgList.push({
          name: l.name,
          src: icon
        });
      });
    } else {
      var icon = getStyle(v.legend);
      icon && imgList.push({
        name: v.name,
        src: icon
      });
    }
  } else if (v.style) {
    if ((0, _lodash.isString)(v.style)) {
      var _icon = getStyle(v.style);
      _icon && imgList.push({
        name: v.name,
        src: _icon
      });
    } else if ((0, _lodash.isObject)(v.style)) {
      var _icon2 = getStyleByObject(v.style);
      _icon2 && imgList.push({
        name: v.name,
        src: _icon2
      });
    } else if ((0, _lodash.isArray)(v.style)) {
      v.style.map(function (l) {
        var icon = getStyleByObject(l);
        icon && imgList.push({
          name: l.name,
          src: icon
        });
      });
    }
  }
  return imgList;
};
var getTreeData = function getTreeData(data) {
  var imgList = [];
  data.map(function (v) {
    var _v$group;
    if ((_v$group = v.group) !== null && _v$group !== void 0 && _v$group.length) {
      v.group.map(function (r) {
        if (!r.hasOwnProperty('show') || r.show) {
          imgList.push.apply(imgList, _toConsumableArray(getItem(r)));
        }
      });
    } else {
      imgList.push.apply(imgList, _toConsumableArray(getItem(v)));
    }
  });
  return imgList;
};
var Content = function Content(props) {
  var _data2, _data3;
  var _props$data = props.data,
    data = _props$data === void 0 ? [] : _props$data,
    column = props.column,
    columLength = props.columLength;
  var _data = [];
  if (data !== null && data !== void 0 && data.length) {
    if (column) {
      _data = (0, _lodash.chunk)(data, Math.ceil(data.length / column));
    } else if ((data === null || data === void 0 ? void 0 : data.length) >= 10) {
      if (columLength) {
        _data = (0, _lodash.chunk)(data, columLength);
      } else {
        // 通过十分位 + 1 得到数组需要分割的数量例如：10 => 2、 20 => 3、 30 => 4
        var num = Number(String(data.length)[0]) + 1;
        _data = (0, _lodash.chunk)(data, Math.ceil(data.length / num));
      }
    } else {
      _data = [data];
    }
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-list-wrap')
  }, ((_data2 = _data) === null || _data2 === void 0 ? void 0 : _data2.length) > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-list-column'),
    style: ((_data3 = _data) === null || _data3 === void 0 ? void 0 : _data3.length) >= 2 ? {} : {
      maxWidth: 270,
      maxHeight: 270,
      overflowY: 'auto'
    }
  }, _data.map(function (list, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: "".concat(index),
      className: (0, _classnames["default"])('cs-tools-legend-list')
    }, list.map(function (v, i) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        key: "".concat(index, "_").concat(i),
        className: (0, _classnames["default"])('cs-tools-legend-item')
      }, /*#__PURE__*/_react["default"].createElement("img", {
        src: v.src,
        alt: "",
        className: (0, _classnames["default"])('cs-tools-legend-item-img')
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])('cs-tools-legend-item-title')
      }, v.name));
    }));
  })));
};
function _default(props) {
  var className = props.className,
    style = props.style,
    column = props.column,
    columLength = props.columLength;
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map;
  var _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    show = _useState2[0],
    setShow = _useState2[1];
  var _useState3 = (0, _react.useState)([]),
    _useState4 = _slicedToArray(_useState3, 2),
    treeData = _useState4[0],
    setTreeData = _useState4[1];
  (0, _react.useEffect)(function () {
    var layersChange = function layersChange() {
      var layers = map.getVisibleLayersByKeys();
      setTreeData(getTreeData(layers));
    };
    layersChange();
    map.layers.on('change', layersChange);
    return function () {
      map.layers.un('change', layersChange);
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend', className),
    style: style
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-content')
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-title')
  }, "\u56FE\u4F8B")), /*#__PURE__*/_react["default"].createElement(Content, {
    data: treeData,
    column: column,
    columLength: columLength
  }));
}
//# sourceMappingURL=index.js.map
