/**
 * Excel address utilities — subset copied from client/shared for use without
 * pulling in the full client/shared dependency.
 */
const CELL_REGEX = /^([A-Z]+)([0-9]+)$/;
/** 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;
}
/**
 * Split an address like "Sheet1!A1" or "'My Sheet'!A1" into the sheet name
 * and the cell/range portion. Handles escaped single-quoted sheet names.
 */
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;
                }
                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 "A1" or "Sheet1!A1" into 0-based row/col + optional sheetName. */
export function parseExcelAddress(address) {
    const { sheetName, rest: cellAddress } = extractSheetAndAddress(address);
    if (cellAddress.includes('!')) {
        throw new Error(`Invalid Excel address: ${address}`);
    }
    const match = cellAddress.match(CELL_REGEX);
    if (!match) {
        throw new Error(`Invalid Excel address: ${address}`);
    }
    const colStr = match[1];
    const rowStr = match[2];
    let col = -1;
    for (let i = 0; i < colStr.length; i++) {
        const v = colStr.charCodeAt(i) - 65;
        col = (col + 1) * 26 + v;
    }
    const row = parseInt(rowStr, 10) - 1;
    const result = { row, col };
    if (sheetName) {
        result.sheetName = sheetName;
    }
    return result;
}
/** Convert 0-based row/col to A1 notation. */
export function indexToAddress(row, col) {
    return `${getColumnLetter(col)}${row + 1}`;
}
//# sourceMappingURL=address-utils.js.map