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

import _typeof from '@babel/runtime/helpers/typeof';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, ref, computed, watch, createVNode, mergeProps } from 'vue';
import isFunction from 'lodash/isFunction';
import SorterButton from '../sorter-button.js';
import useDefaultValue from '../../hooks/useDefaultValue.js';
import '@ldesign/icons-vue-next';
import '../../tooltip/index.js';
import '../../utils/withInstall.js';
import '../../tooltip/tooltip.js';
import 'lodash/omit';
import '../../popup/props.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import 'lodash/isObject';
import 'lodash/isString';
import '../../hooks/useConfig.js';
import '../../config-provider/useConfig.js';
import 'lodash/cloneDeep';
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 '../../_chunks/dep-c9c789a8.js';
import 'dayjs';
import '../../hooks/useVModel.js';
import 'lodash/kebabCase';
import '../../utils/dom.js';
import '../../utils/easing.js';
import '../../_common/js/utils/set-style.js';
import '../../popup/container.js';
import '../../hooks/useResizeObserver.js';
import '../../hooks/tnode.js';
import 'lodash/camelCase';
import '../../utils/render-tnode.js';
import 'lodash/isEmpty';
import '../../tooltip/util.js';
import '../../tooltip/props.js';
import '../../hooks/useGlobalIcon.js';
import './useClassName.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 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(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.js.map
