/**
 * Excel address utilities — shared by all wrapper functions.
 *
 * Ported from client/shared/src/lib/spreadsheet/utils/excel-address-utils.ts.
 */
// ---------------------------------------------------------------------------
// Column letter ↔ index
// ---------------------------------------------------------------------------
/** Convert 0-based column index to Excel-style column letters (0 → "A", 26 → "AA"). */
export function getColumnLetter(index) {
    let columnName = '';
    let dividend = index + 1;
    let modulo;
    while (dividend > 0) {
        modulo = (dividend - 1) % 26;
        columnName = String.fromCharCode(65 + modulo) + columnName;
        dividend = Math.floor((dividend - modulo) / 26);
    }
    return columnName;
}
/** Convert Excel-style column letters to 0-based index ("A" → 0, "AA" → 26). */
export function getColumnIndex(columnLetter) {
    let index = 0;
    for (let i = 0; i < columnLetter.length; i++) {
        index = index * 26 + (columnLetter.charCodeAt(i) - 64);
    }
    return index - 1;
}
// ---------------------------------------------------------------------------
// Address conversion
// ---------------------------------------------------------------------------
/** Convert 0-based row/col to Excel address (e.g. 0,0 → "A1"). */
export function toExcelAddress(row, col) {
    return `${getColumnLetter(col)}${row + 1}`;
}
// ---------------------------------------------------------------------------
// Sheet name extraction (handles quoted names like "'My Sheet'!A1")
// ---------------------------------------------------------------------------
const CELL_REGEX = /^([A-Z]+)([0-9]+)$/;
/**
 * Extract optional sheet name and the remainder of an address/range string.
 * Supports quoted sheet names like "'My Sheet'!A1" and "'Q1! Sales'!A1".
 */
export function extractSheetAndAddress(input) {
    if (!input.includes('!')) {
        return { rest: input };
    }
    if (input.startsWith("'")) {
        let i = 1;
        while (i < input.length) {
            if (input[i] === "'") {
                if (i + 1 < input.length && input[i + 1] === "'") {
                    i += 2; // skip escaped ''
                }
                else {
                    if (i + 1 < input.length && input[i + 1] === '!') {
                        const sheetName = input.slice(1, i).replace(/''/g, "'");
                        const rest = input.slice(i + 2);
                        return { sheetName, rest };
                    }
                    break;
                }
            }
            else {
                i++;
            }
        }
        return { rest: input };
    }
    const bangIndex = input.indexOf('!');
    const sheetName = input.slice(0, bangIndex);
    if (!sheetName)
        return { rest: input };
    return { sheetName, rest: input.slice(bangIndex + 1) };
}
// ---------------------------------------------------------------------------
// Parse address / range
// ---------------------------------------------------------------------------
/** Parse "A1" or "Sheet1!A1" into 0-based row/col + optional sheetName. */
export function parseAddress(address) {
    const { sheetName, rest } = extractSheetAndAddress(address);
    if (rest.includes('!'))
        throw new Error(`Invalid Excel address: ${address}`);
    const match = rest.match(CELL_REGEX);
    if (!match)
        throw new Error(`Invalid Excel address: ${address}`);
    let col = -1;
    for (let i = 0; i < match[1].length; i++) {
        col = (col + 1) * 26 + (match[1].charCodeAt(i) - 65);
    }
    const result = {
        row: parseInt(match[2], 10) - 1,
        col
    };
    if (sheetName)
        result.sheetName = sheetName;
    return result;
}
/**
 * Parse "A1:D50", "Sheet1!A1:D50", "'My Sheet'!A1:D50", or single cell "A1".
 * Returns 0-based start/end row/col + optional sheetName.
 */
export function parseRange(range) {
    const { sheetName, rest } = extractSheetAndAddress(range);
    if (rest.includes('!'))
        throw new Error(`Invalid Excel range: ${range}`);
    const parts = rest.split(':');
    const start = parseAddress(parts[0]);
    const end = parts.length === 2 ? parseAddress(parts[1]) : start;
    const result = {
        startRow: start.row,
        startCol: start.col,
        endRow: end.row,
        endCol: end.col
    };
    if (sheetName)
        result.sheetName = sheetName;
    return result;
}
//# sourceMappingURL=address-utils.js.map