"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.getTreeDataByAnalysis = exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _useProps2 = _interopRequireDefault(require("@vtx/ol-map/lib/use-props"));
var _antd = require("antd");
var _icons = require("@ant-design/icons");
var _Tools = _interopRequireDefault(require("./Tools"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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; }
var _getTreeDataByAnalysis = exports.getTreeDataByAnalysis = function getTreeDataByAnalysis(data) {
  var treeData = [];
  data.map(function (v) {
    var _v$analysis;
    var item = {
      key: v.key,
      label: v.name,
      icon: v === null || v === void 0 ? void 0 : v.icon
    };
    if ((_v$analysis = v.analysis) !== null && _v$analysis !== void 0 && _v$analysis.length) {
      item.children = _getTreeDataByAnalysis(v.analysis);
    }
    treeData.push(item);
  });
  return treeData;
};
var AnalysisTools = function AnalysisTools(props) {
  var _useProps = (0, _useProps2["default"])(),
    map = _useProps.map;
  var center = props.center,
    zoom = props.zoom;
  var _useState = (0, _react.useState)([]),
    _useState2 = _slicedToArray(_useState, 2),
    groupList = _useState2[0],
    setGroupList = _useState2[1];
  var _useState3 = (0, _react.useState)(null),
    _useState4 = _slicedToArray(_useState3, 2),
    groupKey = _useState4[0],
    setGroupKey = _useState4[1];

  // const [list, setList] = useState([]);
  // const [moreList, setMoreList] = useState([]);
  var _useState5 = (0, _react.useState)(null),
    _useState6 = _slicedToArray(_useState5, 2),
    activeKey = _useState6[0],
    setActiveKey = _useState6[1];
  var data = (0, _react.useRef)([]);

  // 监听分析组件加载变化
  (0, _react.useEffect)(function () {
    var _map$analysis;
    var first = false;
    var analysisChange = function analysisChange(analysis) {
      var _data$;
      var _data = _getTreeDataByAnalysis(analysis || []);
      data.current = _data;
      setGroupList(_data.map(function (v) {
        return {
          label: v.label,
          key: v.key,
          image: v.icon
        };
      }));

      // 初始化
      if (!first && _data !== null && _data !== void 0 && (_data$ = _data[0]) !== null && _data$ !== void 0 && (_data$ = _data$.children) !== null && _data$ !== void 0 && _data$.length) {
        first = true;
        onInit(_data);
      }
    };
    analysisChange(map === null || map === void 0 || (_map$analysis = map.analysis) === null || _map$analysis === void 0 ? void 0 : _map$analysis.analysis);
    map.analysis.on('change', analysisChange);
    return function () {
      map.analysis.un('change', analysisChange);
    };
  }, []);
  var onInit = function onInit(data) {
    setGroupKey(data[0].key);

    // 设置默认选中key
    map.setAnalysisActiveKey(data[0].children[0].key);
  };

  // 监听选中的key
  (0, _react.useEffect)(function () {
    var activeChange = function activeChange(_activeKey) {
      setActiveKey(_activeKey);
      if (_activeKey) {
        if ((center === null || center === void 0 ? void 0 : center.length) == 2) {
          map.getView().setCenter(center);
        }
        if (zoom) {
          map.getView().setZoom(zoom);
        }
      }
    };
    map.analysis.on('activeChange', activeChange);
    return function () {
      map.analysis.un('activeChange', activeChange);
    };
  }, []);
  var onGroup = function onGroup(v) {
    setGroupKey(v.key);
  };
  var _useMemo = (0, _react.useMemo)(function () {
      var _data$current$find;
      var _list = ((_data$current$find = data.current.find(function (d) {
        return d.key === groupKey;
      })) === null || _data$current$find === void 0 ? void 0 : _data$current$find.children) || [];
      var _moreList = [];
      if (_list.length > 8) {
        _moreList = _list.slice(8, _list.length);
        _list = _list.slice(0, 8);
      }
      return {
        list: _list,
        moreList: _moreList
      };
    }, [data.current, groupList, groupKey]),
    _useMemo$list = _useMemo.list,
    list = _useMemo$list === void 0 ? [] : _useMemo$list,
    _useMemo$moreList = _useMemo.moreList,
    moreList = _useMemo$moreList === void 0 ? [] : _useMemo$moreList;
  var onChange = function onChange(l) {
    try {
      map.hideTemplate();
    } catch (err) {}
    map.setAnalysisActiveKey(activeKey === l.key ? null : l.key);
  };
  var groupItem = groupList.find(function (v) {
    return v.key === groupKey;
  }) || {};
  var groupText = groupItem.label;
  var groupIcon = groupItem.image;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('analysis-gis-tools-list')
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('analysis-gis-tools-list-left')
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('analysis-gis-tools-list-left-group')
  }, /*#__PURE__*/_react["default"].createElement(_antd.Dropdown, {
    overlay: /*#__PURE__*/_react["default"].createElement(_antd.Menu, {
      items: groupList,
      onClick: onGroup,
      selectedKeys: groupKey
    })
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('analysis-gis-tools-list-left-group-item'),
    onClick: function onClick(e) {
      return e.preventDefault();
    }
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: groupIcon,
    alt: "",
    width: 20,
    height: 20
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])('analysis-gis-tools-list-left-group-item-text')
  }, groupText || '分析'), /*#__PURE__*/_react["default"].createElement(_icons.DownOutlined, null)))), (list === null || list === void 0 ? void 0 : list.length) > 0 && list.map(function (v, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: index,
      onClick: function onClick() {
        return onChange(v);
      },
      className: (0, _classnames["default"])('analysis-gis-tools-list-left-item', {
        'analysis-gis-tools-list-left-item-active': activeKey === v.key
      })
    }, v.label);
  }), (moreList === null || moreList === void 0 ? void 0 : moreList.length) > 0 && /*#__PURE__*/_react["default"].createElement(_antd.Dropdown, {
    overlay: /*#__PURE__*/_react["default"].createElement(_antd.Menu, {
      items: moreList,
      onClick: onChange,
      selectedKeys: activeKey
    })
  }, /*#__PURE__*/_react["default"].createElement("a", {
    onClick: function onClick(e) {
      return e.preventDefault();
    },
    className: (0, _classnames["default"])('analysis-gis-tools-list-left-more')
  }, /*#__PURE__*/_react["default"].createElement("span", null, "\u66F4\u591A"), /*#__PURE__*/_react["default"].createElement(_icons.DownOutlined, null)))), /*#__PURE__*/_react["default"].createElement(_Tools["default"], null));
};
var _default = exports["default"] = AnalysisTools;
//# sourceMappingURL=AnalysisTools.js.map
