{
  "version": 3,
  "sources": ["../../../../../src/functions/lookup/index-lookup/index-lookup.ts"],
  "sourcesContent": [
    "import {\n  FormulaError,\n  type CellValue,\n  type FunctionDefinition,\n  type FunctionEvaluationResult,\n  type ValueEvaluationResult,\n  type SpilledValuesEvaluationResult,\n  type CellAddress,\n  type SpreadsheetRange,\n  type ErrorEvaluationResult,\n} from \"../../../core/types.cjs\";\nimport type { FormulaEngine } from \"../../../core/engine.cjs\";\nimport type { FormulaEvaluator } from \"../../../evaluator/formula-evaluator.cjs\";\nimport type { EvaluationContext } from \"../../../evaluator/evaluation-context.cjs\";\n\n/**\n * INDEX function - Returns a value from a table or array\n * INDEX(array, row_num, [column_num])\n *\n * STRICT TYPE CHECKING:\n * - array: range/array only\n * - row_num: number only (integer, 1-based)\n * - column_num: number only (integer, 1-based, optional)\n *\n * If array is 1-dimensional (single row or column):\n * - Only row_num is used (treats as a linear array)\n * - column_num is ignored if provided\n *\n * If array is 2-dimensional:\n * - Both row_num and column_num can be used\n * - If column_num is omitted, returns entire row\n */\n\n// Helper function to get value from array at specific position\nfunction getValueFromArray(\n  this: FormulaEvaluator,\n  arrayResult: SpilledValuesEvaluationResult,\n  row: number,\n  col: number,\n  context: EvaluationContext\n): CellValue | ErrorEvaluationResult {\n  const dims = arrayResult.spillArea(context.cellAddress);\n\n  // Convert 1-based indices to 0-based\n  const rowIndex = row - 1;\n  const colIndex = col - 1;\n\n  // Calculate actual cell position\n  const actualRow = dims.start.row + rowIndex;\n  const actualCol = dims.start.col + colIndex;\n\n  // Check bounds\n  if (\n    actualRow < 0 ||\n    (dims.end.row.type === \"number\" && actualRow > dims.end.row.value)\n  ) {\n    return {\n      type: \"error\",\n      err: FormulaError.REF,\n      message: `INDEX: row_num ${row} is out of range`,\n      errAddress: context.dependencyNode,\n    };\n  }\n\n  if (\n    actualCol < 0 ||\n    (dims.end.col.type === \"number\" && actualCol > dims.end.col.value)\n  ) {\n    return {\n      type: \"error\",\n      err: FormulaError.REF,\n      message: `INDEX: column_num ${col} is out of range`,\n      errAddress: context.dependencyNode,\n    };\n  }\n\n  const spilledAddress: CellAddress = {\n    colIndex: actualCol,\n    rowIndex: actualRow,\n    sheetName: context.cellAddress.sheetName,\n    workbookName: context.cellAddress.workbookName,\n  };\n\n  const spill = {\n    address: spilledAddress,\n    spillOffset: {\n      x: actualCol - dims.start.col,\n      y: actualRow - dims.start.row,\n    },\n  };\n\n  const spillResult = arrayResult.evaluate(spill.spillOffset, context);\n\n  if (!spillResult) {\n    return {\n      type: \"error\",\n      err: FormulaError.VALUE,\n      message: \"INDEX: Unable to retrieve value from array\",\n      errAddress: context.dependencyNode,\n    };\n  }\n\n  if (\n    spillResult.type === \"error\" ||\n    spillResult.type === \"awaiting-evaluation\"\n  ) {\n    return spillResult;\n  }\n\n  return spillResult.result;\n}\n\nexport const INDEX: FunctionDefinition = {\n  name: \"INDEX\",\n  evaluate: function (node, context): FunctionEvaluationResult {\n    if (node.args.length < 2 || node.args.length > 3) {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: \"INDEX function takes 2 or 3 arguments\",\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    // Evaluate array argument\n    const arrayResult = this.evaluateNode(node.args[0]!, context);\n    if (\n      arrayResult.type === \"error\" ||\n      arrayResult.type === \"awaiting-evaluation\"\n    ) {\n      return arrayResult;\n    }\n\n    // Evaluate row_num argument\n    const rowNumResult = this.evaluateNode(node.args[1]!, context);\n    if (\n      rowNumResult.type === \"error\" ||\n      rowNumResult.type === \"awaiting-evaluation\"\n    ) {\n      return rowNumResult;\n    }\n\n    // Evaluate column_num argument (optional)\n    let colNumResult: FunctionEvaluationResult | null = null;\n    if (node.args[2]) {\n      colNumResult = this.evaluateNode(node.args[2], context);\n      if (\n        colNumResult.type === \"error\" ||\n        colNumResult.type === \"awaiting-evaluation\"\n      ) {\n        return colNumResult;\n      }\n    }\n\n    // Handle spilled arrays for row_num and col_num (not array which is expected to be a range)\n    if (\n      rowNumResult.type === \"spilled-values\" ||\n      (colNumResult && colNumResult.type === \"spilled-values\")\n    ) {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message:\n          \"INDEX: Spilled row_num/column_num arguments not yet implemented\",\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    // Strict type checking for row_num\n    if (rowNumResult.result.type !== \"number\") {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: `INDEX row_num must be number, got ${rowNumResult.result.type}`,\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    const colNumValue = colNumResult?.result;\n\n    // Strict type checking for column_num if provided\n    if (colNumValue && colNumValue.type !== \"number\") {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: `INDEX column_num must be number, got ${colNumValue.type}`,\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    // Extract row and column numbers (convert to integers)\n    const rowNum = Math.floor(rowNumResult.result.value);\n    const colNum = colNumValue ? Math.floor(colNumValue.value) : 1;\n\n    // Validate that indices are positive\n    if (rowNum < 1) {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: `INDEX row_num must be >= 1, got ${rowNum}`,\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    if (colNum < 1) {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: `INDEX column_num must be >= 1, got ${colNum}`,\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    // Handle different array result types\n    if (arrayResult.type === \"value\") {\n      // Single value case - can only access [1,1]\n      if (rowNum !== 1 || colNum !== 1) {\n        return {\n          type: \"error\",\n          err: FormulaError.REF,\n          message: `INDEX: Single value can only be accessed at [1,1], got [${rowNum},${colNum}]`,\n          errAddress: context.dependencyNode,\n        };\n      }\n      return {\n        type: \"value\",\n        result: arrayResult.result,\n      } satisfies ValueEvaluationResult;\n    } else if (arrayResult.type === \"spilled-values\") {\n      // Array case - use helper function to get value\n      const result = getValueFromArray.call(\n        this,\n        arrayResult,\n        rowNum,\n        colNum,\n        context\n      );\n\n      if (result.type === \"error\" || result.type === \"awaiting-evaluation\") {\n        return result;\n      }\n\n      return {\n        type: \"value\",\n        result,\n      } satisfies ValueEvaluationResult;\n    } else {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: \"INDEX: Invalid array argument type\",\n        errAddress: context.dependencyNode,\n      };\n    }\n  },\n};\n"
  ],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUO,IAVP;AAkCA,SAAS,iBAAiB,CAExB,aACA,KACA,KACA,SACmC;AAAA,EACnC,MAAM,OAAO,YAAY,UAAU,QAAQ,WAAW;AAAA,EAGtD,MAAM,WAAW,MAAM;AAAA,EACvB,MAAM,WAAW,MAAM;AAAA,EAGvB,MAAM,YAAY,KAAK,MAAM,MAAM;AAAA,EACnC,MAAM,YAAY,KAAK,MAAM,MAAM;AAAA,EAGnC,IACE,YAAY,KACX,KAAK,IAAI,IAAI,SAAS,YAAY,YAAY,KAAK,IAAI,IAAI,OAC5D;AAAA,IACA,OAAO;AAAA,MACL,MAAM;AAAA,MACN,KAAK,0BAAa;AAAA,MAClB,SAAS,kBAAkB;AAAA,MAC3B,YAAY,QAAQ;AAAA,IACtB;AAAA,EACF;AAAA,EAEA,IACE,YAAY,KACX,KAAK,IAAI,IAAI,SAAS,YAAY,YAAY,KAAK,IAAI,IAAI,OAC5D;AAAA,IACA,OAAO;AAAA,MACL,MAAM;AAAA,MACN,KAAK,0BAAa;AAAA,MAClB,SAAS,qBAAqB;AAAA,MAC9B,YAAY,QAAQ;AAAA,IACtB;AAAA,EACF;AAAA,EAEA,MAAM,iBAA8B;AAAA,IAClC,UAAU;AAAA,IACV,UAAU;AAAA,IACV,WAAW,QAAQ,YAAY;AAAA,IAC/B,cAAc,QAAQ,YAAY;AAAA,EACpC;AAAA,EAEA,MAAM,QAAQ;AAAA,IACZ,SAAS;AAAA,IACT,aAAa;AAAA,MACX,GAAG,YAAY,KAAK,MAAM;AAAA,MAC1B,GAAG,Y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