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

import { _ as _typeof } from '../../_chunks/dep-b4041200.mjs';
import { _ as _toConsumableArray } from '../../_chunks/dep-e091cfd9.mjs';
import { _ as _defineProperty } from '../../_chunks/dep-eb20e23f.mjs';
import { _ as _slicedToArray } from '../../_chunks/dep-55846619.mjs';
import { toRefs, ref, computed, createVNode, mergeProps, watch } from 'vue';
import { i as isFunction_1 } from '../../_chunks/dep-6b22dd27.mjs';
import SorterButton from '../sorter-button.mjs';
import useDefaultValue from '../../hooks/useDefaultValue.mjs';
import '../../_chunks/dep-cfcedf47.mjs';
import '../../_chunks/dep-a0b4a044.mjs';
import '../../_chunks/dep-c959a6fb.mjs';
import '../../_chunks/dep-0f0c89ff.mjs';
import '@ldesign/icons-vue-next';
import '../../tooltip/index.mjs';
import '../../utils/withInstall.mjs';
import '../../tooltip/tooltip.mjs';
import '../../_chunks/dep-b2af48ba.mjs';
import '../../_chunks/dep-37ba476d.mjs';
import '../../_chunks/dep-e3b14d70.mjs';
import '../../_chunks/dep-66d6733b.mjs';
import '../../_chunks/dep-be27d184.mjs';
import '../../_chunks/dep-c31e1d9e.mjs';
import '../../_chunks/dep-bf74f1c2.mjs';
import '../../_chunks/dep-79821ff2.mjs';
import '../../_chunks/dep-0bfbf721.mjs';
import '../../_chunks/dep-39cc276d.mjs';
import '../../_chunks/dep-8f777748.mjs';
import '../../_chunks/dep-ae357f26.mjs';
import '../../_chunks/dep-352691c2.mjs';
import '../../_chunks/dep-2cfd5b3a.mjs';
import '../../_chunks/dep-05fa17c9.mjs';
import '../../_chunks/dep-a9c8a5d5.mjs';
import '../../_chunks/dep-10085c61.mjs';
import '../../_chunks/dep-1b058de3.mjs';
import '../../_chunks/dep-c852a61a.mjs';
import '../../_chunks/dep-92e7085e.mjs';
import '../../_chunks/dep-44f056cd.mjs';
import '../../_chunks/dep-23a0f453.mjs';
import '../../_chunks/dep-7acc11cb.mjs';
import '../../_chunks/dep-59c8420d.mjs';
import '../../_chunks/dep-ba3c0b2e.mjs';
import '../../_chunks/dep-774ac2d0.mjs';
import '../../_chunks/dep-3afa94cf.mjs';
import '../../popup/props.mjs';
import '../../popup/index.mjs';
import '../../popup/popup.mjs';
import '@popperjs/core';
import '../../_chunks/dep-1cf2b129.mjs';
import '../../hooks/index.mjs';
import '../../hooks/icon.mjs';
import '../../hooks/slot.mjs';
import '../../hooks/tnode.mjs';
import '../../_chunks/dep-f6649a46.mjs';
import '../../_chunks/dep-1fe0ad42.mjs';
import '../../_chunks/dep-d54f2690.mjs';
import '../../_chunks/dep-74848b92.mjs';
import '../../_chunks/dep-c4e1a112.mjs';
import '../../utils/render-tnode.mjs';
import '../../_chunks/dep-bd03960e.mjs';
import '../../hooks/useConfig.mjs';
import '../../config-provider/useConfig.mjs';
import '../../_chunks/dep-44417261.mjs';
import '../../_chunks/dep-99920870.mjs';
import '../../_chunks/dep-c7e9ce16.mjs';
import '../../_chunks/dep-ee57d235.mjs';
import '../../_common/js/global-config/default-config.mjs';
import '../../_common/js/global-config/locale/zh_CN.mjs';
import '../../_chunks/dep-6b4aa943.mjs';
import '../../_chunks/dep-d52291a9.mjs';
import '../../config-provider/type.mjs';
import '../../hooks/useCommonClassName.mjs';
import '../../hooks/useDestroyOnClose.mjs';
import '../../hooks/useKeepAnimation.mjs';
import '../../hooks/useRipple.mjs';
import '../../utils/set-style.mjs';
import '../../hooks/useVirtualScroll.mjs';
import '../../hooks/useVModel.mjs';
import '../../utils/dom.mjs';
import '../../utils/easing.mjs';
import '../../_common/js/utils/set-style.mjs';
import '../../popup/container.mjs';
import '../../hooks/useResizeObserver.mjs';
import '../../popup/style/css.mjs';
import '../../popup/type.mjs';
import '../../tooltip/util.mjs';
import '../../tooltip/props.mjs';
import '../../tooltip/style/css.mjs';
import '../../tooltip/type.mjs';
import '../../hooks/useGlobalIcon.mjs';
import './useClassName.mjs';

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 useSorter(props, _ref) {
  var slots = _ref.slots;
  var _toRefs = toRefs(props),
    sort = _toRefs.sort,
    data = _toRefs.data;
  var originalData = ref();
  var _useDefaultValue = useDefaultValue(sort, props.defaultSort, props.onSortChange, "sort"),
    _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
    tSortInfo = _useDefaultValue2[0],
    setTSortInfo = _useDefaultValue2[1];
  var _useDefaultValue3 = useDefaultValue(data, [], props.onDataChange, "data"),
    _useDefaultValue4 = _slicedToArray(_useDefaultValue3, 2),
    tData = _useDefaultValue4[0],
    setTData = _useDefaultValue4[1];
  var sorterFuncMap = computed(function () {
    return getSorterFuncMap(props.columns);
  });
  var innerSort = ref();
  var sortArray = computed(function () {
    var sort2 = tSortInfo.value;
    if (!sort2) return [];
    return Array.isArray(sort2) ? sort2 : [sort2];
  });
  var sortMap = computed(function () {
    var sortMap2 = {};
    sortArray.value.forEach(function (info, index) {
      var sortBy = info.sortBy;
      sortMap2[sortBy] = _objectSpread({
        index: index
      }, info);
    });
    return sortMap2;
  });
  function getSorterFuncMap(columns) {
    var map = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    for (var i = 0, len = columns.length; i < len; i++) {
      var _col$children;
      var col = columns[i];
      if (isFunction_1(col.sorter)) map[col.colKey] = col.sorter;
      if ((_col$children = col.children) !== null && _col$children !== void 0 && _col$children.length) getSorterFuncMap(col.children, map);
    }
    return map;
  }
  function handleDataSort(sortInfo) {
    var sort2 = sortInfo;
    if (!Object.keys(sorterFuncMap.value).length) return;
    if (!originalData.value) originalData.value = tData.value;
    var isEmptyArraySort = !sort2 || Array.isArray(sort2) && !sort2.length;
    var isEmptyObjectSort = !Array.isArray(sort2) && !(sort2 !== null && sort2 !== void 0 && sort2.sortBy);
    if (isEmptyArraySort || isEmptyObjectSort) {
      setTData(originalData.value, {
        trigger: "sort"
      });
      return originalData.value;
    }
    var formatedSort = Array.isArray(sort2) ? sort2 : [sort2];
    var newData = tData.value.slice().sort(function (a, b) {
      var sortResult = 0;
      for (var i = 0, len = formatedSort.length; i < len; i++) {
        var item = formatedSort[i];
        var sortFunc = sorterFuncMap.value[item.sortBy];
        if (sortResult === 0 && sortFunc) sortResult = item.descending ? sortFunc(b, a) : sortFunc(a, b);else break;
      }
      return sortResult;
    });
    if (JSON.stringify(newData) === JSON.stringify(tData.value)) return;
    setTData(newData, {
      trigger: "sort"
    });
    return newData;
  }
  function handleSortHeaderClick(col, p) {
    var _props$onChange;
    var sortInfo;
    if (props.multipleSort) {
      sortInfo = getMultipleNextSort(col, p);
    } else {
      var sort2 = Array.isArray(tSortInfo.value) ? tSortInfo.value[0] : tSortInfo.value;
      sortInfo = getSingleNextSort(col, sort2, p);
    }
    var newData = handleDataSort(sortInfo);
    var currentData = newData || tData.value;
    var currentDataSource = currentData;
    setTSortInfo(sortInfo, {
      currentDataSource: currentDataSource,
      col: col
    });
    (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, {
      sorter: sortInfo
    }, {
      currentData: currentData,
      trigger: "sorter"
    });
    innerSort.value = sortInfo;
  }
  function getSortOrder(descending) {
    if (descending === void 0) return;
    return descending ? "desc" : "asc";
  }
  function getSingleNextSort(col, sortInfo, p) {
    if (sortInfo && sortInfo.sortBy === col.colKey && sortInfo.descending === p.descending) return void 0;
    return {
      sortBy: col.colKey,
      descending: p.descending
    };
  }
  function getMultipleNextSort(col, p) {
    var sort2 = tSortInfo.value;
    if (!Array.isArray(sort2)) return;
    var colKey = col.colKey;
    var result = _toConsumableArray(sort2);
    for (var i = 0, len = sort2.length; i < len; i++) {
      if (sort2[i].sortBy === colKey) {
        var next = getSingleNextSort(col, sort2[i], p);
        next ? result[i] = next : result.splice(i, 1);
        return result;
      }
    }
    result.push({
      sortBy: colKey,
      descending: p.descending
    });
    return result;
  }
  function renderSortIcon(_ref2) {
    var _sortMap$value$col$co;
    var col = _ref2.col;
    if (!col.sorter) return null;
    var sorterButtonsProps = {
      sortType: col.sortType,
      sortOrder: getSortOrder((_sortMap$value$col$co = sortMap.value[col.colKey]) === null || _sortMap$value$col$co === void 0 ? void 0 : _sortMap$value$col$co.descending),
      sortIcon: props.sortIcon,
      hideSortTips: props.hideSortTips
    };
    return createVNode(SorterButton, mergeProps(sorterButtonsProps, {
      "onSortIconClick": function onSortIconClick(_, p) {
        return handleSortHeaderClick(col, p);
      }
    }), {
      sortIcon: slots.sortIcon
    });
  }
  var isSortInfoSame = function isSortInfoSame(a, b) {
    var tmpSortInfo = Array.isArray(a) ? a : [a];
    var tmpInnerSortInfo = Array.isArray(b) ? b : [b];
    if (tmpSortInfo.length && !b) return false;
    var _loop = function _loop() {
      var item = tmpSortInfo[i];
      var result = tmpInnerSortInfo.find(function (t) {
        return t.sortBy === item.sortBy;
      });
      if (!result) return {
        v: false
      };
      return {
        v: item.descending === result.descending
      };
    };
    for (var i = 0, len = tmpSortInfo.length; i < len; i++) {
      var _ret = _loop();
      if (_typeof(_ret) === "object") return _ret.v;
    }
  };
  watch(tSortInfo, function () {
    if (!tSortInfo.value) return;
    if (!isSortInfoSame(tSortInfo.value, innerSort.value)) handleDataSort(tSortInfo.value);
  }, {
    immediate: true
  });
  return {
    renderSortIcon: renderSortIcon
  };
}

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