/**
 * Cell categorizer — copied from client/shared/src/lib/platform/utils/cell-categorizer.ts
 * with imports adjusted for local types.
 */
import { isExcelError } from './types.js';
/** Categories of quality issues that can be flagged on a mutated cell. */
export var IssueType;
(function (IssueType) {
    IssueType["UNTAGGED_HARDCODED_NUMBER"] = "hardcodes";
    IssueType["HARDCODED_NUMBER_IN_FORMULA"] = "hardcodes in formulas";
    IssueType["LARGE_PERCENTAGE"] = "large %s";
    IssueType["INVALID_FORMULA"] = "formula errs";
})(IssueType || (IssueType = {}));
/** Issues that require MUST FIX prefix */
export const MUST_FIX_ISSUES = new Set([IssueType.INVALID_FORMULA]);
function isHardcodedNumber(cell) {
    if (cell.formula)
        return false;
    const value = cell.value;
    if (typeof value !== 'number')
        return false;
    const format = cell.numberFormat?.toLowerCase() || '';
    if (format.includes('d') || format.includes('m') || format.includes('y'))
        return false;
    if (format.includes('h') || format.includes('s'))
        return false;
    if (format.includes('$') || format.includes('¥') || format.includes('€'))
        return false;
    return true;
}
/** Standard constants that are acceptable inside formulas. */
const FORMULA_CONSTANT_ALLOWLIST = new Set([0, 1, 2, 10, 12, 100, 360, 365]);
function hasHardcodedNumbersInFormula(formula) {
    const hardcodedNumberPattern = /(?<![A-Z$!])(?<![A-Z]\d*)\b(\d+\.?\d*)\b(?![A-Z])/gi;
    const formulaWithoutStrings = formula.replace(/"[^"]*"/g, '');
    let match;
    while ((match = hardcodedNumberPattern.exec(formulaWithoutStrings)) !== null) {
        const num = parseFloat(match[1]);
        if (!FORMULA_CONSTANT_ALLOWLIST.has(num))
            return true;
    }
    return false;
}
function hasFormulaError(cell) {
    return isExcelError(cell.value);
}
function isLargePercentage(cell) {
    const format = cell.numberFormat || '';
    if (!format.includes('%'))
        return false;
    if (cell.formula)
        return false;
    const value = cell.value;
    if (typeof value !== 'number')
        return false;
    return Math.abs(value) >= 1;
}
/** Detect all quality issues on a single cell edit. */
export function categorizeCellIssues(cell) {
    const issues = [];
    if (isLargePercentage(cell))
        issues.push(IssueType.LARGE_PERCENTAGE);
    if (isHardcodedNumber(cell))
        issues.push(IssueType.UNTAGGED_HARDCODED_NUMBER);
    if (cell.formula && hasHardcodedNumbersInFormula(cell.formula))
        issues.push(IssueType.HARDCODED_NUMBER_IN_FORMULA);
    if (hasFormulaError(cell))
        issues.push(IssueType.INVALID_FORMULA);
    return { cell, issues };
}
/** Partition a list of cell edits into good cells and per-issue-type buckets. */
export function categorizeCells(cells) {
    const goodCells = [];
    const largePercentages = [];
    const hardcodedNumbers = [];
    const hardcodedNumbersInFormulas = [];
    const invalidFormulas = [];
    const allProblemCells = [];
    for (const cell of cells) {
        const categorized = categorizeCellIssues(cell);
        if (categorized.issues.length === 0) {
            goodCells.push(cell);
        }
        else {
            allProblemCells.push(categorized);
            if (categorized.issues.includes(IssueType.LARGE_PERCENTAGE))
                largePercentages.push(categorized);
            if (categorized.issues.includes(IssueType.UNTAGGED_HARDCODED_NUMBER))
                hardcodedNumbers.push(categorized);
            if (categorized.issues.includes(IssueType.HARDCODED_NUMBER_IN_FORMULA))
                hardcodedNumbersInFormulas.push(categorized);
            if (categorized.issues.includes(IssueType.INVALID_FORMULA))
                invalidFormulas.push(categorized);
        }
    }
    return {
        goodCells,
        largePercentages,
        hardcodedNumbers,
        hardcodedNumbersInFormulas,
        invalidFormulas,
        allProblemCells
    };
}
//# sourceMappingURL=cell-categorizer.js.map