"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const event = require("../../../constants/event.js");
const types = require("../../../utils/types.js");
const strings = require("../../../utils/strings.js");
const index = require("../../tree/index.js");
const treeSelectOption = require("./tree-select-option.js");
const utils = require("./utils.js");
const Vue = require("vue");
const isEqual = require("../../../../../lodash-es/isEqual.js");
const pick = require("../../../../../lodash-es/pick.js");
const isNil = require("../../../../../lodash-es/isNil.js");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const useTree = (props, { attrs, slots, emit }, { select, tree, key }) => {
  Vue.watch([() => props.modelValue, tree], () => {
    if (props.showCheckbox) Vue.nextTick(() => {
      const treeInstance = tree.value;
      if (treeInstance && !isEqual.default(treeInstance.getCheckedKeys(), utils.toValidArray(props.modelValue))) treeInstance.setCheckedKeys(utils.toValidArray(props.modelValue));
    });
  }, {
    immediate: true,
    deep: true
  });
  const propsMap = Vue.computed(() => ({
    value: key.value,
    label: "label",
    children: "children",
    disabled: "disabled",
    isLeaf: "isLeaf",
    ...props.props
  }));
  const getNodeValByProp = (prop, data) => {
    var _a;
    const propVal = propsMap.value[prop];
    if (shared_esmBundler.isFunction(propVal)) return propVal(data, (_a = tree.value) == null ? void 0 : _a.getNode(getNodeValByProp("value", data)));
    else return data[propVal];
  };
  const defaultExpandedParentKeys = utils.toValidArray(props.modelValue).map((value) => {
    return utils.treeFind(props.data || [], (data) => getNodeValByProp("value", data) === value, (data) => getNodeValByProp("children", data), (data, index2, array, parent) => parent && getNodeValByProp("value", parent));
  }).filter((item) => utils.isValidValue(item));
  const cacheOptions = Vue.computed(() => {
    if (!props.renderAfterExpand && !props.lazy) return [];
    const options = [];
    utils.treeEach(props.data.concat(props.cacheData), (node) => {
      const value = getNodeValByProp("value", node);
      options.push({
        value,
        currentLabel: getNodeValByProp("label", node),
        isDisabled: getNodeValByProp("disabled", node)
      });
    }, (data) => getNodeValByProp("children", data));
    return options;
  });
  const getChildCheckedKeys = () => {
    var _a;
    return (_a = tree.value) == null ? void 0 : _a.getCheckedKeys().filter((checkedKey) => {
      var _a2;
      const node = (_a2 = tree.value) == null ? void 0 : _a2.getNode(checkedKey);
      return !isNil.default(node) && types.isEmpty(node.childNodes);
    });
  };
  const emitChange = (val) => {
    if (!isEqual.default(props.modelValue, val)) emit(event.CHANGE_EVENT, val);
  };
  function update(val) {
    emit(event.UPDATE_MODEL_EVENT, val);
    emitChange(val);
  }
  return {
    ...pick.default(Vue.toRefs(props), Object.keys(index.ElTree.props)),
    ...attrs,
    nodeKey: key,
    expandOnClickNode: Vue.computed(() => {
      return !props.checkStrictly && props.expandOnClickNode;
    }),
    defaultExpandedKeys: Vue.computed(() => {
      return props.defaultExpandedKeys ? props.defaultExpandedKeys.concat(defaultExpandedParentKeys) : defaultExpandedParentKeys;
    }),
    renderContent: (h, { node, data, store }) => {
      return h(treeSelectOption.default, {
        value: getNodeValByProp("value", data),
        label: getNodeValByProp("label", data),
        disabled: getNodeValByProp("disabled", data),
        visible: node.visible
      }, props.renderContent ? () => props.renderContent(h, {
        node,
        data,
        store
      }) : slots.default ? () => slots.default({
        node,
        data,
        store
      }) : void 0);
    },
    filterNodeMethod: (value, data, node) => {
      if (props.filterNodeMethod) return props.filterNodeMethod(value, data, node);
      if (!value) return true;
      return new RegExp(strings.escapeStringRegexp(value), "i").test(getNodeValByProp("label", data) || "");
    },
    onNodeClick: (data, node, e) => {
      var _a, _b, _c;
      (_a = attrs.onNodeClick) == null ? void 0 : _a.call(attrs, data, node, e);
      if (props.showCheckbox && props.checkOnClickNode) return;
      if (!props.showCheckbox && (props.checkStrictly || node.isLeaf)) {
        if (!getNodeValByProp("disabled", data)) {
          const option = (_b = select.value) == null ? void 0 : _b.states.options.get(getNodeValByProp("value", data));
          (_c = select.value) == null ? void 0 : _c.handleOptionSelect(option);
        }
      } else if (props.expandOnClickNode) e.proxy.handleExpandIconClick();
    },
    onCheck: (data, params) => {
      var _a;
      if (!props.showCheckbox) return;
      const dataValue = getNodeValByProp("value", data);
      const dataMap = {};
      utils.treeEach([tree.value.store.root], (node) => dataMap[node.key] = node, (node) => node.childNodes);
      const uncachedCheckedKeys = params.checkedKeys;
      const cachedKeys = props.multiple ? utils.toValidArray(props.modelValue).filter((item) => !(item in dataMap) && !uncachedCheckedKeys.includes(item)) : [];
      const checkedKeys = cachedKeys.concat(uncachedCheckedKeys);
      if (props.checkStrictly) update(props.multiple ? checkedKeys : checkedKeys.includes(dataValue) ? dataValue : void 0);
      else if (props.multiple) {
        const childKeys = getChildCheckedKeys();
        update(cachedKeys.concat(childKeys));
      } else {
        const firstLeaf = utils.treeFind([data], (data2) => !utils.isValidArray(getNodeValByProp("children", data2)) && !getNodeValByProp("disabled", data2), (data2) => getNodeValByProp("children", data2));
        const firstLeafKey = firstLeaf ? getNodeValByProp("value", firstLeaf) : void 0;
        const hasCheckedChild = utils.isValidValue(props.modelValue) && !!utils.treeFind([data], (data2) => getNodeValByProp("value", data2) === props.modelValue, (data2) => getNodeValByProp("children", data2));
        update(firstLeafKey === props.modelValue || hasCheckedChild ? void 0 : firstLeafKey);
      }
      Vue.nextTick(() => {
        var _a2;
        const checkedKeys2 = utils.toValidArray(props.modelValue);
        tree.value.setCheckedKeys(checkedKeys2);
        (_a2 = attrs.onCheck) == null ? void 0 : _a2.call(attrs, data, {
          checkedKeys: tree.value.getCheckedKeys(),
          checkedNodes: tree.value.getCheckedNodes(),
          halfCheckedKeys: tree.value.getHalfCheckedKeys(),
          halfCheckedNodes: tree.value.getHalfCheckedNodes()
        });
      });
      (_a = select.value) == null ? void 0 : _a.focus();
    },
    onNodeExpand: (data, node, e) => {
      var _a;
      (_a = attrs.onNodeExpand) == null ? void 0 : _a.call(attrs, data, node, e);
      Vue.nextTick(() => {
        if (!props.checkStrictly && props.lazy && props.multiple && node.checked) {
          const dataMap = {};
          const uncachedCheckedKeys = tree.value.getCheckedKeys();
          utils.treeEach([tree.value.store.root], (node2) => dataMap[node2.key] = node2, (node2) => node2.childNodes);
          const cachedKeys = utils.toValidArray(props.modelValue).filter((item) => !(item in dataMap) && !uncachedCheckedKeys.includes(item));
          const childKeys = getChildCheckedKeys();
          update(cachedKeys.concat(childKeys));
        }
      });
    },
    cacheOptions
  };
};
exports.useTree = useTree;
