import { parseCellAddress } from '@bilig/formula';
function isDirectLookupOperandInstruction(value) {
    if (!value || typeof value !== 'object') {
        return false;
    }
    const opcode = Reflect.get(value, 'opcode');
    switch (opcode) {
        case 'push-cell':
            return typeof Reflect.get(value, 'address') === 'string';
        case 'push-number':
            return typeof Reflect.get(value, 'value') === 'number';
        case 'push-boolean':
            return typeof Reflect.get(value, 'value') === 'boolean';
        case 'push-string':
            return typeof Reflect.get(value, 'value') === 'string';
        case 'push-error':
            return typeof Reflect.get(value, 'code') === 'number';
        case 'push-name':
            return typeof Reflect.get(value, 'name') === 'string';
        default:
            return false;
    }
}
function isDirectVectorLookupInstruction(value) {
    if (!value || typeof value !== 'object') {
        return false;
    }
    const opcode = Reflect.get(value, 'opcode');
    if ((opcode !== 'lookup-exact-match' && opcode !== 'lookup-approximate-match') ||
        typeof Reflect.get(value, 'start') !== 'string' ||
        typeof Reflect.get(value, 'end') !== 'string' ||
        typeof Reflect.get(value, 'startRow') !== 'number' ||
        typeof Reflect.get(value, 'endRow') !== 'number' ||
        typeof Reflect.get(value, 'startCol') !== 'number' ||
        typeof Reflect.get(value, 'endCol') !== 'number') {
        return false;
    }
    if (opcode === 'lookup-exact-match') {
        const searchMode = Reflect.get(value, 'searchMode');
        return searchMode === 1 || searchMode === -1;
    }
    const matchMode = Reflect.get(value, 'matchMode');
    return matchMode === 1 || matchMode === -1;
}
function isReturnInstruction(value) {
    return !!value && typeof value === 'object' && Reflect.get(value, 'opcode') === 'return';
}
export function resolveRuntimeDirectLookupBinding(jsPlan, ownerSheetName) {
    const [operand, lookup, terminal] = jsPlan;
    if (!operand || !lookup || !terminal || !isReturnInstruction(terminal)) {
        return undefined;
    }
    if (!isDirectLookupOperandInstruction(operand) || !isDirectVectorLookupInstruction(lookup)) {
        return undefined;
    }
    if (operand.opcode !== 'push-cell' || lookup.startCol !== lookup.endCol) {
        return undefined;
    }
    const operandSheetName = operand.sheetName ?? ownerSheetName;
    const operandAddress = parseCellAddress(operand.address, operandSheetName).text;
    const lookupSheetName = lookup.sheetName ?? ownerSheetName;
    if (lookup.opcode === 'lookup-exact-match') {
        return {
            kind: 'exact',
            operandSheetName,
            operandAddress,
            lookupSheetName,
            rowStart: lookup.startRow,
            rowEnd: lookup.endRow,
            col: lookup.startCol,
            searchMode: lookup.searchMode,
        };
    }
    return {
        kind: 'approximate',
        operandSheetName,
        operandAddress,
        lookupSheetName,
        rowStart: lookup.startRow,
        rowEnd: lookup.endRow,
        col: lookup.startCol,
        matchMode: lookup.matchMode,
    };
}
//# sourceMappingURL=direct-vector-lookup.js.map