import { parseCellAddress, translateFormulaReferences } from '@bilig/formula';
export function getMutationMatrixCell(matrix, rowIndex, colIndex) {
    const row = matrix[rowIndex];
    if (row === undefined) {
        throw new RangeError(`Missing source row at index ${rowIndex}`);
    }
    const cell = row[colIndex];
    if (cell === undefined) {
        throw new RangeError(`Missing source cell at row ${rowIndex}, column ${colIndex}`);
    }
    return cell;
}
export function isMutationStructuralInsertOp(op) {
    return op.kind === 'insertRows' || op.kind === 'insertColumns';
}
export function inverseMutationStructuralInsertOp(op) {
    return op.kind === 'insertRows'
        ? { kind: 'deleteRows', sheetName: op.sheetName, start: op.start, count: op.count }
        : { kind: 'deleteColumns', sheetName: op.sheetName, start: op.start, count: op.count };
}
export function collectLiveCreatedSheetNames(existingSheetNames, ops) {
    const knownSheetNames = new Set(existingSheetNames);
    const liveCreatedSheetNames = new Set();
    for (let index = 0; index < ops.length; index += 1) {
        const op = ops[index];
        if (!op) {
            continue;
        }
        if (op.kind === 'upsertSheet') {
            const sheetName = op.name;
            if (typeof sheetName === 'string' && !knownSheetNames.has(sheetName)) {
                liveCreatedSheetNames.add(sheetName);
            }
            if (typeof sheetName === 'string') {
                knownSheetNames.add(sheetName);
            }
            continue;
        }
        if (op.kind !== 'renameSheet') {
            continue;
        }
        const oldName = op.oldName;
        const newName = op.newName;
        if (typeof oldName === 'string' && typeof newName === 'string' && liveCreatedSheetNames.delete(oldName)) {
            liveCreatedSheetNames.add(newName);
        }
        if (typeof oldName === 'string') {
            knownSheetNames.delete(oldName);
        }
        if (typeof newName === 'string') {
            knownSheetNames.add(newName);
        }
    }
    return liveCreatedSheetNames;
}
export function translateMutationFormulaForTarget(formula, sourceSheetName, sourceAddress, targetSheetName, targetAddress) {
    if (sourceSheetName !== targetSheetName) {
        return formula;
    }
    const source = parseCellAddress(sourceAddress, sourceSheetName);
    const target = parseCellAddress(targetAddress, targetSheetName);
    return translateFormulaReferences(formula, target.row - source.row, target.col - source.col);
}
//# sourceMappingURL=mutation-cell-content-helpers.js.map