{"version":3,"file":"expressions.cjs","names":["getPropRefSourceAlias","Value","PropRef","getPropRefPropertyPath","Func"],"sources":["../../../../src/query/compiler/expressions.ts"],"sourcesContent":["import {\n  Func,\n  PropRef,\n  Value,\n  getPropRefPropertyPath,\n  getPropRefSourceAlias,\n} from '../ir.js'\nimport type { BasicExpression, OrderBy } from '../ir.js'\n\n/** Extracts the source aliases referenced by an expression. */\nexport function getSourceAliasesFromExpression(\n  expr: BasicExpression,\n): Set<string> {\n  switch (expr.type) {\n    case `ref`: {\n      const sourceAlias = getPropRefSourceAlias(expr) ?? expr.path[0]\n      return new Set(sourceAlias ? [sourceAlias] : [])\n    }\n    case `func`: {\n      const sourceAliases = new Set<string>()\n      for (const arg of expr.args) {\n        for (const alias of getSourceAliasesFromExpression(arg)) {\n          sourceAliases.add(alias)\n        }\n      }\n      return sourceAliases\n    }\n    default:\n      return new Set()\n  }\n}\n\n/**\n * Normalizes a WHERE clause expression by removing table aliases from property references.\n *\n * This function recursively traverses an expression tree and creates new BasicExpression\n * instances with normalized paths. The main transformation is removing the collection alias\n * from property reference paths (e.g., `['user', 'id']` becomes `['id']` when `collectionAlias`\n * is `'user'`), which is needed when converting query-level expressions to collection-level\n * expressions for subscriptions.\n *\n * @param whereClause - The WHERE clause expression to normalize\n * @param collectionAlias - The alias of the collection being filtered (to strip from paths)\n * @returns A new BasicExpression with normalized paths\n *\n * @example\n * // Input: ref with path ['user', 'id'] where collectionAlias is 'user'\n * // Output: ref with path ['id']\n */\nexport function normalizeExpressionPaths(\n  whereClause: BasicExpression<boolean>,\n  collectionAlias: string,\n): BasicExpression<boolean> {\n  const tpe = whereClause.type\n  if (tpe === `val`) {\n    return new Value(whereClause.value)\n  } else if (tpe === `ref`) {\n    const path = whereClause.path\n    const sourceAlias = getPropRefSourceAlias(whereClause)\n    if (Array.isArray(path)) {\n      if (sourceAlias === collectionAlias) {\n        return new PropRef(getPropRefPropertyPath(whereClause))\n      } else if (sourceAlias !== undefined) {\n        return new PropRef(path, sourceAlias)\n      } else if (path[0] === collectionAlias && path.length > 1) {\n        // Remove the table alias from the path for single-collection queries\n        return new PropRef(path.slice(1))\n      } else if (path.length === 1 && path[0] !== undefined) {\n        // Single field reference\n        return new PropRef([path[0]])\n      }\n    }\n    // Fallback for non-array paths\n    return new PropRef(Array.isArray(path) ? path : [String(path)])\n  } else {\n    // Recursively convert all arguments\n    const args: Array<BasicExpression> = []\n    for (const arg of whereClause.args) {\n      const convertedArg = normalizeExpressionPaths(\n        arg as BasicExpression<boolean>,\n        collectionAlias,\n      )\n      args.push(convertedArg)\n    }\n    return new Func(whereClause.name, args)\n  }\n}\n\nexport function normalizeOrderByPaths(\n  orderBy: OrderBy,\n  collectionAlias: string,\n): OrderBy {\n  const normalizedOrderBy = orderBy.map((clause) => {\n    const basicExp = normalizeExpressionPaths(\n      clause.expression,\n      collectionAlias,\n    )\n\n    return {\n      ...clause,\n      expression: basicExp,\n    }\n  })\n\n  return normalizedOrderBy\n}\n"],"mappings":";AAUA,SAAgB,+BACd,MACa;CACb,QAAQ,KAAK,MAAb;EACE,KAAK,OAAO;GACV,MAAM,cAAcA,WAAAA,sBAAsB,IAAI,KAAK,KAAK,KAAK;GAC7D,OAAO,IAAI,IAAI,cAAc,CAAC,WAAW,IAAI,CAAC,CAAC;EACjD;EACA,KAAK,QAAQ;GACX,MAAM,gCAAgB,IAAI,IAAY;GACtC,KAAA,MAAW,OAAO,KAAK,MACrB,KAAA,MAAW,SAAS,+BAA+B,GAAG,GACpD,cAAc,IAAI,KAAK;GAG3B,OAAO;EACT;EACA,SACE,uBAAO,IAAI,IAAI;CACnB;AACF;AAmBA,SAAgB,yBACd,aACA,iBAC0B;CAC1B,MAAM,MAAM,YAAY;CACxB,IAAI,QAAQ,OACV,OAAO,IAAIC,WAAAA,MAAM,YAAY,KAAK;MAAA,IACzB,QAAQ,OAAO;EACxB,MAAM,OAAO,YAAY;EACzB,MAAM,cAAcD,WAAAA,sBAAsB,WAAW;EACrD,IAAI,MAAM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