/**
 * setIBTextColors — apply investment banking model text color conventions.
 *
 * Ported from client/shared/src/lib/spreadsheet/spreadjs/sheet/delegates/styling.ts.
 *
 * Color rules:
 * - Blue (#0000FF): hard-coded numeric values (inputs/assumptions)
 * - Black (#000000): formulas producing numbers
 * - Green (#008000): formulas referencing other sheets (contain "!")
 *
 * Runs entirely on top of the SDK's Worksheet API — no SpreadJS dependency.
 */
import { parseRange, toExcelAddress } from './address-utils.js';
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Apply IB model text color conventions to a range of cells.
 *
 * Only applies to cells containing numeric values or formulas that produce numbers.
 *
 * @param ws - The worksheet to modify.
 * @param range - A1-style range (e.g. "A1:D50").
 * @param options - Optional ignored constants and custom colors.
 */
export async function setIBTextColors(ws, range, options = {}) {
    const { ignoredConstants = [], colors = {} } = options;
    const BLUE = colors.inputColor ?? '#0000FF';
    const BLACK = colors.formulaColor ?? '#000000';
    const GREEN = colors.crossSheetColor ?? '#008000';
    const { startRow, startCol, endRow, endCol } = parseRange(range);
    // Read the range data in one call — CellData[][] has .value and .formula
    const rangeAddr = `${toExcelAddress(startRow, startCol)}:${toExcelAddress(endRow, endCol)}`;
    const data = await ws.getRange(rangeAddr);
    // Collect cells by color to minimize format calls
    const blue = [];
    const black = [];
    const green = [];
    for (let r = 0; r < data.length; r++) {
        for (let c = 0; c < (data[r]?.length ?? 0); c++) {
            const cell = data[r][c];
            if (!cell)
                continue;
            const value = cell.value;
            const formula = cell.formula;
            // Skip non-numeric cells without formulas
            if (!formula && (typeof value !== 'number' || isNaN(value)))
                continue;
            // Skip ignored constants
            if (!formula && typeof value === 'number' && ignoredConstants.includes(value))
                continue;
            const addr = toExcelAddress(startRow + r, startCol + c);
            if (formula) {
                if (formula.includes('!')) {
                    green.push(addr);
                }
                else {
                    black.push(addr);
                }
            }
            else {
                blue.push(addr);
            }
        }
    }
    // Apply colors in bulk per color group
    const applyColor = async (addresses, color) => {
        for (const addr of addresses) {
            await ws.formats.set(addr, { fontColor: color });
        }
    };
    await Promise.all([applyColor(blue, BLUE), applyColor(black, BLACK), applyColor(green, GREEN)]);
}
//# sourceMappingURL=ib-text-colors.js.map