"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = _default;
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _icons = require("@ant-design/icons");
var _ItemWrap = _interopRequireDefault(require("../components/ItemWrap"));
var _layerControl = require("../layer-control");
var _LayersCheckService = require("./LayersCheckService");
var _Icon = _interopRequireDefault(require("../components/Icon"));
var _classnames = _interopRequireDefault(require("classnames"));
require("./index.less");
var _useProps = _interopRequireDefault(require("../../use-props"));
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 _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 _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 _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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, 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"])('ol-tools-layer-sub-list')
  }, options.map(function (v) {
    var _v$children;
    var status = v.status || 3;
    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"])('ol-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);
      }
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])('popover-iconEle')
    }, 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"])('ol-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);
    }
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])('popover-iconEle')
  }, 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"])('ol-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"])('ol-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);
      }
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])('popover-iconEle')
    }, v.icon || ''), v.title));
  }));
};
function _default(props) {
  var _props$showAll = props.showAll,
    showAll = _props$showAll === void 0 ? true : _props$showAll,
    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];
  var map = _useProps["default"].map;
  (0, _react.useEffect)(function () {
    var layersChange = function layersChange(layers) {
      var _map$layers;
      setCheckedKeys((_map$layers = map.layers) === null || _map$layers === void 0 ? void 0 : _map$layers.getShowKeys());
      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.layers.setShowByKeys(unique_checkedKeys);
  };
  return /*#__PURE__*/_react["default"].createElement(_ItemWrap["default"], {
    content: /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
      type: "icon-tuceng"
    })
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('ol-tools-layer'),
    style: {
      maxHeight: maxHeight
    }
  }, showAll ? /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('ol-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
  })));
}
;
//# sourceMappingURL=index.js.map
