/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var VueCompositionAPI = require('@vue/composition-api');
var get = require('lodash/get');
var hooks_useDefaultValue = require('../../hooks/useDefaultValue.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

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__default["default"](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 childrenMap = /* @__PURE__ */new Map();
function getChildrenData(treeDataMap, data, childrenKey, rowKey, r) {
  if (childrenMap.get(data)) return childrenMap.get(data);
  var result = r || {
    allChildren: [],
    allChildrenKeys: []
  };
  var children = get__default["default"](data, childrenKey);
  if (!children || !children.length) return result;
  var selectableChildren = children.filter(function (item) {
    var _treeDataMap$get;

    return !((_treeDataMap$get = treeDataMap.get(get__default["default"](item, rowKey))) !== null && _treeDataMap$get !== void 0 && _treeDataMap$get.disabled);
  });
  result.allChildren = result.allChildren.concat(selectableChildren);
  var childrenKeys = selectableChildren.map(function (item) {
    return get__default["default"](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__default["default"](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;
}
function useTreeSelect(props, treeDataMap) {
  var _toRefs = VueCompositionAPI.toRefs(props),
      selectedRowKeys = _toRefs.selectedRowKeys;

  var _useDefaultValue = hooks_useDefaultValue["default"](selectedRowKeys, props.defaultSelectedRowKeys, props.onSelectChange, "selectedRowKeys", "select-change"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2);
      _useDefaultValue2[0];
      var setTSelectedRowKeys = _useDefaultValue2[1];

  var rowDataKeys = VueCompositionAPI.computed(function () {
    var _props$tree;

    return {
      rowKey: props.rowKey || "id",
      childrenKey: ((_props$tree = props.tree) === null || _props$tree === void 0 ? void 0 : _props$tree.childrenKey) || "children"
    };
  });

  function onInnerSelectChange(rowKeys, extraData) {
    if (extraData.currentRowKey === "CHECK_ALL_BOX") {
      handleSelectAll(extraData);
    } else {
      handleSelect(rowKeys, extraData);
    }
  }

  function handleSelectAll(extraData) {
    var newRowKeys = [];
    var newRowData = [];

    if (extraData.type === "check") {
      var arr = _toConsumableArray__default["default"](treeDataMap.value.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__default["default"](item.row, rowDataKeys.value.rowKey));
        }
      }
    }

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

    setTSelectedRowKeys(newRowKeys, newExtraData);
  }

  function handleSelect(rowKeys, extraData) {
    var newRowKeys = _toConsumableArray__default["default"](rowKeys);

    if ((props === null || props === void 0 ? void 0 : props.tree.checkStrictly) === false) {
      if ((extraData === null || extraData === void 0 ? void 0 : extraData.type) === "check") {
        var result = getChildrenData(treeDataMap.value, extraData.currentRowData, rowDataKeys.value.childrenKey, rowDataKeys.value.rowKey);
        var allChildrenKeys = result.allChildrenKeys;
        childrenMap.set(extraData.currentRowData, result);
        newRowKeys = _toConsumableArray__default["default"](new Set(newRowKeys.concat(allChildrenKeys)));
      } else if ((extraData === null || extraData === void 0 ? void 0 : extraData.type) === "uncheck") {
        var children = getChildrenData(treeDataMap.value, extraData.currentRowData, rowDataKeys.value.childrenKey, rowDataKeys.value.rowKey);

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

        newRowKeys = _result.keys;
      }
    }

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

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

    setTSelectedRowKeys(newRowKeys, newExtraData);
  }

  return {
    onInnerSelectChange: onInnerSelectChange
  };
}

exports.childrenMap = childrenMap;
exports["default"] = useTreeSelect;
exports.getChildrenData = getChildrenData;
exports.getRowDataByKeys = getRowDataByKeys;
exports.removeChildrenKeys = removeChildrenKeys;
//# sourceMappingURL=useTreeSelect.js.map
