"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sortingFns = exports.reSplitAlphaNumeric = void 0;
exports.reSplitAlphaNumeric = /([0-9]+)/gm;
const alphanumeric = (rowA, rowB, columnId) => {
    return compareAlphanumeric(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
};
const alphanumericCaseSensitive = (rowA, rowB, columnId) => {
    return compareAlphanumeric(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
};
// The text filter is more basic (less numeric support)
// but is much faster
const text = (rowA, rowB, columnId) => {
    return compareBasic(toString(rowA.getValue(columnId)).toLowerCase(), toString(rowB.getValue(columnId)).toLowerCase());
};
// The text filter is more basic (less numeric support)
// but is much faster
const textCaseSensitive = (rowA, rowB, columnId) => {
    return compareBasic(toString(rowA.getValue(columnId)), toString(rowB.getValue(columnId)));
};
const datetime = (rowA, rowB, columnId) => {
    const a = rowA.getValue(columnId);
    const b = rowB.getValue(columnId);
    // Can handle nullish values
    // Use > and < because == (and ===) doesn't work with
    // Date objects (would require calling getTime()).
    if (a > b)
        return 1;
    if (a < b)
        return -1;
    return 0;
};
const basic = (rowA, rowB, columnId) => {
    return compareBasic(rowA.getValue(columnId), rowB.getValue(columnId));
};
// Utils
function compareBasic(a, b) {
    if (a === b)
        return 0;
    // 如果两个值都是字符串，使用 localeCompare 进行本地化比较（支持中文拼音排序）
    if (typeof a === 'string' && typeof b === 'string') {
        return a.localeCompare(b, 'zh-CN', { numeric: true, sensitivity: 'base' });
    }
    // 对于其他类型，使用默认比较
    return a > b ? 1 : -1;
}
function toString(a) {
    if (typeof a === 'number') {
        if (isNaN(a) || a === Infinity || a === -Infinity) {
            return '';
        }
        return String(a);
    }
    if (typeof a === 'string') {
        return a;
    }
    return '';
}
// Mixed sorting is slow, but very inclusive of many edge cases.
// It handles numbers, mixed alphanumeric combinations, and even
// null, undefined, and Infinity
function compareAlphanumeric(aStr, bStr) {
    // Split on number groups, but keep the delimiter
    // Then remove falsey split values
    const a = aStr.split(exports.reSplitAlphaNumeric).filter(Boolean);
    const b = bStr.split(exports.reSplitAlphaNumeric).filter(Boolean);
    // While
    while (a.length && b.length) {
        const aa = a.shift();
        const bb = b.shift();
        const an = parseInt(aa, 10);
        const bn = parseInt(bb, 10);
        const combo = [an, bn].sort();
        // Both are string
        if (isNaN(combo[0])) {
            // 使用 localeCompare 进行本地化比较（支持中文拼音排序）
            const compareResult = aa.localeCompare(bb, 'zh-CN', { numeric: true, sensitivity: 'base' });
            if (compareResult !== 0) {
                return compareResult;
            }
            continue;
        }
        // One is a string, one is a number
        if (isNaN(combo[1])) {
            return isNaN(an) ? -1 : 1;
        }
        // Both are numbers
        if (an > bn) {
            return 1;
        }
        if (bn > an) {
            return -1;
        }
    }
    return a.length - b.length;
}
// Exports
exports.sortingFns = {
    alphanumeric,
    alphanumericCaseSensitive,
    text,
    textCaseSensitive,
    datetime,
    basic,
};
