/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../../chunks/helper.js';
import { toRefs, ref, computed } from '@vue/composition-api';
import isFunction from 'lodash/isFunction';
import SorterButton from '../core/sorter-button.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import '../../../chunks/_commonjsHelpers.js';
import '@ldesign/icons-vue';
import '../../tooltip/index.js';
import '../../../utils/map-props.js';
import 'vue';
import '../../../utils/with-install.js';
import 'lodash';
import '../../../config.js';
import '../../tooltip/tooltip.js';
import '../../popup/props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../chunks/set-style.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '../../../utils/helper.js';
import 'lodash/camelCase';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/mixins.js';
import '../../popup/container.js';
import '../../tooltip/props.js';
import '../../tooltip/style/css.js';
import '../../tooltip/type.js';
import '../../config-provider/useConfig.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/kebabCase';
import '../../../hooks/render-lnode.js';
import '../../../hooks/use-global-icon.js';
import '../../../hooks/use-config.js';
import './useClassName.js';

function useSorter(props, _ref) {
  let {
    emit,
    slots
  } = _ref;
  const {
    sort,
    data
  } = toRefs(props);
  const originalData = ref();
  const [tSortInfo, setTSortInfo] = useDefaultValue(sort, props.defaultSort, props.onSortChange, "sort", "sort-change");
  const [tData, setTData] = useDefaultValue(data, [], props.onDataChange, "data", "data-change");
  const sorterFuncMap = computed(() => getSorterFuncMap(props.columns));
  const sortArray = computed(() => {
    const sort2 = tSortInfo.value;
    if (!sort2) return [];
    return Array.isArray(sort2) ? sort2 : [sort2];
  });
  const sortMap = computed(() => {
    const sortMap2 = {};
    sortArray.value.forEach((info, index) => {
      const {
        sortBy
      } = info;
      sortMap2[sortBy] = {
        index,
        ...info
      };
    });
    return sortMap2;
  });
  function getSorterFuncMap(columns) {
    let map = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    for (let i = 0, len = columns.length; i < len; i++) {
      var _col$children;
      const 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) {
    const sort2 = sortInfo;
    if (!Object.keys(sorterFuncMap.value).length) return;
    if (!originalData.value) originalData.value = tData.value;
    const isEmptyArraySort = !sort2 || Array.isArray(sort2) && !sort2.length;
    const isEmptyObjectSort = !Array.isArray(sort2) && !(sort2 !== null && sort2 !== void 0 && sort2.sortBy);
    if (isEmptyArraySort || isEmptyObjectSort) {
      setTData(originalData.value, {
        trigger: "sort"
      });
      return originalData.value;
    }
    const formattedSort = Array.isArray(sort2) ? sort2 : [sort2];
    const newData = tData.value.slice().sort((a, b) => {
      let sortResult = 0;
      for (let i = 0, len = formattedSort.length; i < len; i++) {
        const item = formattedSort[i];
        const 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;
    let sortInfo;
    if (props.multipleSort) {
      sortInfo = getMultipleNextSort(col, p);
    } else {
      const sort2 = Array.isArray(tSortInfo.value) ? tSortInfo.value[0] : tSortInfo.value;
      sortInfo = getSingleNextSort(col, sort2, p);
    }
    const newData = handleDataSort(sortInfo);
    const currentData = newData || tData.value;
    const currentDataSource = currentData;
    setTSortInfo(sortInfo, {
      currentDataSource,
      col
    });
    (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, {
      sorter: sortInfo
    }, {
      currentData,
      trigger: "sorter"
    });
    emit("change", {
      sorter: sortInfo
    }, {
      currentData,
      col,
      trigger: "sorter"
    });
  }
  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) {
    const sort2 = tSortInfo.value;
    if (!Array.isArray(sort2)) return;
    const {
      colKey
    } = col;
    const result = [...sort2];
    for (let i = 0, len = sort2.length; i < len; i++) {
      if (sort2[i].sortBy === colKey) {
        const 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(h, _ref2) {
    var _sortMap$value$col$co;
    let {
      col
    } = _ref2;
    if (!col.sorter) return null;
    const 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 h(SorterButton, helper([{
      "scopedSlots": {
        sortIcon: slots.sortIcon
      }
    }, {
      "props": sorterButtonsProps
    }, {}, {
      "on": {
        "sort-icon-click": (_, p) => handleSortHeaderClick(col, p)
      }
    }]));
  }
  return {
    renderSortIcon
  };
}

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