{
  "version": 3,
  "sources": ["../../../../../src/functions/information/cell/cell.ts"],
  "sourcesContent": [
    "import {\n  FormulaError,\n  type CellValue,\n  type CellNumber,\n  type FunctionDefinition,\n  type FunctionEvaluationResult,\n  type ValueEvaluationResult,\n  type CellAddress,\n  type SpreadsheetRange,\n  type SpilledValuesEvaluationResult,\n  type SingleEvaluationResult,\n} from \"../../../core/types.mjs\";\nimport type { EvaluationContext } from \"../../../evaluator/evaluation-context.mjs\";\nimport type { ReferenceNode } from \"../../../parser/ast.mjs\";\nimport { isCellAddress, isRangeAddress } from \"../../../core/utils.mjs\";\n\n/**\n * CELL function - Returns information about the formatting, location, or contents of a cell\n * \n * Usage: CELL(info_type, [reference])\n * \n * info_type: Text value that specifies what type of cell information you want\n * reference: The cell or range to get information about (defaults to current cell if omitted)\n * \n * Supported info_types:\n * - \"address\" - Returns the reference of the first cell in reference, as text\n * - \"col\" - Returns the column number of the cell\n * - \"row\" - Returns the row number of the cell\n * - \"contents\" - Returns the value of the upper-left cell\n * - \"type\" - Returns \"b\" for blank, \"l\" for label (text), \"v\" for value (number)\n * - \"width\" - Returns the column width\n * - \"filename\" - Returns the filename (including full path) of the file\n * \n * Examples:\n * - CELL(\"address\", A1) returns \"$A$1\"\n * - CELL(\"row\", B5) returns 5\n * - CELL(\"col\", C3) returns 3\n */\nexport const CELL: FunctionDefinition = {\n  name: \"CELL\",\n  evaluate: function (node, context): FunctionEvaluationResult {\n    if (node.args.length === 0 || node.args.length > 2) {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: \"CELL function requires 1 or 2 arguments\",\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    // Evaluate the info_type argument\n    const infoTypeResult = this.evaluateNode(node.args[0]!, context);\n    if (\n      infoTypeResult.type === \"error\" ||\n      infoTypeResult.type === \"awaiting-evaluation\"\n    ) {\n      return infoTypeResult;\n    }\n\n    if (infoTypeResult.type !== \"value\" || infoTypeResult.result.type !== \"string\") {\n      return {\n        type: \"error\",\n        err: FormulaError.VALUE,\n        message: \"CELL function info_type must be a string\",\n        errAddress: context.dependencyNode,\n      };\n    }\n\n    const infoType = infoTypeResult.result.value.toLowerCase();\n\n    // Determine the reference cell\n    let referenceCell: CellAddress = context.cellAddress;\n    let referenceValue: CellValue | null = null;\n    \n    if (node.args.length === 2) {\n      const refArg = node.args[1]!;\n      \n      // Evaluate the reference argument\n      const refResult = this.evaluateNode(refArg, context);\n      if (\n        refResult.type === \"error\" ||\n        refResult.type === \"awaiting-evaluation\"\n      ) {\n        return refResult;\n      }\n      \n      // Check if we have a cell or range reference\n      const cellOrRange =\n        refResult.type === \"spilled-values\"\n          ? (refResult.sourceCell ?? refResult.sourceRange)\n          : refResult.sourceCell;\n      \n      if (!cellOrRange) {\n        return {\n          type: \"error\",\n          err: FormulaError.VALUE,\n          message: \"CELL function requires a cell or range reference\",\n          errAddress: context.dependencyNode,\n        };\n      }\n      \n      // Extract the cell address\n      if (isCellAddress(cellOrRange)) {\n        // It's a CellAddress\n        referenceCell = cellOrRange;\n      } else if (isRangeAddress(cellOrRange)) {\n        // It's a RangeAddress - use the upper-left cell\n        referenceCell = {\n          sheetName: cellOrRange.sheetName,\n          workbookName: cellOrRange.workbookName,\n          colIndex: cellOrRange.range.start.col,\n          rowIndex: cellOrRange.range.start.row,\n        };\n      } else {\n        return {\n          type: \"error\",\n          err: FormulaError.VALUE,\n          message: \"CELL function requires a cell or range reference\",\n          errAddress: context.dependencyNode,\n        };\n      }\n      \n      // For \"contents\" and \"type\", we need to get the cell value\n      if (infoType === \"contents\" || infoType === \"type\") {\n        if (refResult.type === \"value\") {\n          referenceValue = refResult.result;\n        } else if (refResult.type === \"spilled-values\") {\n          const cellResult = refResult.evaluate({ x: 0, y: 0 }, context);\n          if (cellResult && cellResult.type === \"value\") {\n            referenceValue = cellResult.result;\n          }\n        }\n      }\n    }\n\n    // Handle different info types (return 1-based row/column numbers)\n    switch (infoType) {\n      case \"row\":\n        return {\n          type: \"value\",\n          result: { type: \"number\", value: referenceCell.rowIndex + 1 },\n        };\n      \n      case \"col\":\n        return {\n          type: \"value\",\n          result: { type: \"number\", value: referenceCell.colIndex + 1 },\n        };\n      \n      case \"address\": {\n        // Return the cell address as text in A1 format\n        const col = columnNumberToLetter(referenceCell.colIndex + 1);\n        const row = referenceCell.rowIndex + 1;\n        let address = `$${col}$${row}`;\n        \n        // Include sheet name if it's different from current sheet\n        if (referenceCell.sheetName !== context.cellAddress.sheetName) {\n          address = `${referenceCell.sheetName}!${address}`;\n        }\n        \n        return {\n          type: \"value\",\n          result: { type: \"string\", value: address },\n        };\n      }\n      \n      case \"contents\":\n        // Return the value of the cell\n        if (referenceValue !== null) {\n          // Empty string counts as empty cell in Excel\n          if (referenceValue.type === \"string\" && referenceValue.value === \"\") {\n            return {\n              type: \"value\",\n              result: { type: \"number\", value: 0 },\n            };\n          }\n          return {\n            type: \"value\",\n            result: referenceValue,\n          };\n        }\n        // If we couldn't get the value, return 0 (empty cell)\n        return {\n          type: \"value\",\n          result: { type: \"number\", value: 0 },\n        };\n      \n      case \"type\": {\n        // Return \"b\" for blank, \"l\" for label (text), \"v\" for value (number)\n        if (referenceValue === null) {\n          return {\n            type: \"value\",\n            result: { type: \"string\", value: \"b\" },\n          };\n        }\n        // Empty string counts as blank cell in Excel\n        if (referenceValue.type === \"string\" && referenceValue.value === \"\") {\n          return {\n            type: \"value\",\n            result: { type: \"string\", value: \"b\" },\n          };\n        }\n        if (referenceValue.type === \"string\") {\n          return {\n            type: \"value\",\n            result: { type: \"string\", value: \"l\" },\n          };\n        }\n        if (referenceValue.type === \"number\") {\n          return {\n            type: \"value\",\n            result: { type: \"string\", value: \"v\" },\n          };\n        }\n        // Boolean or other types default to \"v\"\n        return {\n          type: \"value\",\n          result: { type: \"string\", value: \"v\" },\n        };\n      }\n      \n      default:\n        return {\n          type: \"error\",\n          err: FormulaError.VALUE,\n          message: `CELL function unknown info_type: \"${infoType}\"`,\n          errAddress: context.dependencyNode,\n        };\n    }\n  },\n};\n\n/**\n * Helper function to convert column number (1-based) to letter(s)\n */\nfunction columnNumberToLetter(num: number): string {\n  let letter = \"\";\n  while (num > 0) {\n    const remainder = (num - 1) % 26;\n    letter = String.fromCharCode(65 + remainder) + letter;\n    num = Math.floor((num - 1) / 26);\n  }\n  return letter;\n}\n"
  ],
  "mappings": ";AAAA;AAAA;AAAA;AAcA;AAwBO,IAAM,OAA2B;AAAA,EACtC,MAAM;AAAA,EACN,UAAU,QAAS,CAAC,MAAM,SAAmC;AAAA,IAC3D,IAAI,KAAK,KAAK,WAAW,KAAK,KAAK,KAAK,SAAS,GAAG;AAAA,MAClD,OAAO;AAAA,QACL,MAAM;AAAA,QACN,KAAK,aAAa;AAAA,QAClB,SAAS;AAAA,QACT,YAAY,QAAQ;AAAA,MACtB;AAAA,IACF;AAAA,IAGA,MAAM,iBAAiB,KAAK,aAAa,KAAK,KAAK,IAAK,OAAO;AAAA,IAC/D,IACE,eAAe,SAAS,WACxB,eAAe,SAAS,uBACxB;AAAA,MACA,OAAO;AAAA,IACT;AAAA,IAEA,IAAI,eAAe,SAAS,WAAW,eAAe,OAAO,SAAS,UAAU;AAAA,MAC9E,OAAO;AAAA,QACL,MAAM;AAAA,QACN,KAAK,aAAa;AAAA,QAClB,SAAS;AAAA,QACT,YAAY,QAAQ;AAAA,MACtB;AAAA,IACF;AAAA,IAEA,MAAM,WAAW,eAAe,OAAO,MAAM,YAAY;AAAA,IAGzD,IAAI,gBAA6B,QAAQ;AAAA,IACzC,IAAI,iBAAmC;AAAA,IAEvC,IAAI,KAAK,KAAK,WAAW,GAAG;AAAA,MAC1B,MAAM,SAAS,KAAK,KAAK;AAAA,MAGzB,MAAM,YAAY,KAAK,aAAa,QAAQ,OAAO;AAAA,MACnD,IACE,UAAU,SAAS,WACnB,UAAU,SAAS,uBACnB;AAAA,QACA,OAAO;AAAA,MACT;AAAA,MAGA,MAAM,cACJ,UAAU,SAAS,mBACd,UAAU,cAAc,UAAU,cACnC,UAAU;AAAA,MAE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