"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;
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _useProps2 = _interopRequireDefault(require("../../_util/useProps"));
var _icons = require("@ant-design/icons");
var _itemWrap = _interopRequireDefault(require("../components/item-wrap"));
var _layerControl = require("../layer-control");
var _LayersCheckService = require("./LayersCheckService");
var _Icon = _interopRequireDefault(require("@ant-design/icons/lib/components/Icon"));
var _classnames = _interopRequireDefault(require("classnames"));
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 _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 _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 _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 _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 _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 _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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } /**
 * @returns 气泡卡片式图层控制器
 */
var SubList = function SubList(props) {
  var options = props.options,
    checkedKeys = props.checkedKeys,
    _onChange = props.onChange,
    onCheckAllChange = props.onCheckAllChange;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-layer-sub-list')
  }, options.map(function (v) {
    var _v$children;
    return (_v$children = v.children) !== null && _v$children !== void 0 && _v$children.length ? /*#__PURE__*/_react["default"].createElement(SubListWrap, {
      v: v,
      key: v.key,
      title: v.title,
      value: v.key,
      options: v.children,
      checkedKeys: checkedKeys,
      onChange: _onChange,
      onCheckAllChange: onCheckAllChange
    }) : /*#__PURE__*/_react["default"].createElement("div", {
      key: v.key,
      className: (0, _classnames["default"])('cs-tools-layer-item')
    }, /*#__PURE__*/_react["default"].createElement(_antd.Checkbox, {
      checked: checkedKeys.includes(v.key),
      onChange: function onChange(e) {
        return _onChange(e.target.checked, v.key);
      }
    }, v.icon, v.title));
  }));
};
var SubListWrap = function SubListWrap(props) {
  var v = props.v,
    title = props.title,
    options = props.options,
    checkedKeys = props.checkedKeys,
    onChange = props.onChange,
    onCheckAllChange = props.onCheckAllChange,
    key = props.value;
  return /*#__PURE__*/_react["default"].createElement(_antd.Popover, {
    trigger: "hover",
    placement: "rightTop",
    content: /*#__PURE__*/_react["default"].createElement(SubList, {
      checkedKeys: checkedKeys,
      options: options,
      onChange: onChange,
      onCheckAllChange: onCheckAllChange
    }),
    getPopupContainer: function getPopupContainer(triggerNode) {
      return triggerNode.parentElement;
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-layer-item')
  }, /*#__PURE__*/_react["default"].createElement(_antd.Checkbox, _extends({}, (0, _LayersCheckService.getIndeterminateAndChecked)(options, checkedKeys, key), {
    onChange: function onChange(e) {
      return onCheckAllChange(e.target.checked, options, key);
    }
  }), v.icon, title), /*#__PURE__*/_react["default"].createElement(_icons.RightOutlined, null)));
};
var List = function List(props) {
  var treeData = props.treeData,
    checkedKeys = props.checkedKeys,
    _onChange2 = props.onChange,
    onCheckAllChange = props.onCheckAllChange;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('cs-tools-layer-list')
  }, treeData.map(function (v) {
    var _v$children2;
    return (_v$children2 = v.children) !== null && _v$children2 !== void 0 && _v$children2.length ? /*#__PURE__*/_react["default"].createElement(SubListWrap, {
      v: v,
      key: v.key,
      title: v.title,
      value: v.key,
      options: v.children,
      checkedKeys: checkedKeys,
      onChange: _onChange2,
      onCheckAllChange: onCheckAllChange
    }) : /*#__PURE__*/_react["default"].createElement("div", {
      key: v.key,
      className: (0, _classnames["default"])('cs-tools-layer-item')
    }, /*#__PURE__*/_react["default"].createElement(_antd.Checkbox, {
      checked: checkedKeys.includes(v.key),
      onChange: function onChange(e) {
        return _onChange2(e.target.checked, v.key);
      }
    }, v.icon, v.title));
  }));
};
function _default(props) {
  var _props$showAll = props.showAll,
    showAll = _props$showAll === void 0 ? true : _props$showAll,
    maxHeight = props.maxHeight;
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map;
  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((0, _layerControl.getTreeDataByLayers)(layers));
    };
    layersChange(map.layers.layers);
    map.layers.on('change', layersChange);
    return function () {
      map.layers.un('change', layersChange);
    };
  }, []);
  (0, _react.useEffect)(function () {
    if (treeData) {
      onChange();
    }
  }, [JSON.stringify(treeData)]);
  var onChange = function onChange(checked, key) {
    if (showAll) {
      // 多选
      var _checkedKeys = _toConsumableArray(checkedKeys);
      if (checked) {
        _checkedKeys.push(key);
      } else {
        _checkedKeys = _checkedKeys.filter(function (v) {
          return v !== key;
        });
      }
      setCheckedKeysAndLayersVisible(_checkedKeys);
    } else {
      // 单选
      props.onChange && props.onChange(key);
    }
  };

  // 有子集的选择框
  var onCheckAllChange = function onCheckAllChange(checked, data, key) {
    var _checkedKeys = _toConsumableArray(checkedKeys);
    var keys = (0, _LayersCheckService.getKeys)(data);
    if (checked) {
      _checkedKeys = [].concat(_toConsumableArray(_checkedKeys), _toConsumableArray(keys));
    } else {
      if (key === 'ALL') {
        _checkedKeys = [];
      } else {
        _checkedKeys = _checkedKeys.filter(function (v) {
          return v !== key;
        });
        _checkedKeys = _checkedKeys.filter(function (v) {
          return !keys.includes(v);
        });
      }
    }
    setCheckedKeysAndLayersVisible(_checkedKeys);
  };
  var setCheckedKeysAndLayersVisible = function setCheckedKeysAndLayersVisible(_checkedKeys) {
    var unique_checkedKeys = _toConsumableArray(new Set(_checkedKeys)); // 去重
    setCheckedKeys(unique_checkedKeys);
    props.onChange && props.onChange(unique_checkedKeys);

    // 设置图层显示隐藏
    map.setLayersShowByKeys(unique_checkedKeys);
  };
  var content = function content() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])('cs-tools-layer'),
      style: {
        maxHeight: maxHeight
      }
    }, showAll ? /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])('cs-tools-layer-check-all')
    }, /*#__PURE__*/_react["default"].createElement(_antd.Checkbox, _extends({}, (0, _LayersCheckService.getIndeterminateAndChecked)(treeData, checkedKeys, 'ALL'), {
      onChange: function onChange(e) {
        return onCheckAllChange(e.target.checked, treeData, 'ALL');
      }
    }), "\u5168\u9009")) : null, /*#__PURE__*/_react["default"].createElement(List, {
      treeData: treeData,
      checkedKeys: checkedKeys,
      onChange: onChange,
      onCheckAllChange: onCheckAllChange
    }));
  };
  return /*#__PURE__*/_react["default"].createElement(_itemWrap["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"
      }));
    }
  }));
}
//# sourceMappingURL=index.js.map
