import { defineComponent, ref, computed, watch, toRaw, watchEffect, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../locale/index.mjs';
import { useInjectFormItemContext, FormItemInputContext } from '../../form/src/FormItemContext.mjs';
import LocaleReceiver from '../../locale-provider/src/LocaleReceiver.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import useStyle from '../style/index.mjs';
import List from './list.mjs';
import Operation from './operation.mjs';
import { transferProps } from './props.mjs';
import { getMergedStatus, getStatusClassNames } from '../../../utils/statusUtils.mjs';
import { getPropsSlot } from '../../../utils/vue/props.mjs';
import { groupDisabledKeysMap, groupKeysMap } from '../../../utils/transKeys.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import localeValues from '../../../locale/lang/en_US.mjs';

var ATransfer = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ATransfer",
  inheritAttrs: false,
  props: transferProps(),
  slots: Object,
  // emits: ['update:targetKeys', 'update:selectedKeys', 'change', 'search', 'scroll', 'selectChange'],
  setup(props, {
    emit,
    attrs,
    slots,
    expose
  }) {
    const {
      configProvider,
      prefixCls,
      direction
    } = useConfigInject("transfer", props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const sourceSelectedKeys = ref([]);
    const targetSelectedKeys = ref([]);
    const formItemContext = useInjectFormItemContext();
    const formItemInputContext = FormItemInputContext.useInject();
    const mergedStatus = computed(() => getMergedStatus(formItemInputContext.status, props.status));
    watch(() => props.selectedKeys, () => {
      var _a, _b;
      sourceSelectedKeys.value = ((_a = props.selectedKeys) == null ? void 0 : _a.filter((key) => !props.targetKeys.includes(key))) || [];
      targetSelectedKeys.value = ((_b = props.selectedKeys) == null ? void 0 : _b.filter((key) => props.targetKeys.includes(key))) || [];
    }, {
      immediate: true
    });
    const getLocale = (transferLocale, renderEmpty) => {
      const oldLocale = {
        notFoundContent: renderEmpty("Transfer")
      };
      const notFoundContent = getPropsSlot(slots, props, "notFoundContent");
      if (notFoundContent)
        oldLocale.notFoundContent = notFoundContent;
      if (props.searchPlaceholder !== void 0)
        oldLocale.searchPlaceholder = props.searchPlaceholder;
      return {
        ...transferLocale,
        ...oldLocale,
        ...props.locale
      };
    };
    const handleSelectChange = (direction2, holder) => {
      if (direction2 === "left") {
        if (!props.selectedKeys)
          sourceSelectedKeys.value = holder;
        emit("update:selectedKeys", [...holder, ...targetSelectedKeys.value]);
        emit("selectChange", holder, toRaw(targetSelectedKeys.value));
      } else {
        if (!props.selectedKeys)
          targetSelectedKeys.value = holder;
        emit("update:selectedKeys", [...holder, ...sourceSelectedKeys.value]);
        emit("selectChange", toRaw(sourceSelectedKeys.value), holder);
      }
    };
    const moveTo = (direction2) => {
      const {
        targetKeys = [],
        dataSource = []
      } = props;
      const moveKeys = direction2 === "right" ? sourceSelectedKeys.value : targetSelectedKeys.value;
      const dataSourceDisabledKeysMap = groupDisabledKeysMap(dataSource);
      const newMoveKeys = moveKeys.filter((key) => !dataSourceDisabledKeysMap.has(key));
      const newMoveKeysMap = groupKeysMap(newMoveKeys);
      const newTargetKeys = direction2 === "right" ? newMoveKeys.concat(targetKeys) : targetKeys.filter((targetKey) => !newMoveKeysMap.has(targetKey));
      const oppositeDirection = direction2 === "right" ? "left" : "right";
      direction2 === "right" ? sourceSelectedKeys.value = [] : targetSelectedKeys.value = [];
      emit("update:targetKeys", newTargetKeys);
      handleSelectChange(oppositeDirection, []);
      emit("change", newTargetKeys, direction2, newMoveKeys);
      formItemContext.onFieldChange();
    };
    const moveToLeft = () => {
      moveTo("left");
    };
    const moveToRight = () => {
      moveTo("right");
    };
    const onItemSelectAll = (direction2, selectedKeys) => {
      handleSelectChange(direction2, selectedKeys);
    };
    const onLeftItemSelectAll = (selectedKeys) => {
      return onItemSelectAll("left", selectedKeys);
    };
    const onRightItemSelectAll = (selectedKeys) => {
      return onItemSelectAll("right", selectedKeys);
    };
    const handleFilter = (direction2, e) => {
      const value = e.target.value;
      emit("search", direction2, value);
    };
    const handleLeftFilter = (e) => {
      handleFilter("left", e);
    };
    const handleRightFilter = (e) => {
      handleFilter("right", e);
    };
    const handleClear = (direction2) => {
      emit("search", direction2, "");
    };
    const handleLeftClear = () => {
      handleClear("left");
    };
    const handleRightClear = () => {
      handleClear("right");
    };
    const onItemSelect = (direction2, selectedKey, checked) => {
      const holder = direction2 === "left" ? [...sourceSelectedKeys.value] : [...targetSelectedKeys.value];
      const index = holder.indexOf(selectedKey);
      if (index > -1)
        holder.splice(index, 1);
      if (checked)
        holder.push(selectedKey);
      handleSelectChange(direction2, holder);
    };
    const onLeftItemSelect = (selectedKey, checked) => {
      return onItemSelect("left", selectedKey, checked);
    };
    const onRightItemSelect = (selectedKey, checked) => {
      return onItemSelect("right", selectedKey, checked);
    };
    const onRightItemRemove = (targetedKeys) => {
      const {
        targetKeys = []
      } = props;
      const newTargetKeys = targetKeys.filter((key) => !targetedKeys.includes(key));
      emit("update:targetKeys", newTargetKeys);
      emit("change", newTargetKeys, "left", [...targetedKeys]);
    };
    const handleScroll = (direction2, e) => {
      emit("scroll", direction2, e);
    };
    const handleLeftScroll = (e) => {
      handleScroll("left", e);
    };
    const handleRightScroll = (e) => {
      handleScroll("right", e);
    };
    const handleListStyle = (listStyle, direction2) => {
      if (typeof listStyle === "function")
        return listStyle({
          direction: direction2
        });
      return listStyle;
    };
    const leftDataSource = ref([]);
    const rightDataSource = ref([]);
    watchEffect(() => {
      const {
        dataSource,
        rowKey,
        targetKeys = []
      } = props;
      const ld = [];
      const rd = new Array(targetKeys.length);
      const targetKeysMap = groupKeysMap(targetKeys);
      dataSource.forEach((record) => {
        if (rowKey)
          record.key = rowKey(record);
        if (targetKeysMap.has(record.key))
          rd[targetKeysMap.get(record.key)] = record;
        else
          ld.push(record);
      });
      leftDataSource.value = ld;
      rightDataSource.value = rd;
    });
    expose({
      handleSelectChange
    });
    const renderTransfer = (transferLocale) => {
      var _a, _b, _c, _d, _e, _f;
      const {
        disabled,
        operations = [],
        showSearch,
        listStyle,
        operationStyle,
        filterOption,
        showSelectAll,
        selectAllLabels = [],
        oneWay,
        pagination,
        id = formItemContext.id.value
      } = props;
      const {
        class: className,
        style
      } = attrs;
      const children = slots.children;
      const mergedPagination = !children && pagination;
      const renderEmpty = configProvider.renderEmpty;
      const locale = getLocale(transferLocale, renderEmpty);
      const {
        footer
      } = slots;
      const renderItem = props.render || slots.render;
      const leftActive = targetSelectedKeys.value.length > 0;
      const rightActive = sourceSelectedKeys.value.length > 0;
      const cls = classNames(prefixCls.value, className, {
        [`${prefixCls.value}-disabled`]: disabled,
        [`${prefixCls.value}-customize-list`]: !!children,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl"
      }, getStatusClassNames(prefixCls.value, mergedStatus.value, formItemInputContext.hasFeedback), hashId.value);
      const titles = props.titles;
      const leftTitle = (_c = (_b = titles && titles[0]) != null ? _b : (_a = slots.leftTitle) == null ? void 0 : _a.call(slots)) != null ? _c : (locale.titles || ["", ""])[0];
      const rightTitle = (_f = (_e = titles && titles[1]) != null ? _e : (_d = slots.rightTitle) == null ? void 0 : _d.call(slots)) != null ? _f : (locale.titles || ["", ""])[1];
      return createVNode("div", {
        ...attrs,
        "class": cls,
        "style": style,
        "id": id
      }, [createVNode(List, {
        "key": "leftList",
        "prefixCls": `${prefixCls.value}-list`,
        "dataSource": leftDataSource.value,
        "filterOption": filterOption,
        "style": handleListStyle(listStyle, "left"),
        "checkedKeys": sourceSelectedKeys.value,
        "handleFilter": handleLeftFilter,
        "handleClear": handleLeftClear,
        "onItemSelect": onLeftItemSelect,
        "onItemSelectAll": onLeftItemSelectAll,
        "renderItem": renderItem,
        "showSearch": showSearch,
        "renderList": children,
        "onScroll": handleLeftScroll,
        "disabled": disabled,
        "direction": direction.value === "rtl" ? "right" : "left",
        "showSelectAll": showSelectAll,
        "selectAllLabel": selectAllLabels[0] || slots.leftSelectAllLabel,
        "pagination": mergedPagination,
        ...locale
      }, {
        titleText: () => leftTitle,
        footer
      }), createVNode(Operation, {
        "key": "operation",
        "class": `${prefixCls.value}-operation`,
        "rightActive": rightActive,
        "rightArrowText": operations[0],
        "moveToRight": moveToRight,
        "leftActive": leftActive,
        "leftArrowText": operations[1],
        "moveToLeft": moveToLeft,
        "style": operationStyle,
        "disabled": disabled,
        "direction": direction.value,
        "oneWay": oneWay
      }, null), createVNode(List, {
        "key": "rightList",
        "prefixCls": `${prefixCls.value}-list`,
        "dataSource": rightDataSource.value,
        "filterOption": filterOption,
        "style": handleListStyle(listStyle, "right"),
        "checkedKeys": targetSelectedKeys.value,
        "handleFilter": handleRightFilter,
        "handleClear": handleRightClear,
        "onItemSelect": onRightItemSelect,
        "onItemSelectAll": onRightItemSelectAll,
        "onItemRemove": onRightItemRemove,
        "renderItem": renderItem,
        "showSearch": showSearch,
        "renderList": children,
        "onScroll": handleRightScroll,
        "disabled": disabled,
        "direction": direction.value === "rtl" ? "left" : "right",
        "showSelectAll": showSelectAll,
        "selectAllLabel": selectAllLabels[1] || slots.rightSelectAllLabel,
        "showRemove": oneWay,
        "pagination": mergedPagination,
        ...locale
      }, {
        titleText: () => rightTitle,
        footer
      })]);
    };
    return () => wrapSSR(createVNode(LocaleReceiver, {
      "componentName": "Transfer",
      "defaultLocale": localeValues.Transfer,
      "children": renderTransfer
    }, null));
  }
});

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