import { formatAddress, parseCellAddress, parseRangeAddress } from '@bilig/formula';
export function dependencyTouchesStructuralDeleteSpan(dependency, ownerSheetName, targetSheetName, axis, start) {
    if (dependency.includes(':')) {
        const parsed = parseRangeAddress(dependency, ownerSheetName);
        const dependencySheetName = parsed.sheetName ?? ownerSheetName;
        if (dependencySheetName !== targetSheetName) {
            return false;
        }
        if (parsed.kind === 'cells') {
            return (axis === 'row' ? parsed.end.row : parsed.end.col) >= start;
        }
        if (parsed.kind === 'rows') {
            return axis === 'row' && parsed.end.row >= start;
        }
        return axis === 'column' && parsed.end.col >= start;
    }
    const parsed = parseCellAddress(dependency, ownerSheetName);
    const dependencySheetName = parsed.sheetName ?? ownerSheetName;
    if (dependencySheetName !== targetSheetName) {
        return false;
    }
    return (axis === 'row' ? parsed.row : parsed.col) >= start;
}
export function structuralFormulaUndoRecordToOp(record) {
    return {
        kind: 'setCellFormula',
        sheetName: record.sheetName,
        address: formatAddress(record.row, record.col),
        formula: record.formula,
    };
}
export function structuralDeletedCellUndoRecordToOps(record, toCellStateOps) {
    const address = formatAddress(record.row, record.col);
    switch (record.kind) {
        case 'formula':
            return [
                { kind: 'setCellFormula', sheetName: record.sheetName, address, formula: record.formula },
                ...(record.explicitFormat === undefined
                    ? []
                    : [{ kind: 'setCellFormat', sheetName: record.sheetName, address, format: record.explicitFormat }]),
            ];
        case 'value':
            return [
                { kind: 'setCellValue', sheetName: record.sheetName, address, value: record.value },
                ...(record.explicitFormat === undefined
                    ? []
                    : [{ kind: 'setCellFormat', sheetName: record.sheetName, address, format: record.explicitFormat }]),
            ];
        case 'snapshot':
            if (record.explicitFormat !== undefined) {
                return toCellStateOps(record.sheetName, address, record.snapshot);
            }
            return toCellStateOps(record.sheetName, address, record.snapshot).filter((op) => {
                if (op.kind === 'setCellFormat' && op.sheetName === record.sheetName && op.address === address) {
                    return false;
                }
                if (op.kind === 'setFormatRange' &&
                    op.range.sheetName === record.sheetName &&
                    op.range.startAddress === address &&
                    op.range.endAddress === address) {
                    return false;
                }
                if (op.kind === 'setStyleRange' &&
                    op.range.sheetName === record.sheetName &&
                    op.range.startAddress === address &&
                    op.range.endAddress === address) {
                    return false;
                }
                return true;
            });
        case 'blank':
            return [
                record.restoreExplicitBlank
                    ? { kind: 'setCellValue', sheetName: record.sheetName, address, value: null }
                    : { kind: 'clearCell', sheetName: record.sheetName, address },
                ...(record.explicitFormat === undefined
                    ? []
                    : [{ kind: 'setCellFormat', sheetName: record.sheetName, address, format: record.explicitFormat }]),
            ];
    }
}
//# sourceMappingURL=mutation-structural-undo-records.js.map