import { defineComponent, ref, reactive, computed, nextTick, toRefs, watch, openBlock, createElementBlock, normalizeClass, unref, createElementVNode, createVNode, isRef, withCtx, createTextVNode, toDisplayString, createBlock, createCommentVNode } from 'vue';
import '../../../hooks/index.mjs';
import { ElCheckbox } from '../../checkbox/index.mjs';
import { ElInput } from '../../input/index.mjs';
import { Search } from '@element-plus/icons-vue';
import { ElTree } from '../../tree/index.mjs';
import { transferPanelProps, transferPanelEmits } from './transfer-panel.mjs';
import './composables/index.mjs';
import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs';
import { usePropsAlias } from './composables/use-props-alias.mjs';
import { useLocale } from '../../../hooks/use-locale/index.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { useCheck } from './composables/use-check.mjs';

const __default__ = defineComponent({
  name: "ElTransferPanel"
});
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: transferPanelProps,
  emits: transferPanelEmits,
  setup(__props, { expose, emit }) {
    const props = __props;
    const propsAlias = usePropsAlias(props);
    const renderTree = ref(true);
    const tree = ref();
    const { t } = useLocale();
    const ns = useNamespace("unidirectionalTransfer");
    const panelState = reactive({
      checked: [],
      allChecked: false,
      query: ""
    });
    const { isIndeterminate, checkedSummary, checkableData } = useCheck(props, panelState, getInTreeCheckData);
    const originLeafNodeIdList = computed(() => {
      return getLeaf(props.data, 0);
    });
    const getLeaf = function(data, sum) {
      const childKey = props.props.children;
      data.forEach((children) => {
        if (children[childKey]) {
          sum = getLeaf(children[childKey], sum);
        } else {
          sum = sum + 1;
        }
      });
      return sum;
    };
    const getStoreCheck = () => {
      return (props.modelValue || []).filter((item) => {
        return !getNodeData(item);
      });
    };
    function getInTreeCheckData() {
      return panelState.checked.filter((item) => getNodeData(item));
    }
    const handleCheckChange = function() {
      var _a;
      const checkedNodes = ((_a = tree.value) == null ? void 0 : _a.getCheckedNodes(true)) || [];
      panelState.checked = [
        ...getStoreCheck(),
        ...(checkedNodes == null ? void 0 : checkedNodes.map((item) => item[propsAlias.value.key])) || []
      ];
      panelState.allChecked = getInTreeCheckData().length === checkableData.value.length;
      const fatherNodeDataList = panelState.checked.map((item) => {
        var _a2;
        return (_a2 = tree.value) == null ? void 0 : _a2.getNodePath(item);
      });
      emit("checked-change", checkedNodes.map((item, index) => {
        var _a2, _b;
        return {
          ...item,
          fatherData: (_b = fatherNodeDataList[index]) == null ? void 0 : _b.slice(0, ((_a2 = fatherNodeDataList[index]) == null ? void 0 : _a2.length) || 0 - 1)
        };
      }));
      emit("change", panelState.checked);
    };
    const handleAllCheckedChange = (value) => {
      var _a;
      panelState.checked = [
        ...getStoreCheck(),
        ...value ? checkableData.value.map((item) => item[propsAlias.value.key]) : []
      ];
      (_a = tree.value) == null ? void 0 : _a.setCheckedKeys(panelState.checked);
      const fatherNodeDataList = panelState.checked.map((item) => {
        var _a2;
        return (_a2 = tree.value) == null ? void 0 : _a2.getNodePath(item);
      });
      emit("checked-change", value ? checkableData.value.map((item, index) => {
        var _a2, _b;
        return {
          ...item,
          fatherData: (_b = fatherNodeDataList[index]) == null ? void 0 : _b.slice(0, ((_a2 = fatherNodeDataList[index]) == null ? void 0 : _a2.length) || 0 - 1)
        };
      }) : []);
      emit("change", panelState.checked);
    };
    const filterNodeMethod = (value, data, node) => {
      if (typeof props.filterMethod === "function") {
        return props.filterMethod(value, data, node);
      } else {
        const label = String(data[propsAlias.value.label] || data[propsAlias.value.key]);
        return label.toLowerCase().includes(panelState.query.toLowerCase());
      }
    };
    const setChecked = function(keys) {
      var _a;
      panelState.checked = keys;
      (_a = tree.value) == null ? void 0 : _a.setCheckedKeys(keys);
      nextTick(handleCheckChange);
    };
    const removeChecked = function(key) {
      var _a;
      (_a = tree.value) == null ? void 0 : _a.setChecked(key, false, true);
      nextTick(() => {
        handleCheckChange();
      });
    };
    const { checked, allChecked, query } = toRefs(panelState);
    const getNodeData = (key) => {
      var _a;
      return (_a = tree.value) == null ? void 0 : _a.getNode(key);
    };
    watch(() => query.value, () => {
      var _a;
      (_a = tree.value) == null ? void 0 : _a.filter(query.value);
    });
    watch(() => props.data, () => {
      nextTick(handleCheckChange);
    });
    expose({
      setChecked,
      removeChecked,
      getNodeData
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: normalizeClass(unref(ns).b("panel"))
      }, [
        createElementVNode("p", {
          class: normalizeClass(unref(ns).be("panel", "header"))
        }, [
          createVNode(unref(ElCheckbox), {
            modelValue: unref(allChecked),
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(allChecked) ? allChecked.value = $event : null),
            indeterminate: unref(isIndeterminate),
            "validate-event": false,
            onChange: handleAllCheckedChange
          }, {
            default: withCtx(() => [
              createTextVNode(toDisplayString(unref(t)("el.transfer.all")) + " ", 1),
              createElementVNode("span", null, toDisplayString(unref(checkedSummary)), 1)
            ]),
            _: 1
          }, 8, ["modelValue", "indeterminate"])
        ], 2),
        createElementVNode("div", {
          class: normalizeClass([unref(ns).be("panel", "body"), unref(ns).be("panel", "body-left")])
        }, [
          _ctx.filterable || unref(originLeafNodeIdList) >= 10 ? (openBlock(), createBlock(unref(ElInput), {
            key: 0,
            modelValue: unref(query),
            "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(query) ? query.value = $event : null),
            class: normalizeClass(unref(ns).be("panel", "filter")),
            size: "default",
            placeholder: _ctx.placeholder,
            icon: unref(Search),
            clearable: "",
            "validate-event": false
          }, null, 8, ["modelValue", "class", "placeholder", "icon"])) : createCommentVNode("v-if", true),
          renderTree.value ? (openBlock(), createBlock(unref(ElTree), {
            key: 1,
            ref_key: "tree",
            ref: tree,
            modelValue: unref(checked),
            "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => isRef(checked) ? checked.value = $event : null),
            data: _ctx.data,
            "show-checkbox": "",
            "node-key": unref(propsAlias).key,
            props: unref(propsAlias),
            "render-content": _ctx.renderContent,
            "filter-node-method": filterNodeMethod,
            onCheck: handleCheckChange
          }, null, 8, ["modelValue", "data", "node-key", "props", "render-content"])) : createCommentVNode("v-if", true)
        ], 2)
      ], 2);
    };
  }
});
var TransferLeftPanel = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/judai/code/ddll-element-plus/packages/components/unidirectionalTransfer/src/transfer-left-plane.vue"]]);

export { TransferLeftPanel as default };
//# sourceMappingURL=transfer-left-plane.mjs.map
