/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import Vue from 'vue';
import get from 'lodash/get';
import baseTableProps from '../base-table-props.js';
import enhancedTableProps from '../enhanced-table-props.js';
import { emitEvent } from '../../utils/event.js';
import primaryTableProps from '../primary-table-props.js';
import TableTreeStore from './tree-store.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '../util/common.js';
import 'lodash/isFunction';
import 'lodash/isString';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var childreMap = new Map();
function getChildrenData(treeDataMap, data, childrenKey, rowKey, r) {
  if (childreMap.get(data)) return childreMap.get(data);
  var result = r || {
    allChildren: [],
    allChildrenKeys: []
  };
  var children = get(data, childrenKey);
  if (!children || !children.length) return result;
  var selectableChildren = children.filter(function (item) {
    var _treeDataMap$get;

    return !((_treeDataMap$get = treeDataMap.get(get(item, rowKey))) !== null && _treeDataMap$get !== void 0 && _treeDataMap$get.disabled);
  });
  result.allChildren = result.allChildren.concat(selectableChildren);
  var childrenKeys = selectableChildren.map(function (item) {
    return get(item, rowKey);
  });
  result.allChildrenKeys = result.allChildrenKeys.concat(childrenKeys);

  for (var i = 0, len = children.length; i < len; i++) {
    var tItem = children[i];
    var c = get(tItem, childrenKey);

    if (c !== null && c !== void 0 && c.length) {
      var nextLevelData = getChildrenData(treeDataMap, tItem, childrenKey, rowKey, result);
      result.allChildren = result.allChildren.concat(nextLevelData.allChildren);
      result.allChildrenKeys = result.allChildrenKeys.concat(nextLevelData.allChildrenKeys);
    }
  }

  return result;
}
function removeChildrenKeys(p, r) {
  var selectedRowKeys = p.selectedRowKeys,
      removeKeys = p.removeKeys;
  var result = r || {
    data: [],
    keys: []
  };

  for (var i = 0, len = selectedRowKeys.length; i < len; i++) {
    var key = selectedRowKeys[i];

    if (!removeKeys.includes(key)) {
      result.keys.push(key);
    }
  }

  return result;
}
function getRowDataByKeys(p) {
  var treeDataMap = p.treeDataMap,
      selectedRowKeys = p.selectedRowKeys;
  var result = [];

  for (var i = 0, len = selectedRowKeys.length; i < len; i++) {
    var key = selectedRowKeys[i];
    result.push(treeDataMap.get(key));
  }

  return result;
}
var TreeSelect = Vue.extend({
  props: {
    data: baseTableProps.data,
    rowKey: baseTableProps.rowKey,
    columns: primaryTableProps.columns,
    tree: enhancedTableProps.tree
  },
  data: function data() {
    return {
      dataSource: this.data,
      store: new TableTreeStore()
    };
  },
  computed: {
    childrenKey: function childrenKey() {
      var _this$tree;

      return ((_this$tree = this.tree) === null || _this$tree === void 0 ? void 0 : _this$tree.childrenKey) || "children";
    },
    rowDataKeys: function rowDataKeys() {
      return {
        rowKey: this.rowKey,
        childrenKey: this.childrenKey
      };
    }
  },
  methods: {
    onInnerSelectChange: function onInnerSelectChange(rowKeys, extraData) {
      if (extraData.currentRowKey === "CHECK_ALL_BOX") {
        this.handleSelectAll(extraData);
      } else {
        this.handleSelect(rowKeys, extraData);
      }
    },
    handleSelectAll: function handleSelectAll(extraData) {
      var newRowKeys = [];
      var newRowData = [];

      if (extraData.type === "check") {
        var arr = _toConsumableArray(this.store.treeDataMap.values());

        for (var i = 0, len = arr.length; i < len; i++) {
          var item = arr[i];

          if (!item.disabled) {
            newRowData.push(item.row);
            newRowKeys.push(get(item.row, this.rowKey));
          }
        }
      }

      var newExtraData = _objectSpread(_objectSpread({}, extraData), {}, {
        selectedRowData: newRowData || []
      });

      emitEvent(this, "select-change", newRowKeys, newExtraData);
      this.$emit("update:selectedRowKeys", newRowKeys, newExtraData);
    },
    handleSelect: function handleSelect(rowKeys, extraData) {
      var newRowKeys = _toConsumableArray(rowKeys);

      if ((this === null || this === void 0 ? void 0 : this.tree.checkStrictly) === false) {
        if ((extraData === null || extraData === void 0 ? void 0 : extraData.type) === "check") {
          var result = getChildrenData(this.store.treeDataMap, extraData.currentRowData, this.childrenKey, this.rowKey);
          var allChildrenKeys = result.allChildrenKeys;
          childreMap.set(extraData.currentRowData, result);
          newRowKeys = _toConsumableArray(new Set(newRowKeys.concat(allChildrenKeys)));
        } else if ((extraData === null || extraData === void 0 ? void 0 : extraData.type) === "uncheck") {
          var children = getChildrenData(this.store.treeDataMap, extraData.currentRowData, this.childrenKey, this.rowKey);

          var _result = removeChildrenKeys({
            selectedRowKeys: rowKeys,
            removeKeys: children.allChildrenKeys
          });

          newRowKeys = _result.keys;
        }
      }

      var newRowData = getRowDataByKeys({
        treeDataMap: this.store.treeDataMap,
        selectedRowKeys: newRowKeys
      });

      var newExtraData = _objectSpread(_objectSpread({}, extraData), {}, {
        selectedRowData: newRowData
      });

      emitEvent(this, "select-change", newRowKeys, newExtraData);
      this.$emit("update:selectedRowKeys", newRowKeys, newExtraData);
    }
  }
});

export { childreMap, TreeSelect as default, getChildrenData, getRowDataByKeys, removeChildrenKeys };
//# sourceMappingURL=tree-select.js.map
