"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = _default;
exports.getTreeDataByLayers = exports.getImgUrl = exports.getIcon = void 0;
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _toolsItem = _interopRequireDefault(require("../tools-item"));
var _useProps2 = _interopRequireDefault(require("../../_util/useProps"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Icon = _interopRequireDefault(require("@ant-design/icons/lib/components/Icon"));
var _renderer = require("../../renderer");
var _lodash = require("lodash");
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 _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); }
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 _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 _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 _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 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); }
var getIcon = function getIcon(_options) {
  var options = (0, _lodash.isString)(_options) ? {
    icon: _options
  } : _options;
  return (0, _renderer.getImage)(_objectSpread({
    type: 'Icon',
    color: '#0080ff'
  }, options));
};
exports.getIcon = getIcon;
var getImgUrl = function getImgUrl(v) {
  if (v.legend) {
    if ((0, _lodash.isFunction)(v.legend)) {
      return v.legend(v);
    } else if ((0, _lodash.isString)(v.legend) || (0, _lodash.isObject)(v.legend)) {
      return getIcon(v.legend);
    }
    return v.legend;
  } else if (v.style) {
    if ((0, _lodash.isString)(v.style)) {
      return getIcon(v.style);
    } else if ((0, _lodash.isObject)(v.style)) {
      var _v$style = v.style,
        type = _v$style.type,
        style = _v$style.style;
      if (type === 'Marker') {
        if ((0, _lodash.isString)(style)) {
          return getIcon(style);
        } else if ((0, _lodash.isObject)(style)) {
          return getIcon(style);
        }
      } else if (type === 'Polyline') {
        return getIcon(_objectSpread({
          icon: 'GD_CSK'
        }, style !== null && style !== void 0 && style.stroke ? style === null || style === void 0 ? void 0 : style.stroke : {}));
      } else if (type === 'Polygon') {
        return getIcon(_objectSpread({
          icon: 'AREA'
        }, style !== null && style !== void 0 && style.fill ? style === null || style === void 0 ? void 0 : style.fill : {}));
      }
    }
  }
};
exports.getImgUrl = getImgUrl;
var getTreeDataByLayers = function getTreeDataByLayers(data) {
  var treeData = [];
  data.map(function (v) {
    var _v$layers, _v$group;
    var item = {
      key: v.key,
      title: v.name
    };
    var imgUrl = getImgUrl(v);
    if (imgUrl) {
      item.icon = /*#__PURE__*/_react["default"].createElement("img", {
        src: imgUrl,
        alt: "",
        className: (0, _classnames["default"])('cs-tools-layer-tree-legend')
      });
    }
    if ((_v$layers = v.layers) !== null && _v$layers !== void 0 && _v$layers.length) {
      item.children = getTreeDataByLayers(v.layers);
    } else if ((_v$group = v.group) !== null && _v$group !== void 0 && _v$group.length) {
      item.children = getTreeDataByLayers(v.group);
    }
    treeData.push(item);
  });
  return treeData;
};
exports.getTreeDataByLayers = getTreeDataByLayers;
function _default(props) {
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map;
  var _props$showAll = props.showAll,
    showAll = _props$showAll === void 0 ? true : _props$showAll,
    treeClassName = props.treeClassName,
    show = props.show,
    maxHeight = props.maxHeight;
  var _useState = (0, _react.useState)([]),
    _useState2 = _slicedToArray(_useState, 2),
    treeData = _useState2[0],
    setTreeData = _useState2[1];
  var _useState3 = (0, _react.useState)([]),
    _useState4 = _slicedToArray(_useState3, 2),
    checkedKeys = _useState4[0],
    setCheckedKeys = _useState4[1];
  (0, _react.useEffect)(function () {
    var layersChange = function layersChange(layers) {
      setCheckedKeys(map.getLayersShowKeys());
      setTreeData(getTreeDataByLayers(layers));
    };
    map.layers.on('change', layersChange);
    return function () {
      map.layers.un('change', layersChange);
    };
  }, []);
  var onCheck = function onCheck(checkedKeys) {
    map.setLayersShowByKeys(checkedKeys);
  };
  var content = function content() {
    return (treeData === null || treeData === void 0 ? void 0 : treeData.length) > 0 && /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        maxHeight: maxHeight || 400,
        overflowY: 'auto'
      }
    }, /*#__PURE__*/_react["default"].createElement(_antd.Tree, {
      showIcon: true,
      checkable: true,
      selectable: false,
      defaultExpandAll: true,
      treeData: showAll ? [{
        title: '全部',
        key: 'ALL',
        children: treeData
      }] : treeData,
      checkedKeys: checkedKeys,
      onCheck: onCheck,
      rootClassName: (0, _classnames["default"])('cs-tools-layer-tree', treeClassName)
    }));
  };
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, show ? /*#__PURE__*/_react["default"].createElement(_toolsItem["default"], {
    content: props.content || content
  }, /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
    component: function component() {
      return /*#__PURE__*/_react["default"].createElement("svg", {
        width: "1em",
        height: "1em",
        fill: "currentColor",
        viewBox: "0 0 1024 1024"
      }, /*#__PURE__*/_react["default"].createElement("path", {
        d: "M76.885358 279.114423l435.001777 219.929638L946.729127 278.922679 511.887135 66.566904 76.789486 279.018551z m61.070261 341.718631L46.334249 575.23009a73.917067 73.917067 0 0 1 0-136.649103L137.955619 393.073895l-91.62137-45.698835a75.067527 75.067527 0 0 1-38.029101-98.620003 56.915822 56.915822 0 0 1 30.487195-30.359366L473.762163 5.752301a105.45885 105.45885 0 0 1 68.708039 0l435.001777 212.64339a66.72669 66.72669 0 0 1 30.487195 98.651961 58.066282 58.066282 0 0 1-30.487195 30.359365L885.818651 393.073895l83.823807 37.901272a73.086179 73.086179 0 0 1 53.592271 75.994286 78.998266 78.998266 0 0 1-53.592271 75.866458l-83.951635 37.837357 83.951635 37.965186a73.086179 73.086179 0 0 1 53.592271 75.898414 79.253924 79.253924 0 0 1-53.592271 75.962329l-419.566436 204.941699a57.523009 57.523009 0 0 1-38.156929 7.60582 68.196723 68.196723 0 0 1-38.188887-7.637777L38.760386 802.989249a66.72669 66.72669 0 0 1-38.156929-68.452381 73.086179 73.086179 0 0 1 45.730792-68.22868z m83.951636 45.475134l-145.117769 68.22868 435.033735 220.217253L946.729127 742.174646l-144.926026-68.196723-251.822951 121.437463a56.947779 56.947779 0 0 1-38.124972 7.637778 68.196723 68.196723 0 0 1-38.188886-7.637778l-251.854908-129.20307z m579.95976-227.695244l-251.790993 121.437464a105.45885 105.45885 0 0 1-68.708039 0l-259.556599-121.437464-144.926026 68.100851 435.001777 220.313125L946.729127 506.905539l-145.021898-68.196723z"
      }));
    }
  })) : /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null));
}
//# sourceMappingURL=index.js.map
