'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../locale/index.cjs');
var FormItemContext = require('../../form/src/FormItemContext.cjs');
var LocaleReceiver = require('../../locale-provider/src/LocaleReceiver.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
var list = require('./list.cjs');
var operation = require('./operation.cjs');
var props = require('./props.cjs');
var statusUtils = require('../../../utils/statusUtils.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var transKeys = require('../../../utils/transKeys.cjs');
var _class = require('../../../utils/dom/class.cjs');
var en_US = require('../../../locale/lang/en_US.cjs');

var ATransfer = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ATransfer",
  inheritAttrs: false,
  props: props.transferProps(),
  slots: Object,
  // emits: ['update:targetKeys', 'update:selectedKeys', 'change', 'search', 'scroll', 'selectChange'],
  setup(props, {
    emit,
    attrs,
    slots,
    expose
  }) {
    const {
      configProvider,
      prefixCls,
      direction
    } = useConfigInject.default("transfer", props);
    const [wrapSSR, hashId] = index.default(prefixCls);
    const sourceSelectedKeys = vue.ref([]);
    const targetSelectedKeys = vue.ref([]);
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const formItemInputContext = FormItemContext.FormItemInputContext.useInject();
    const mergedStatus = vue.computed(() => statusUtils.getMergedStatus(formItemInputContext.status, props.status));
    vue.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 = props$1.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, vue.toRaw(targetSelectedKeys.value));
      } else {
        if (!props.selectedKeys)
          targetSelectedKeys.value = holder;
        emit("update:selectedKeys", [...holder, ...sourceSelectedKeys.value]);
        emit("selectChange", vue.toRaw(sourceSelectedKeys.value), holder);
      }
    };
    const moveTo = (direction2) => {
      const {
        targetKeys = [],
        dataSource = []
      } = props;
      const moveKeys = direction2 === "right" ? sourceSelectedKeys.value : targetSelectedKeys.value;
      const dataSourceDisabledKeysMap = transKeys.groupDisabledKeysMap(dataSource);
      const newMoveKeys = moveKeys.filter((key) => !dataSourceDisabledKeysMap.has(key));
      const newMoveKeysMap = transKeys.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 = vue.ref([]);
    const rightDataSource = vue.ref([]);
    vue.watchEffect(() => {
      const {
        dataSource,
        rowKey,
        targetKeys = []
      } = props;
      const ld = [];
      const rd = new Array(targetKeys.length);
      const targetKeysMap = transKeys.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 = _class.classNames(prefixCls.value, className, {
        [`${prefixCls.value}-disabled`]: disabled,
        [`${prefixCls.value}-customize-list`]: !!children,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl"
      }, statusUtils.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 vue.createVNode("div", {
        ...attrs,
        "class": cls,
        "style": style,
        "id": id
      }, [vue.createVNode(list.default, {
        "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
      }), vue.createVNode(operation.default, {
        "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), vue.createVNode(list.default, {
        "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(vue.createVNode(LocaleReceiver.default, {
      "componentName": "Transfer",
      "defaultLocale": en_US.default.Transfer,
      "children": renderTransfer
    }, null));
  }
});

exports.default = ATransfer;
//# sourceMappingURL=Transfer.cjs.map
