"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"] = void 0;
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _vmSubTabs = _interopRequireDefault(require("../../../vm-sub-tabs"));
var _components = require("@vtx/components");
var _api = require("../../api");
var _lodash = require("lodash");
var _Chart = _interopRequireDefault(require("./Chart"));
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 _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; }
var prefix = 'vtx-device-modal-analysis';
var DataAnalysis = function DataAnalysis(props) {
  var id = props.id;
  var _useState = (0, _react.useState)('dan'),
    _useState2 = _slicedToArray(_useState, 2),
    tabKey = _useState2[0],
    setTabKey = _useState2[1];
  var _BaseService$useGetDe = _api.BaseService.useGetDeviceTree(id),
    _BaseService$useGetDe2 = _BaseService$useGetDe.data,
    data = _BaseService$useGetDe2 === void 0 ? [] : _BaseService$useGetDe2;
  var _useState3 = (0, _react.useState)([]),
    _useState4 = _slicedToArray(_useState3, 2),
    selectDeviceIds = _useState4[0],
    setSelectDeviceIds = _useState4[1];
  var deviceIds = (0, _react.useMemo)(function () {
    return [].concat(_toConsumableArray(tabKey === 'dan' ? [] : selectDeviceIds), [id]);
  }, [id, tabKey, selectDeviceIds]);
  var _BaseService$useGetMo = _api.BaseService.useGetMonitor({
      deviceIds: deviceIds
    }),
    _BaseService$useGetMo2 = _BaseService$useGetMo.data,
    monitorTree = _BaseService$useGetMo2 === void 0 ? [] : _BaseService$useGetMo2;
  var initMonitorItemIds = (0, _react.useRef)(null);
  var _useState5 = (0, _react.useState)([]),
    _useState6 = _slicedToArray(_useState5, 2),
    monitorItemIds = _useState6[0],
    setMonitorItemIds = _useState6[1];
  var _useState7 = (0, _react.useState)([]),
    _useState8 = _slicedToArray(_useState7, 2),
    expandedKeys = _useState8[0],
    setExpandedKeys = _useState8[1];
  (0, _react.useEffect)(function () {
    var _getMonitorItemIds = function getMonitorItemIds(data) {
      var _a;
      if ((data === null || data === void 0 ? void 0 : data.type) === 'monitorItem') {
        if (!initMonitorItemIds.current) {
          initMonitorItemIds.current = data.key;
        }
        setMonitorItemIds([data.key]);
        setExpandedKeys([data.key]);
        return;
      }
      if ((0, _lodash.isEmpty)(data === null || data === void 0 ? void 0 : data.children)) {
        return;
      }
      _getMonitorItemIds((_a = data === null || data === void 0 ? void 0 : data.children) === null || _a === void 0 ? void 0 : _a[0]);
    };
    _getMonitorItemIds(monitorTree === null || monitorTree === void 0 ? void 0 : monitorTree[0]);
  }, [JSON.stringify(monitorTree)]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-top")
  }, /*#__PURE__*/_react["default"].createElement(_vmSubTabs["default"], {
    activeKey: tabKey,
    onChange: function onChange(key) {
      setTabKey(key);
      setMonitorItemIds([initMonitorItemIds.current]);
      setExpandedKeys([initMonitorItemIds.current]);
    },
    items: [{
      label: '单点分析',
      key: 'dan'
    }, {
      label: '多点分析',
      key: 'duo'
    }]
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-content")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-content--left")
  }, tabKey === 'duo' && (/*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-content--tree")
  }, /*#__PURE__*/_react["default"].createElement(_components.VtxTree, {
    treeData: data,
    checkable: true,
    checkedKeys: selectDeviceIds,
    onCheck: function onCheck(_, _ref) {
      var checkedNodes = _ref.checkedNodes;
      setSelectDeviceIds(checkedNodes.filter(function (item) {
        return item.type === 'device';
      }).map(function (item) {
        return item.key;
      }));
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-content--tree")
  }, !(0, _lodash.isEmpty)(monitorTree) && (/*#__PURE__*/_react["default"].createElement(_components.VtxTree, Object.assign({
    treeData: monitorTree
  }, tabKey === 'duo' ? {
    selectedKeys: monitorItemIds,
    onSelect: function onSelect(selectedKeys) {
      setMonitorItemIds(selectedKeys);
    }
  } : {
    checkedKeys: monitorItemIds,
    onCheck: function onCheck(_, _ref2) {
      var checkedNodes = _ref2.checkedNodes;
      setMonitorItemIds(checkedNodes.filter(function (item) {
        return item.type === 'monitorType';
      }).map(function (item) {
        return item.key;
      }));
    }
  }, {
    checkable: tabKey === 'dan',
    expandedKeys: expandedKeys,
    onExpand: function onExpand(expandedKeys) {
      setExpandedKeys(expandedKeys);
    },
    autoExpandParent: true
  }))))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefix, "-content--right")
  }, /*#__PURE__*/_react["default"].createElement(_Chart["default"], {
    deviceIds: deviceIds,
    monitorItemIds: monitorItemIds
  }))));
};
var _default = exports["default"] = DataAnalysis;
//# sourceMappingURL=index.js.map
