/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { isVNode, defineComponent, toRefs, computed, createVNode } from 'vue';
import pick from 'lodash/pick';
import isFunction from 'lodash/isFunction';
import useVModel from '../hooks/useVModel.js';
import useDefaultValue from '../hooks/useDefaultValue.js';
import { useFormDisabled } from '../form/hooks.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import props from './props.js';
import { TRANSFER_NAME, getTransferData, filterTransferData, getDataValues, getTransferListOption } from './utils.js';
import TransferOperations from './components/transfer-operations.js';
import TransferList from './components/transfer-list.js';
import 'lodash/kebabCase';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/isString';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-2e5d8948.js';
import 'dayjs';
import '../_chunks/dep-4580aabe.js';
import '../config-provider/type.js';
import 'lodash/isUndefined';
import '../utils/event.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/camelCase';
import 'lodash/isNull';
import 'lodash/isNumber';
import '@ldesign/icons-vue-next';
import '../button/index.js';
import '../utils/withInstall.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../utils/dom.js';
import '../utils/easing.js';
import '../loading/loading.js';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isObject';
import '../hooks/useTeleport.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../_common/js/utils/helper.js';
import '../loading/style';
import '../loading/type.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../hooks/tnode.js';
import '../button/props.js';
import '../button/style';
import '../button/type.js';
import '../hooks/useGlobalIcon.js';
import '../pagination/index.js';
import '../pagination/pagination.js';
import 'lodash/isNaN';
import '../input-number/index.js';
import '../input-number/input-number.js';
import '../input/index.js';
import '../input/input.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../hooks/index.js';
import '../hooks/icon.js';
import '../hooks/slot.js';
import '../hooks/useCommonClassName.js';
import '../hooks/useDestroyOnClose.js';
import '../hooks/useVirtualScroll.js';
import '../input/useLengthLimit.js';
import '../_common/js/log/index.js';
import '../_common/js/log/log.js';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../input/style';
import '../input/type.js';
import '../input-number/props.js';
import '../input-number/useInputNumber.js';
import '../_common/js/input-number/number.js';
import 'lodash/isNil';
import '../_common/js/input-number/large-number.js';
import '../input-number/style';
import '../input-number/type.js';
import '../select/index.js';
import '../select/select.js';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/intersection';
import '../common-components/fake-arrow.js';
import '../select-input/index.js';
import '../select-input/select-input.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/container.js';
import '../hooks/useResizeObserver.js';
import '../popup/props.js';
import '../popup/style';
import '../popup/type.js';
import '../select-input/props.js';
import '../select-input/useSingle.js';
import '../select-input/useMultiple.js';
import '../tag-input/index.js';
import '../tag-input/tag-input.js';
import '../tag-input/props.js';
import '../tag-input/hooks/useTagScroll.js';
import '../tag-input/useTagList.js';
import '../tag/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../tag/style';
import '../tag/type.js';
import '../tag-input/hooks/useHover.js';
import '../tag-input/hooks/useDragSorter.js';
import '../tag-input/style';
import '../tag-input/type.js';
import '../select-input/useOverlayInnerStyle.js';
import '../select-input/style';
import '../select-input/type.js';
import '../select/props.js';
import '../select/select-panel.js';
import 'lodash/omit';
import '../select/option.js';
import '../checkbox/index.js';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/constants.js';
import '../checkbox/group.js';
import '../checkbox/checkbox-group-props.js';
import '../checkbox/style';
import '../checkbox/type.js';
import '../select/option-props.js';
import '../select/helper.js';
import '../select/optionGroup.js';
import '../select/option-group-props.js';
import '../select/hooks/usePanelVirtualScroll.js';
import '../select/hooks/useSelectOptions.js';
import '../select/style';
import '../select/type.js';
import '../input-adornment/index.js';
import '../input-adornment/input-adornment.js';
import '../input-adornment/props.js';
import '../input-adornment/style';
import '../input-adornment/type.js';
import '../pagination/props.js';
import '../pagination/usePaginationClasses.js';
import '../pagination/useMoreAction.js';
import '../pagination/pagination-mini.js';
import '../pagination/pagination-mini-props.js';
import '../pagination/style';
import '../pagination/type.js';
import './components/transfer-search.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !isVNode(s);
}
var SOURCE = "source";
var TARGET = "target";
var _Transfer = defineComponent({
  name: TRANSFER_NAME,
  props: _objectSpread({}, props),
  setup: function setup(props2, _ref) {
    var slots = _ref.slots;
    var disabled = useFormDisabled();
    var classPrefix = usePrefixClass();
    var _toRefs = toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue,
      checked = _toRefs.checked;
    var _useVModel = useVModel(value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      innerValue = _useVModel2[0],
      setInnerValue = _useVModel2[1];
    var _useDefaultValue = useDefaultValue(checked, props2.defaultChecked, props2.onCheckedChange, "checked"),
      _useDefaultValue2 = _slicedToArray(_useDefaultValue, 1),
      innerChecked = _useDefaultValue2[0];
    var valueList = computed(function () {
      return innerValue.value;
    });
    var isTreeMode = computed(function () {
      var treeSlot = slots.tree;
      return isFunction(treeSlot);
    });
    var leftButtonDisabled = computed(function () {
      return props2.direction === "right";
    });
    var rightButtonDisabled = computed(function () {
      return props2.direction === "left";
    });
    var transferData = computed(function () {
      return getTransferData(props2.data, props2.keys, isTreeMode.value);
    });
    var sourceList = computed(function () {
      return filterTransferData(transferData.value, valueList.value, false, isTreeMode.value);
    });
    var targetList = computed(function () {
      return filterTransferData(transferData.value, valueList.value, true, isTreeMode.value);
    });
    var checkedValue = computed(function () {
      var _ref2;
      return _ref2 = {}, _defineProperty(_ref2, SOURCE, getDataValues(sourceList.value, innerChecked.value, {
        isTreeMode: isTreeMode.value
      })), _defineProperty(_ref2, TARGET, getDataValues(targetList.value, innerChecked.value, {
        isTreeMode: isTreeMode.value
      })), _ref2;
    });
    var hasFooter = computed(function () {
      return !!slots.footer || !!props2.footer;
    });
    var showPagination = computed(function () {
      return !!props2.pagination && !slots.content;
    });
    var showSearch = computed(function () {
      return !!props2.search;
    });
    var footerOption = computed(function () {
      var footer = props2.footer || "";
      return getTransferListOption(footer);
    });
    var emptyOption = computed(function () {
      return getTransferListOption(props2.empty);
    });
    var searchOption = computed(function () {
      return getTransferListOption(props2.search);
    });
    var checkAllOption = computed(function () {
      return getTransferListOption(props2.showCheckAll);
    });
    var disabledOption = computed(function () {
      return getTransferListOption(disabled.value);
    });
    var titleOption = computed(function () {
      return getTransferListOption(props2.title);
    });
    var paginationOption = computed(function () {
      return getTransferListOption(props2.pagination);
    });
    var handleCheckedChange = function handleCheckedChange(val, listType) {
      var _props2$onCheckedChan;
      var sourceChecked = listType === SOURCE ? val : checkedValue.value[SOURCE];
      var targetChecked = listType === TARGET ? val : checkedValue.value[TARGET];
      var checked2 = [].concat(_toConsumableArray(sourceChecked), _toConsumableArray(targetChecked));
      var event = {
        checked: checked2,
        sourceChecked: sourceChecked,
        targetChecked: targetChecked,
        type: listType
      };
      innerChecked.value = checked2;
      (_props2$onCheckedChan = props2.onCheckedChange) === null || _props2$onCheckedChan === void 0 ? void 0 : _props2$onCheckedChan.call(props2, event);
    };
    var transferTo = function transferTo(toDirection) {
      var oldTargetValue = JSON.parse(JSON.stringify(valueList.value));
      var newTargetValue;
      var selfCheckedValue = toDirection === TARGET ? checkedValue.value[SOURCE] : checkedValue.value[TARGET];
      if (toDirection === SOURCE) {
        newTargetValue = oldTargetValue.filter(function (v) {
          return !selfCheckedValue.includes(v);
        });
      } else if (props2.targetSort === "original") {
        newTargetValue = getDataValues(transferData.value, oldTargetValue.concat(selfCheckedValue), {
          isTreeMode: isTreeMode.value
        });
      } else if (props2.targetSort === "unshift") {
        newTargetValue = selfCheckedValue.concat(oldTargetValue);
      } else {
        newTargetValue = oldTargetValue.concat(selfCheckedValue);
      }
      handleCheckedChange([], toDirection === SOURCE ? TARGET : SOURCE);
      var params = {
        type: toDirection,
        movedValue: selfCheckedValue
      };
      setInnerValue(newTargetValue, params);
    };
    var transferToRight = function transferToRight() {
      transferTo(TARGET);
    };
    var transferToLeft = function transferToLeft() {
      transferTo(SOURCE);
    };
    var handleScroll = function handleScroll(e, listType) {
      var _props2$onScroll;
      var target = e.target;
      var bottomDistance = target.scrollHeight - target.scrollTop - target.clientHeight;
      var event = {
        e: e,
        bottomDistance: bottomDistance,
        type: listType
      };
      (_props2$onScroll = props2.onScroll) === null || _props2$onScroll === void 0 ? void 0 : _props2$onScroll.call(props2, event);
    };
    var handleSearch = function handleSearch(e) {
      var _props2$onSearch;
      (_props2$onSearch = props2.onSearch) === null || _props2$onSearch === void 0 ? void 0 : _props2$onSearch.call(props2, e);
    };
    var handlePageChange = function handlePageChange(pageInfo, listType) {
      var _props2$onPageChange;
      (_props2$onPageChange = props2.onPageChange) === null || _props2$onPageChange === void 0 ? void 0 : _props2$onPageChange.call(props2, pageInfo, {
        type: listType
      });
    };
    var renderTransferList = function renderTransferList(listType) {
      var scopedSlots = pick(slots, ["title", "empty", "footer", "operation", "transferItem", "default", "tree"]);
      return createVNode(TransferList, {
        "checkboxProps": props2.checkboxProps,
        "transferItem": props2.transferItem,
        "list-type": listType,
        "title": titleOption.value[listType],
        "data-source": listType === SOURCE ? sourceList.value : targetList.value,
        "checked-value": checkedValue.value[listType],
        "disabled": disabledOption.value[listType],
        "search": searchOption.value[listType],
        "pagination": paginationOption.value[listType],
        "check-all": checkAllOption.value[listType],
        "footer": footerOption.value[listType],
        "empty": emptyOption.value[listType],
        "onCheckedChange": function onCheckedChange($event) {
          return handleCheckedChange($event, listType);
        },
        "onScroll": function onScroll($event) {
          return handleScroll($event, listType);
        },
        "onSearch": handleSearch,
        "onPageChange": function onPageChange($event) {
          return handlePageChange($event, listType);
        },
        "isTreeMode": isTreeMode.value
      }, _isSlot(scopedSlots) ? scopedSlots : {
        "default": function _default() {
          return [scopedSlots];
        }
      });
    };
    return function () {
      return createVNode("div", {
        "class": ["".concat(classPrefix.value, "-transfer"), showSearch.value ? "".concat(classPrefix.value, "-transfer__search") : "", hasFooter.value ? "".concat(classPrefix.value, "-transfer__footer") : "", showPagination.value ? "".concat(classPrefix.value, "-transfer__pagination") : "", isTreeMode.value ? "".concat(classPrefix.value, "-transfer--with-tree") : ""]
      }, [renderTransferList(SOURCE), createVNode(TransferOperations, {
        "leftDisabled": disabledOption.value[TARGET] || leftButtonDisabled.value || checkedValue.value[TARGET].length === 0,
        "rightDisabled": disabledOption.value[SOURCE] || rightButtonDisabled.value || checkedValue.value[SOURCE].length === 0,
        "operation": props2.operation,
        "onMoveToRight": transferToRight,
        "onMoveToLeft": transferToLeft
      }, {
        operation: slots.operation
      }), renderTransferList(TARGET)]);
    };
  }
});

export { _Transfer as default };
//# sourceMappingURL=transfer.js.map
