"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.getTreeData = exports.getIcon = void 0;
var _react = _interopRequireWildcard(require("react"));
var _useProps2 = _interopRequireDefault(require("../use-props"));
var _classnames = _interopRequireDefault(require("classnames"));
var _renderer = require("../renderer");
var _lodash = require("lodash");
var _excluded = ["icon", "text"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r) { if (Array.isArray(r)) return r; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
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(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
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(_objectSpread({
        icon: 'WS_GW'
      }, _style), {}, {
        type: 'Icon'
      }));
    } else if (type === 'Polygon') {
      return getIcon(_objectSpread(_objectSpread({
        icon: 'AREA'
      }, _style), {}, {
        type: 'Icon'
      }));
    } else {
      return getIcon(_objectSpread({}, _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)(_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(_objectSpread({
          name: l.name,
          src: icon
        }, l));
      });
    } else {
      var icon = getStyle(v.legend);
      var _style = (0, _lodash.isObject)(v.legend) ? v.legend : {};
      icon && imgList.push(_objectSpread({
        name: v.name,
        src: icon
      }, _style));
    }
  } else if (v.style) {
    var style = v.style;
    if ((0, _lodash.isFunction)(v.style)) {
      style = v.style();
    }
    if ((0, _lodash.isString)(style)) {
      var _icon = getStyle(style);
      _icon && imgList.push({
        name: v.name,
        src: _icon
      });
    } else if ((0, _lodash.isObject)(style)) {
      var _icon2 = getStyleByObject(style);
      _icon2 && imgList.push({
        name: v.name,
        src: _icon2
      });
    } else if ((0, _lodash.isArray)(style)) {
      style.map(function (l) {
        var icon = getStyleByObject(l);
        icon && imgList.push({
          name: l.name,
          src: icon
        });
      });
    }
  }
  return imgList;
};
var getTreeData = exports.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) {
          var items = getItem(r).map(function (c) {
            if (c.name && !c.name.startsWith(v.name)) {
              c.name = "".concat(v.name, "-").concat(c.name);
            }
            return c;
          });
          imgList.push.apply(imgList, _toConsumableArray(items));
        }
      });
    } else if (!v.hasOwnProperty('show') || v.show) {
      imgList.push.apply(imgList, _toConsumableArray(getItem(v)));
    }
  });
  return imgList;
};
var Content = function Content(props) {
  var _data2, _data3;
  var data = props.data,
    children = props.children,
    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];
    }
  }
  // console.log(111111111, _data);

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('ol-tools-legend-list-wrap')
  }, ((_data2 = _data) === null || _data2 === void 0 ? void 0 : _data2.length) > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('ol-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"])('ol-tools-legend-list')
    }, list.map(function (v, i) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        key: "".concat(index, "_").concat(i),
        className: (0, _classnames["default"])('ol-tools-legend-item')
      }, /*#__PURE__*/_react["default"].createElement("img", {
        src: v.src,
        alt: "",
        className: (0, _classnames["default"])('ol-tools-legend-item-img')
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])('ol-tools-legend-item-title')
      }, v.name));
    }));
  })), children);
};
function _default(props) {
  var className = props.className,
    style = props.style,
    extra = props.extra,
    children = props.children,
    content = props.content,
    contentRender = props.contentRender,
    column = props.column,
    columLength = props.columLength;
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map,
    TextEnum = _useProps.TextEnum;
  var _useState = (0, _react.useState)([]),
    _useState2 = _slicedToArray(_useState, 2),
    treeData = _useState2[0],
    setTreeData = _useState2[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);
    };
  }, []);
  if (!(treeData !== null && treeData !== void 0 && treeData.length)) {
    return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null);
  }
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, contentRender ? contentRender(treeData) : /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('ol-tools-legend', className),
    style: style
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('ol-tools-legend-content')
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('ol-tools-legend-title')
  }, /*#__PURE__*/_react["default"].createElement("a", null, TextEnum.legend)), extra), /*#__PURE__*/_react["default"].createElement(Content, {
    data: treeData,
    column: column,
    columLength: columLength
  }, content), children));
}
//# sourceMappingURL=index.js.map
