import { FormulaReferenceAdjustor } from "./formula-reference-adjustor";

export class FormulaPasteProcessor {
    static adjustFormulaForPaste(formula, context) {
        if (!FormulaReferenceAdjustor.isFormula(formula)) return formula;
        const colOffset = context.targetCell.col - context.sourceCell.col, rowOffset = context.targetCell.row - context.sourceCell.row;
        return FormulaReferenceAdjustor.adjustFormulaReferences(formula, colOffset, rowOffset);
    }
    static adjustFormulasForPaste(formulas, context) {
        const colOffset = context.targetRange.startCol - context.sourceRange.startCol, rowOffset = context.targetRange.startRow - context.sourceRange.startRow;
        return this.adjustFormulasForPasteWithOffset(formulas, colOffset, rowOffset);
    }
    static adjustFormulasForPasteWithOffset(formulas, colOffset, rowOffset) {
        const result = [];
        for (let row = 0; row < formulas.length; row++) {
            const newRow = [];
            for (let col = 0; col < formulas[row].length; col++) {
                const formula = formulas[row][col];
                if (FormulaReferenceAdjustor.isFormula(formula)) {
                    const adjustedFormula = FormulaReferenceAdjustor.adjustFormulaReferences(formula, colOffset, rowOffset);
                    newRow.push(adjustedFormula);
                } else newRow.push(formula);
            }
            result.push(newRow);
        }
        return result;
    }
    static needsFormulaAdjustment(value) {
        return FormulaReferenceAdjustor.isFormula(value);
    }
    static getFormulaReferences(formula) {
        return FormulaReferenceAdjustor.extractReferences(formula);
    }
    static validateFormulaReferences(formula, maxCol, maxRow) {
        const references = FormulaReferenceAdjustor.extractReferences(formula);
        for (const ref of references) if (ref.col < 0 || ref.col > maxCol || ref.row < 0 || ref.row > maxRow) return !1;
        return !0;
    }
    static createPasteContext(sourceStartCol, sourceStartRow, targetStartCol, targetStartRow, sourceCols, sourceRows, targetCols, targetRows) {
        return {
            sourceRange: {
                startCol: sourceStartCol,
                startRow: sourceStartRow,
                endCol: sourceStartCol + sourceCols - 1,
                endRow: sourceStartRow + sourceRows - 1
            },
            targetRange: {
                startCol: targetStartCol,
                startRow: targetStartRow,
                endCol: targetStartCol + targetCols - 1,
                endRow: targetStartRow + targetRows - 1
            },
            sourceCell: {
                col: sourceStartCol,
                row: sourceStartRow
            },
            targetCell: {
                col: targetStartCol,
                row: targetStartRow
            }
        };
    }
}
//# sourceMappingURL=formula-paste-processor.js.map