/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var get = require('lodash/get');
var intersection = require('lodash/intersection');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');

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

var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
var intersection__default = /*#__PURE__*/_interopDefaultLegacy(intersection);

const childrenMap = /* @__PURE__ */new Map();
function getChildrenData(treeDataMap, data, keys, r) {
  if (childrenMap.get(data)) return childrenMap.get(data);
  const result = r || {
    allChildren: [],
    allChildrenKeys: [],
    leafNodeKeys: []
  };
  const children = get__default["default"](data, keys.childrenKey);
  if (!children || !children.length) return result;
  const selectableChildren = children.filter(item => {
    var _treeDataMap$get;
    return !((_treeDataMap$get = treeDataMap.get(get__default["default"](item, keys.rowKey))) !== null && _treeDataMap$get !== void 0 && _treeDataMap$get.disabled);
  });
  result.allChildren = [...new Set(result.allChildren.concat(selectableChildren))];
  for (let i = 0, len = children.length; i < len; i++) {
    const tItem = children[i];
    const c = get__default["default"](tItem, keys.childrenKey);
    if (c !== null && c !== void 0 && c.length) {
      const nextLevelData = getChildrenData(treeDataMap, tItem, keys, result);
      result.allChildren = [...new Set(result.allChildren.concat(nextLevelData.allChildren))];
    }
  }
  for (let i = 0, len = result.allChildren.length; i < len; i++) {
    const item = result.allChildren[i];
    const children2 = get__default["default"](item, keys.childrenKey);
    const rowValue = get__default["default"](item, keys.rowKey);
    result.allChildrenKeys.push(rowValue);
    if (!children2 || !children2.length) result.leafNodeKeys.push(rowValue);
  }
  result.allChildrenKeys = [...new Set(result.allChildrenKeys)];
  result.leafNodeKeys = [...new Set(result.leafNodeKeys)];
  return result;
}
function removeChildrenKeys(p, r) {
  const {
    selectedRowKeys,
    removeKeys
  } = p;
  const result = r || {
    data: [],
    keys: []
  };
  for (let i = 0, len = selectedRowKeys.length; i < len; i++) {
    const key = selectedRowKeys[i];
    if (!removeKeys.includes(key)) result.keys.push(key);
  }
  return result;
}
function getRowDataByKeys(p) {
  const {
    treeDataMap,
    selectedRowKeys
  } = p;
  const result = [];
  for (let i = 0, len = selectedRowKeys.length; i < len; i++) {
    const key = selectedRowKeys[i];
    result.push(treeDataMap.get(key));
  }
  return result;
}
function useTreeSelect(props, treeDataMap) {
  const {
    selectedRowKeys,
    tree,
    data,
    indeterminateSelectedRowKeys
  } = VueCompositionAPI.toRefs(props);
  const tIndeterminateSelectedRowKeys = VueCompositionAPI.ref([]);
  const [tSelectedRowKeys, setTSelectedRowKeys] = hooks_useDefaultValue["default"](selectedRowKeys, props.defaultSelectedRowKeys || [], props.onSelectChange, "selectedRowKeys", "select-change");
  const rowDataKeys = VueCompositionAPI.computed(() => {
    var _props$tree;
    return {
      rowKey: props.rowKey || "id",
      childrenKey: ((_props$tree = props.tree) === null || _props$tree === void 0 ? void 0 : _props$tree.childrenKey) || "children"
    };
  });
  VueCompositionAPI.watch([tree, treeDataMap, data, tSelectedRowKeys], _ref => {
    let [tree2, treeDataMap2] = _ref;
    if (!tree2 || !treeDataMap2.size || tree2.checkStrictly) return;
    updateIndeterminateState();
  });
  function updateIndeterminateState() {
    if (!tree.value || tree.value.checkStrictly) return;
    if (!tSelectedRowKeys.value.length) {
      tIndeterminateSelectedRowKeys.value = [];
      return;
    }
    const keys = [];
    const parentMap = {};
    for (let i = 0, len = tSelectedRowKeys.value.length; i < len; i++) {
      const rowValue = tSelectedRowKeys.value[i];
      const state = treeDataMap.value.get(rowValue);
      const children = get__default["default"](state.row, rowDataKeys.value.childrenKey);
      if (!children || !children.length) {
        let parentTmp = state.parent;
        while (parentTmp) {
          if (!parentMap[parentTmp.id]) parentMap[parentTmp.id] = [];
          parentMap[parentTmp.id].push(state.row);
          const checkedLength = parentMap[parentTmp.id].length;
          const {
            allChildrenKeys
          } = getChildrenData(treeDataMap.value, parentTmp.row, rowDataKeys.value);
          const parentTmpIndex = keys.indexOf(parentTmp.id);
          const selectedIndex = tSelectedRowKeys.value.indexOf(parentTmp.id);
          if (checkedLength > 0 && checkedLength < allChildrenKeys.length && selectedIndex === -1) parentTmpIndex === -1 && keys.push(parentTmp.id);else parentTmpIndex !== -1 && keys.splice(parentTmpIndex, 1);
          parentTmp = parentTmp.parent;
        }
      }
    }
    tIndeterminateSelectedRowKeys.value = keys;
  }
  function updateParentCheckedState(selectedKeys, currentRowKey, type) {
    if (!tree.value || tree.value.checkStrictly) return;
    const keys = [...selectedKeys];
    const state = treeDataMap.value.get(currentRowKey);
    let parentTmp = state.parent;
    while (parentTmp) {
      const {
        leafNodeKeys
      } = getChildrenData(treeDataMap.value, parentTmp.row, rowDataKeys.value);
      const checkedChildrenKeys = intersection__default["default"](leafNodeKeys, selectedKeys);
      const selectedIndex = keys.indexOf(parentTmp.id);
      if (type === "uncheck") selectedIndex !== -1 && keys.splice(selectedIndex, 1);else if (checkedChildrenKeys.length === leafNodeKeys.length) selectedIndex === -1 && keys.push(parentTmp.id);
      parentTmp = parentTmp.parent;
    }
    return keys;
  }
  function onInnerSelectChange(rowKeys, extraData) {
    if (!tree.value || tree.value.checkStrictly) {
      setTSelectedRowKeys(rowKeys, extraData);
      return;
    }
    if (extraData.currentRowKey === "CHECK_ALL_BOX") handleSelectAll(extraData);else handleSelect(rowKeys, extraData);
  }
  function handleSelectAll(extraData) {
    const newRowKeys = [];
    const newRowData = [];
    if (extraData.type === "check") {
      const arr = [...treeDataMap.value.values()];
      for (let i = 0, len = arr.length; i < len; i++) {
        const item = arr[i];
        if (!item.disabled) {
          newRowData.push(item.row);
          newRowKeys.push(get__default["default"](item.row, rowDataKeys.value.rowKey));
        }
      }
    }
    const newExtraData = {
      ...extraData,
      selectedRowData: newRowData || []
    };
    setTSelectedRowKeys(newRowKeys, newExtraData);
  }
  function handleSelect(rowKeys, extraData) {
    var _props$tree2;
    let newRowKeys = [...rowKeys];
    if (((_props$tree2 = props.tree) === null || _props$tree2 === void 0 ? void 0 : _props$tree2.checkStrictly) === false) {
      if ((extraData === null || extraData === void 0 ? void 0 : extraData.type) === "check") {
        const result = getChildrenData(treeDataMap.value, extraData.currentRowData, rowDataKeys.value);
        const {
          allChildrenKeys
        } = result;
        childrenMap.set(extraData.currentRowData, result);
        newRowKeys = [...new Set(newRowKeys.concat(allChildrenKeys))];
      } else if ((extraData === null || extraData === void 0 ? void 0 : extraData.type) === "uncheck") {
        const children = getChildrenData(treeDataMap.value, extraData.currentRowData, rowDataKeys.value);
        const result = removeChildrenKeys({
          selectedRowKeys: rowKeys,
          removeKeys: children.allChildrenKeys
        });
        newRowKeys = result.keys;
      }
    }
    newRowKeys = updateParentCheckedState(newRowKeys, extraData.currentRowKey, extraData.type);
    const newRowData = getRowDataByKeys({
      treeDataMap: treeDataMap.value,
      selectedRowKeys: newRowKeys
    });
    const newExtraData = {
      ...extraData,
      selectedRowData: newRowData
    };
    setTSelectedRowKeys(newRowKeys, newExtraData);
  }
  return {
    tIndeterminateSelectedRowKeys: indeterminateSelectedRowKeys.value ? indeterminateSelectedRowKeys : tIndeterminateSelectedRowKeys,
    onInnerSelectChange
  };
}

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