"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 _mapIcon = _interopRequireDefault(require("@vtx/map-icon/lib/map-icon"));
var _lodash = require("lodash");
var _excluded = ["icon", "text"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 = function getIcon(v) {
  var _ref = (0, _lodash.isString)(v) ? {
      icon: v
    } : v,
    icon = _ref.icon,
    text = _ref.text,
    other = _objectWithoutProperties(_ref, _excluded);
  if (_mapIcon["default"].icon[icon]) {
    return (0, _renderer.getImage)(_objectSpread({
      icon: icon,
      hideFooter: true
    }, other));
  } else if (text) {
    return (0, _renderer.getImage)(_objectSpread({
      text: text,
      hideFooter: true
    }, other));
  }
};
exports.getIcon = getIcon;
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));
  }
};
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.show) {
          imgList.push.apply(imgList, _toConsumableArray(getItem(r)));
        }
      });
    } else {
      imgList.push.apply(imgList, _toConsumableArray(getItem(v)));
    }
  });
  return imgList;
};
var Content = function Content(props) {
  var data = props.data;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-list-wrap')
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-title')
  }, "\u56FE\u4F8B"), (data === null || data === void 0 ? void 0 : data.length) > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-list')
  }, data.map(function (v, i) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: 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;
  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));
    };
    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
  }, show ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_icons.CloseOutlined, {
    className: (0, _classnames["default"])('cs-tools-legend-close-btn'),
    onClick: function onClick() {
      return setShow(false);
    }
  }), /*#__PURE__*/_react["default"].createElement(Content, {
    data: treeData
  })) : /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-content'),
    onClick: function onClick() {
      return setShow(true);
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-legend-title')
  }, "\u56FE\u4F8B")));
}
//# sourceMappingURL=index.js.map
