/**
 * 字符串筛选工具函数
 */
/**
 * 筛选项多选时，UI 层用逗号拼接成单个字符串（见 FilterConditionValueInput join）。
 * 对 contains 等应按「任一命中」处理，不能整段当子串匹配。
 */
function getFilterStringParts(filterValue) {
    if (Array.isArray(filterValue)) {
        const parts = filterValue.map((v) => String(v)).filter((s) => s !== '');
        return parts.length > 1 ? parts : null;
    }
    if (typeof filterValue === 'string' && filterValue.includes(',')) {
        const parts = filterValue
            .split(',')
            .map((s) => s.trim())
            .filter((s) => s !== '');
        return parts.length > 1 ? parts : null;
    }
    return null;
}
/** 多选 + 等于：行与筛选项按逗号拆成 token 后集合须完全一致（顺序无关） */
function multiSelectEquals(rowValue, filterParts) {
    const rowTokens = String(rowValue)
        .split(',')
        .map((s) => s.trim())
        .filter((s) => s !== '');
    if (rowTokens.length !== filterParts.length)
        return false;
    const a = [...rowTokens].sort();
    const b = [...filterParts].sort();
    return a.every((t, i) => t === b[i]);
}
/**
 * 处理字符串比较
 */
export function handleStringComparison(rowValue, operator, filterValue) {
    const stringValue = String(rowValue);
    const filterStringValue = String(filterValue);
    const multiParts = getFilterStringParts(filterValue);
    if (multiParts) {
        switch (operator) {
            case 'eq':
                return multiSelectEquals(stringValue, multiParts);
            case 'ne':
                return !multiSelectEquals(stringValue, multiParts);
            case 'contains':
                return multiParts.some((p) => stringValue === p || stringValue.includes(p));
            case 'notContains':
                return !multiParts.some((p) => stringValue.includes(p));
            default:
                break;
        }
    }
    switch (operator) {
        case 'eq':
            return stringValue === filterStringValue;
        case 'ne':
            return stringValue !== filterStringValue;
        case 'gt':
            return stringValue > filterStringValue;
        case 'gte':
            return stringValue >= filterStringValue;
        case 'lt':
            return stringValue < filterStringValue;
        case 'lte':
            return stringValue <= filterStringValue;
        case 'contains':
            return stringValue.includes(filterStringValue);
        case 'notContains':
            return !stringValue.includes(filterStringValue);
        default:
            return false;
    }
}
