import { makeCellKey } from '../../workbook-store.js';
export function createOperationPreparedCellTracker(args) {
    const preparedSheetIdByName = new Map();
    const resolveSheetId = (sheetName, create) => {
        const cachedSheetId = preparedSheetIdByName.get(sheetName);
        if (cachedSheetId !== undefined) {
            if (args.workbook.getSheetById(cachedSheetId)) {
                return cachedSheetId;
            }
            preparedSheetIdByName.delete(sheetName);
        }
        const sheet = create ? args.workbook.getOrCreateSheet(sheetName) : args.workbook.getSheet(sheetName);
        if (!sheet) {
            return undefined;
        }
        preparedSheetIdByName.set(sheetName, sheet.id);
        return sheet.id;
    };
    return {
        invalidateSheetName(sheetName) {
            preparedSheetIdByName.delete(sheetName);
        },
        getExistingCellIndex(sheetName, address, preparedCellAddress) {
            if (!preparedCellAddress) {
                return args.workbook.getCellIndex(sheetName, address);
            }
            const sheetId = resolveSheetId(sheetName, false);
            if (sheetId === undefined) {
                return undefined;
            }
            return args.workbook.cellKeyToIndex.get(makeCellKey(sheetId, preparedCellAddress.row, preparedCellAddress.col));
        },
        ensureCellTracked(sheetName, address, preparedCellAddress) {
            if (!preparedCellAddress) {
                return args.ensureCellTracked(sheetName, address);
            }
            const sheetId = resolveSheetId(sheetName, true);
            if (sheetId === undefined) {
                throw new Error(`Unknown sheet: ${sheetName}`);
            }
            return args.workbook.ensureCellAt(sheetId, preparedCellAddress.row, preparedCellAddress.col).cellIndex;
        },
    };
}
export function createOperationSheetNameResolver(workbook) {
    const sheetNameById = new Map();
    return {
        resolve(sheetId) {
            const cached = sheetNameById.get(sheetId);
            if (cached !== undefined) {
                return cached;
            }
            const sheet = workbook.getSheetById(sheetId);
            if (!sheet) {
                throw new Error(`Unknown sheet id: ${sheetId}`);
            }
            sheetNameById.set(sheetId, sheet.name);
            return sheet.name;
        },
    };
}
export function resolveOperationExistingMutationCellIndex(workbook, ref) {
    const candidate = ref.cellIndex;
    const { sheetId, mutation } = ref;
    if (candidate !== undefined && workbook.cellStore.sheetIds[candidate] === sheetId) {
        const sheet = workbook.getSheetById(sheetId);
        if (sheet?.structureVersion === 1) {
            if (workbook.cellStore.rows[candidate] === mutation.row && workbook.cellStore.cols[candidate] === mutation.col) {
                return candidate;
            }
        }
        else {
            const position = sheet?.logical?.getCellVisiblePosition(candidate);
            if (position?.row === mutation.row && position.col === mutation.col) {
                return candidate;
            }
        }
    }
    return workbook.cellKeyToIndex.get(makeCellKey(sheetId, mutation.row, mutation.col));
}
//# sourceMappingURL=operation-cell-address-resolver.js.map