'use strict';

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

var vue = require('vue');
require('../../../hooks/index.js');
var index$2 = require('../../checkbox/index.js');
var index$3 = require('../../input/index.js');
var Icons = require('@element-plus/icons-vue');
var index$4 = require('../../tree/index.js');
var transferPanel = require('./transfer-panel.js');
require('./composables/index.js');
var pluginVue_exportHelper = require('../../../../_virtual/plugin-vue_export-helper.js');
var usePropsAlias = require('./composables/use-props-alias.js');
var index = require('../../../hooks/use-locale/index.js');
var index$1 = require('../../../hooks/use-namespace/index.js');
var useCheck = require('./composables/use-check.js');

const __default__ = vue.defineComponent({
  name: "ElTransferPanel"
});
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...__default__,
  props: transferPanel.transferPanelProps,
  emits: transferPanel.transferPanelEmits,
  setup(__props, { expose, emit }) {
    const props = __props;
    const propsAlias = usePropsAlias.usePropsAlias(props);
    const renderTree = vue.ref(true);
    const tree = vue.ref();
    const { t } = index.useLocale();
    const ns = index$1.useNamespace("unidirectionalTransfer");
    const panelState = vue.reactive({
      checked: [],
      allChecked: false,
      query: ""
    });
    const { isIndeterminate, checkedSummary, checkableData } = useCheck.useCheck(props, panelState, getInTreeCheckData);
    const originLeafNodeIdList = vue.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);
      vue.nextTick(handleCheckChange);
    };
    const removeChecked = function(key) {
      var _a;
      (_a = tree.value) == null ? void 0 : _a.setChecked(key, false, true);
      vue.nextTick(() => {
        handleCheckChange();
      });
    };
    const { checked, allChecked, query } = vue.toRefs(panelState);
    const getNodeData = (key) => {
      var _a;
      return (_a = tree.value) == null ? void 0 : _a.getNode(key);
    };
    vue.watch(() => query.value, () => {
      var _a;
      (_a = tree.value) == null ? void 0 : _a.filter(query.value);
    });
    vue.watch(() => props.data, () => {
      vue.nextTick(handleCheckChange);
    });
    expose({
      setChecked,
      removeChecked,
      getNodeData
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", {
        class: vue.normalizeClass(vue.unref(ns).b("panel"))
      }, [
        vue.createElementVNode("p", {
          class: vue.normalizeClass(vue.unref(ns).be("panel", "header"))
        }, [
          vue.createVNode(vue.unref(index$2.ElCheckbox), {
            modelValue: vue.unref(allChecked),
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => vue.isRef(allChecked) ? allChecked.value = $event : null),
            indeterminate: vue.unref(isIndeterminate),
            "validate-event": false,
            onChange: handleAllCheckedChange
          }, {
            default: vue.withCtx(() => [
              vue.createTextVNode(vue.toDisplayString(vue.unref(t)("el.transfer.all")) + " ", 1),
              vue.createElementVNode("span", null, vue.toDisplayString(vue.unref(checkedSummary)), 1)
            ]),
            _: 1
          }, 8, ["modelValue", "indeterminate"])
        ], 2),
        vue.createElementVNode("div", {
          class: vue.normalizeClass([vue.unref(ns).be("panel", "body"), vue.unref(ns).be("panel", "body-left")])
        }, [
          _ctx.filterable || vue.unref(originLeafNodeIdList) >= 10 ? (vue.openBlock(), vue.createBlock(vue.unref(index$3.ElInput), {
            key: 0,
            modelValue: vue.unref(query),
            "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => vue.isRef(query) ? query.value = $event : null),
            class: vue.normalizeClass(vue.unref(ns).be("panel", "filter")),
            size: "default",
            placeholder: _ctx.placeholder,
            icon: vue.unref(Icons.Search),
            clearable: "",
            "validate-event": false
          }, null, 8, ["modelValue", "class", "placeholder", "icon"])) : vue.createCommentVNode("v-if", true),
          renderTree.value ? (vue.openBlock(), vue.createBlock(vue.unref(index$4.ElTree), {
            key: 1,
            ref_key: "tree",
            ref: tree,
            modelValue: vue.unref(checked),
            "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => vue.isRef(checked) ? checked.value = $event : null),
            data: _ctx.data,
            "show-checkbox": "",
            "node-key": vue.unref(propsAlias).key,
            props: vue.unref(propsAlias),
            "render-content": _ctx.renderContent,
            "filter-node-method": filterNodeMethod,
            onCheck: handleCheckChange
          }, null, 8, ["modelValue", "data", "node-key", "props", "render-content"])) : vue.createCommentVNode("v-if", true)
        ], 2)
      ], 2);
    };
  }
});
var TransferLeftPanel = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "/home/lxj/workspace/element-plus/packages/components/unidirectionalTransfer/src/transfer-left-plane.vue"]]);

exports["default"] = TransferLeftPanel;
//# sourceMappingURL=transfer-left-plane.js.map
