'use strict';

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

var vue = require('vue');
var iconsVue = require('@ant-design/icons-vue');
require('../../../utils/index.cjs');
var index$2 = require('../../dropdown/index.cjs');
var index$1 = require('../../menu/index.cjs');
var index = require('../../checkbox/index.cjs');
var ListBody = require('./ListBody.cjs');
var search = require('./search.cjs');
var props = require('./props.cjs');
var is = require('../../../utils/is.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var transKeys = require('../../../utils/transKeys.cjs');
var _class = require('../../../utils/dom/class.cjs');

const defaultRender = () => null;
function isRenderResultPlainObject(result) {
  return !!(result && !is.isValidElement(result) && Object.prototype.toString.call(result) === "[object Object]");
}
function getEnabledItemKeys(items) {
  return items.filter((data) => !data.disabled).map((data) => data.key);
}
var List = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "TransferList",
  inheritAttrs: false,
  props: props.transferListProps,
  // emits: ['scroll', 'itemSelectAll', 'itemRemove', 'itemSelect'],
  slots: Object,
  setup(props, {
    attrs,
    slots
  }) {
    const filterValue = vue.ref("");
    const transferNode = vue.ref();
    const defaultListBodyRef = vue.ref();
    const renderListBody = (renderList, props2) => {
      let bodyContent = renderList ? renderList(props2) : null;
      const customize = !!bodyContent && props$1.filterEmpty(bodyContent).length > 0;
      if (!customize)
        bodyContent = vue.createVNode(ListBody.default, {
          ...props2,
          "ref": defaultListBodyRef
        }, null);
      return {
        customize,
        bodyContent
      };
    };
    const renderItemHtml = (item) => {
      const {
        renderItem = defaultRender
      } = props;
      const renderResult = renderItem(item);
      const isRenderResultPlain = isRenderResultPlainObject(renderResult);
      return {
        renderedText: isRenderResultPlain ? renderResult.value : renderResult,
        renderedEl: isRenderResultPlain ? renderResult.label : renderResult,
        item
      };
    };
    const filteredItems = vue.ref([]);
    const filteredRenderItems = vue.ref([]);
    const matchFilter = (text, item) => {
      const {
        filterOption
      } = props;
      if (filterOption)
        return filterOption(filterValue.value, item);
      return text.includes(filterValue.value);
    };
    vue.watchEffect(() => {
      const fItems = [];
      const fRenderItems = [];
      props.dataSource.forEach((item) => {
        const renderedItem = renderItemHtml(item);
        const {
          renderedText
        } = renderedItem;
        if (filterValue.value && filterValue.value.trim() && !matchFilter(renderedText, item))
          return null;
        fItems.push(item);
        fRenderItems.push(renderedItem);
      });
      filteredItems.value = fItems;
      filteredRenderItems.value = fRenderItems;
    });
    const checkStatus = vue.computed(() => {
      const {
        checkedKeys
      } = props;
      if (checkedKeys.length === 0)
        return "none";
      const checkedKeysMap = transKeys.groupKeysMap(checkedKeys);
      if (filteredItems.value.every((item) => checkedKeysMap.has(item.key) || !!item.disabled))
        return "all";
      return "part";
    });
    const enabledItemKeys = vue.computed(() => {
      return getEnabledItemKeys(filteredItems.value);
    });
    const getNewSelectKeys = (keys, unCheckedKeys) => {
      return Array.from(/* @__PURE__ */ new Set([...keys, ...props.checkedKeys])).filter((key) => !unCheckedKeys.includes(key));
    };
    const getCheckBox = ({
      disabled,
      prefixCls
    }) => {
      var _a;
      const checkedAll = checkStatus.value === "all";
      const checkAllCheckbox = vue.createVNode(index.Checkbox, {
        "disabled": ((_a = props.dataSource) == null ? void 0 : _a.length) === 0 || disabled,
        "checked": checkedAll,
        "indeterminate": checkStatus.value === "part",
        "class": `${prefixCls}-checkbox`,
        "onChange": () => {
          const keys = enabledItemKeys.value;
          props.onItemSelectAll(getNewSelectKeys(!checkedAll ? keys : [], checkedAll ? props.checkedKeys : []));
        }
      }, null);
      return checkAllCheckbox;
    };
    const handleFilter = (e) => {
      var _a;
      const {
        target: {
          value: filter
        }
      } = e;
      filterValue.value = filter;
      (_a = props.handleFilter) == null ? void 0 : _a.call(props, e);
    };
    const handleClear = (e) => {
      var _a;
      filterValue.value = "";
      (_a = props.handleClear) == null ? void 0 : _a.call(props, e);
    };
    const getSelectAllLabel = (selectedCount, totalCount) => {
      const {
        itemsUnit,
        itemUnit,
        selectAllLabel
      } = props;
      if (selectAllLabel) {
        return typeof selectAllLabel === "function" ? selectAllLabel({
          selectedCount,
          totalCount
        }) : selectAllLabel;
      }
      const unit = totalCount > 1 ? itemsUnit : itemUnit;
      return vue.createVNode(vue.Fragment, null, [(selectedCount > 0 ? `${selectedCount}/` : "") + totalCount, " ", unit]);
    };
    const notFoundContentEle = vue.computed(() => Array.isArray(props.notFoundContent) ? props.notFoundContent[props.direction === "left" ? 0 : 1] : props.notFoundContent);
    const getListBody = (prefixCls, searchPlaceholder, checkedKeys, renderList, showSearch, disabled) => {
      const search$1 = showSearch ? vue.createVNode("div", {
        "class": `${prefixCls}-body-search-wrapper`
      }, [vue.createVNode(search.default, {
        "prefixCls": `${prefixCls}-search`,
        "onChange": handleFilter,
        "handleClear": handleClear,
        "placeholder": searchPlaceholder,
        "value": filterValue.value,
        "disabled": disabled
      }, null)]) : null;
      let bodyNode;
      const {
        onEvents
      } = props$1.splitAttrs(attrs);
      const {
        bodyContent,
        customize
      } = renderListBody(renderList, {
        ...props,
        filteredItems: filteredItems.value,
        filteredRenderItems: filteredRenderItems.value,
        selectedKeys: checkedKeys,
        ...onEvents
      });
      if (customize) {
        bodyNode = vue.createVNode("div", {
          "class": `${prefixCls}-body-customize-wrapper`
        }, [bodyContent]);
      } else {
        bodyNode = filteredItems.value.length ? bodyContent : vue.createVNode("div", {
          "class": `${prefixCls}-body-not-found`
        }, [notFoundContentEle.value]);
      }
      return vue.createVNode("div", {
        "class": showSearch ? `${prefixCls}-body ${prefixCls}-body-with-search` : `${prefixCls}-body`,
        "ref": transferNode
      }, [search$1, bodyNode]);
    };
    return () => {
      var _a, _b;
      const {
        prefixCls,
        checkedKeys,
        disabled,
        showSearch,
        searchPlaceholder,
        selectAll,
        selectCurrent,
        selectInvert,
        removeAll,
        removeCurrent,
        renderList,
        onItemSelectAll,
        onItemRemove,
        showSelectAll = true,
        showRemove,
        pagination
      } = props;
      const footerDom = (_a = slots.footer) == null ? void 0 : _a.call(slots, {
        ...props
      });
      const listCls = _class.classNames(prefixCls, {
        [`${prefixCls}-with-pagination`]: !!pagination,
        [`${prefixCls}-with-footer`]: !!footerDom
      });
      const listBody = getListBody(prefixCls, searchPlaceholder, checkedKeys, renderList, showSearch, disabled);
      const listFooter = footerDom ? vue.createVNode("div", {
        "class": `${prefixCls}-footer`
      }, [footerDom]) : null;
      const checkAllCheckbox = !showRemove && !pagination && getCheckBox({
        disabled,
        prefixCls
      });
      let menu = null;
      if (showRemove) {
        menu = vue.createVNode(index$1.Menu, null, {
          default: () => [pagination && vue.createVNode(index$1.Menu.Item, {
            "key": "removeCurrent",
            "onClick": () => {
              const pageKeys = getEnabledItemKeys((defaultListBodyRef.value.items || []).map((entity) => entity.item));
              onItemRemove == null ? void 0 : onItemRemove(pageKeys);
            }
          }, {
            default: () => [removeCurrent]
          }), vue.createVNode(index$1.Menu.Item, {
            "key": "removeAll",
            "onClick": () => {
              onItemRemove == null ? void 0 : onItemRemove(enabledItemKeys.value);
            }
          }, {
            default: () => [removeAll]
          })]
        });
      } else {
        menu = vue.createVNode(index$1.Menu, null, {
          default: () => [vue.createVNode(index$1.Menu.Item, {
            "key": "selectAll",
            "onClick": () => {
              const keys = enabledItemKeys.value;
              onItemSelectAll(getNewSelectKeys(keys, []));
            }
          }, {
            default: () => [selectAll]
          }), pagination && vue.createVNode(index$1.Menu.Item, {
            "onClick": () => {
              const pageKeys = getEnabledItemKeys((defaultListBodyRef.value.items || []).map((entity) => entity.item));
              onItemSelectAll(getNewSelectKeys(pageKeys, []));
            }
          }, {
            default: () => [selectCurrent]
          }), vue.createVNode(index$1.Menu.Item, {
            "key": "selectInvert",
            "onClick": () => {
              let availableKeys;
              if (pagination) {
                availableKeys = getEnabledItemKeys((defaultListBodyRef.value.items || []).map((entity) => entity.item));
              } else {
                availableKeys = enabledItemKeys.value;
              }
              const checkedKeySet = new Set(checkedKeys);
              const newCheckedKeys = [];
              const newUnCheckedKeys = [];
              availableKeys.forEach((key) => {
                if (checkedKeySet.has(key))
                  newUnCheckedKeys.push(key);
                else
                  newCheckedKeys.push(key);
              });
              onItemSelectAll(getNewSelectKeys(newCheckedKeys, newUnCheckedKeys));
            }
          }, {
            default: () => [selectInvert]
          })]
        });
      }
      const dropdown = vue.createVNode(index$2.Dropdown, {
        "class": `${prefixCls}-header-dropdown`,
        "overlay": menu,
        "disabled": disabled
      }, {
        default: () => [vue.createVNode(iconsVue.DownOutlined, null, null)]
      });
      return vue.createVNode("div", {
        "class": listCls,
        "style": attrs.style
      }, [vue.createVNode("div", {
        "class": `${prefixCls}-header`
      }, [showSelectAll ? vue.createVNode(vue.Fragment, null, [checkAllCheckbox, dropdown]) : null, vue.createVNode("span", {
        "class": `${prefixCls}-header-selected`
      }, [vue.createVNode("span", null, [getSelectAllLabel(checkedKeys.length, filteredItems.value.length)]), vue.createVNode("span", {
        "class": `${prefixCls}-header-title`
      }, [(_b = slots.titleText) == null ? void 0 : _b.call(slots)])])]), listBody, listFooter]);
    };
  }
});

exports.default = List;
//# sourceMappingURL=list.cjs.map
