import { defineComponent, ref, computed, watch, onMounted, resolveComponent, openBlock, createElementBlock, normalizeClass, unref, createVNode, createElementVNode, renderSlot, toDisplayString, Fragment, renderList, withCtx } from 'vue';
import { isEqual } from 'lodash';
import '../../../hooks/index.mjs';
import { Close } from '@element-plus/icons-vue';
import { UnidirectionalTransferProps, UnidirectionalTransferEmits, DEFAULT_PROPS } from './transfer.mjs';
import TransferLeftPanel from './transfer-left-plane.mjs';
import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs';
import { useLocale } from '../../../hooks/use-locale/index.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';

const __default__ = defineComponent({
  name: "ElUnidirectionalTransfer"
});
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: UnidirectionalTransferProps,
  emits: UnidirectionalTransferEmits,
  setup(__props, { expose, emit }) {
    const props = __props;
    const leftPanel = ref();
    const { t } = useLocale();
    const ns = useNamespace("unidirectionalTransfer");
    const propsValueMap = computed(() => {
      return Object.assign({}, DEFAULT_PROPS, props.props);
    });
    let checkedData = {};
    const getCheckableData = (data) => {
      data.forEach((transferDataItem) => {
        if (!transferDataItem[propsValueMap.value.children] || !transferDataItem[propsValueMap.value.children].length) {
          if (!transferDataItem[propsValueMap.value.disabled]) {
            checkedData[transferDataItem[propsValueMap.value.key]] = transferDataItem;
          }
        } else {
          getCheckableData(transferDataItem[propsValueMap.value.children]);
        }
      });
      return checkedData;
    };
    const checkableData = computed(() => {
      checkedData = {};
      return getCheckableData(props.data);
    });
    const removeChecked = (key) => {
      var _a, _b;
      if (!((_a = leftPanel.value) == null ? void 0 : _a.getNodeData(key))) {
        update(props.modelValue.filter((item) => item !== key));
        return;
      }
      (_b = leftPanel.value) == null ? void 0 : _b.removeChecked(key);
    };
    const panelFilterPlaceholder = computed(() => props.filterPlaceholder || t("el.transfer.filterPlaceholder"));
    let emitValue = [...props.modelValue];
    let firstCheck = false;
    const initModelValue = [...props.modelValue];
    function update(key) {
      if (!firstCheck) {
        const nonexistent = Array.from(/* @__PURE__ */ new Set([...initModelValue, ...props.modelValue])).filter((item) => !key.includes(item));
        emit("change-nonexistent", nonexistent);
      }
      firstCheck = true;
      emitValue = key;
      emit("update:modelValue", key);
      emit("change", key);
    }
    watch(() => props.data, () => {
      firstCheck = false;
    });
    onMounted(() => {
      var _a;
      props.modelValue && leftPanel.value && ((_a = leftPanel.value) == null ? void 0 : _a.setChecked(props.modelValue));
    });
    watch(() => props.modelValue, (keys, oldValue) => {
      var _a;
      if (!isEqual(keys, oldValue) && !isEqual(keys, emitValue)) {
        (_a = leftPanel.value) == null ? void 0 : _a.setChecked(keys);
      }
    });
    function setChecked(key) {
      var _a;
      (_a = leftPanel.value) == null ? void 0 : _a.setChecked(key);
    }
    expose({
      setChecked
    });
    return (_ctx, _cache) => {
      const _component_el_icon = resolveComponent("el-icon");
      return openBlock(), createElementBlock("div", {
        class: normalizeClass(unref(ns).b())
      }, [
        createVNode(TransferLeftPanel, {
          ref_key: "leftPanel",
          ref: leftPanel,
          data: props.data,
          placeholder: unref(panelFilterPlaceholder),
          filterable: _ctx.filterable,
          "filter-method": _ctx.filterMethod,
          props: props.props,
          "render-content": props.renderContent,
          "model-value": props.modelValue,
          onChange: update
        }, null, 8, ["data", "placeholder", "filterable", "filter-method", "props", "render-content", "model-value"]),
        createElementVNode("div", {
          class: normalizeClass(unref(ns).b("panel"))
        }, [
          createElementVNode("p", {
            class: normalizeClass(unref(ns).be("panel", "header"))
          }, [
            renderSlot(_ctx.$slots, "searchContent", {
              length: _ctx.modelValue.length
            }, () => [
              createElementVNode("span", null, toDisplayString(unref(t)("el.transfer.hasChecked")) + "\uFF08" + toDisplayString(_ctx.modelValue.length) + "\uFF09", 1)
            ])
          ], 2),
          createElementVNode("div", {
            class: normalizeClass(unref(ns).be("panel", "body"))
          }, [
            createElementVNode("div", {
              class: normalizeClass(unref(ns).be("panel", "container"))
            }, [
              (openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.modelValue, (item, index) => {
                return openBlock(), createElementBlock("div", {
                  key: index,
                  class: normalizeClass(unref(ns).be("panel", "list"))
                }, [
                  renderSlot(_ctx.$slots, "reftPanelContent", {
                    key: item,
                    data: unref(checkableData)[item]
                  }, () => {
                    var _a;
                    return [
                      createElementVNode("div", null, toDisplayString(((_a = unref(checkableData)[item]) == null ? void 0 : _a[unref(propsValueMap).label]) || ""), 1)
                    ];
                  }),
                  createVNode(_component_el_icon, {
                    size: "12",
                    onClick: ($event) => removeChecked(item)
                  }, {
                    default: withCtx(() => [
                      createVNode(unref(Close))
                    ]),
                    _: 2
                  }, 1032, ["onClick"])
                ], 2);
              }), 128))
            ], 2)
          ], 2)
        ], 2)
      ], 2);
    };
  }
});
var Transfer = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/kimihiro/workfiles/dingdandao/element-plus/packages/components/unidirectionalTransfer/src/transfer.vue"]]);

export { Transfer as default };
//# sourceMappingURL=transfer2.mjs.map
