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WhereClause } from '../types'\nimport { QueryError } from './query-error'\n\n/**\n * Normalise a `where` clause to its canonical form (RT-102). This is the ONE place that\n * decides what a where value means, so the SQL the store runs, the descriptor and the\n * query key (store cache, React/Vue/Svelte bindings) always agree:\n *\n * - `undefined` means \"no condition\": `where({ projectId: undefined })` is `where({})`\n *   (the usual \"nothing selected yet\" pattern), and an operator whose operand is\n *   `undefined` (`{ $gt: undefined }`) is dropped; a field left with no operator is\n *   dropped. A later `.where({ x: undefined })` therefore does not remove an earlier\n *   condition on `x`.\n * - `null` means SQL `IS NULL` (`$ne: null` means `IS NOT NULL`).\n * - Non-finite numbers (`NaN`, `Infinity`) have no stored representation: refused.\n * - `undefined` inside a `$in` list is refused (ambiguous: no condition cannot be one\n *   element of a list).\n *\n * @param where - The clause as the developer wrote it\n * @returns A new clause with the rules above applied\n * @throws {QueryError} For a non-finite number or an `undefined` `$in` element\n */\nexport function normalizeWhere(where: WhereClause): WhereClause {\n\tconst out: WhereClause = {}\n\tfor (const [field, value] of Object.entries(where)) {\n\t\tif (value === undefined) continue\n\t\tif (isOperatorObject(value)) {\n\t\t\tconst operators: Record<string, unknown> = {}\n\t\t\tfor (const [operator, operand] of Object.entries(value)) {\n\t\t\t\tif (operand === undefined) continue\n\t\t\t\toperators[operator] = normalizeOperand(field, operator, operand)\n\t\t\t}\n\t\t\tif (Object.keys(operators).length === 0) continue\n\t\t\tout[field] = operators\n\t\t\tcontinue\n\t\t}\n\t\tout[field] = normalizeOperand(field, '$eq', value)\n\t}\n\treturn out\n}\n\nfunction normalizeOperand(field: string, operator: string, operand: unknown): unknown {\n\tif (Array.isArray(operand)) {\n\t\treturn operand.map((item) => {\n\t\t\tif (item === undefined) {\n\t\t\t\tthrow new QueryError(\n\t\t\t\t\t`Invalid ${operator} list for field \"${field}\": it contains undefined. Remove the undefined element, or use null to match missing values.`,\n\t\t\t\t\t{ field, operator },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn assertFinite(field, operator, item)\n\t\t})\n\t}\n\treturn assertFinite(field, operator, operand)\n}\n\nfunction assertFinite(field: string, operator: string, value: unknown): unknown {\n\tif (typeof value === 'number' && !Number.isFinite(value)) {\n\t\tthrow new QueryError(\n\t\t\t`Invalid value for field \"${field}\" (${operator}): ${String(value)} is not a finite number and cannot match a stored value.`,\n\t\t\t{ field, operator, value: String(value) },\n\t\t)\n\t}\n\treturn value\n}\n\nfunction isOperatorObject(value: unknown): value is Record<string, unknown> {\n\tif (value === null || typeof value !== 'object' || Array.isArray(value)) return false\n\tif (value instanceof Date || ArrayBuffer.isView(value)) return false\n\treturn true\n}\n\n/**\n * The canonical identity of a query: equal keys if and only if the descriptors run the\n * same query (RT-102). Used by the store's query-store cache and by every framework\n * binding to decide when to share a store or re-subscribe.\n *\n * Unlike `JSON.stringify`, nothing is dropped or conflated: `where` keys are sorted\n * (their order does not change the query), `undefined` is dropped exactly as\n * {@link normalizeWhere} drops it, and `NaN`, `Date` and byte values keep distinct,\n * typed encodings.\n *\n * @param descriptor - The query descriptor (`queryBuilder.getDescriptor()`)\n * @returns A string key\n */\nexport function queryKey(descriptor: QueryDescriptor): string {\n\treturn canonical({\n\t\tcollection: descriptor.collection,\n\t\twhere: normalizeWhere(descriptor.where),\n\t\torderBy: descriptor.orderBy.map((clause) => [clause.field, clause.direction]),\n\t\tlimit: descriptor.limit,\n\t\toffset: descriptor.offset,\n\t\tinclude: descriptor.include,\n\t})\n}\n\nfunction canonical(value: unknown): string {\n\tif (value === undefined) return 'u'\n\tif (value === null) return 'n'\n\tswitch (typeof value) {\n\t\tcase 'string':\n\t\t\treturn JSON.stringify(value)\n\t\tcase 'number':\n\t\t\t// Distinguishes NaN, Infinity and -0 (never equal to another stored number).\n\t\t\treturn Object.is(value, -0) ? 'num:-0' : `num:${String(value)}`\n\t\tcase 'boolean':\n\t\t\treturn value ? 'true' : 'false'\n\t\tcase 'bigint':\n\t\t\treturn `big:${value.toString()}`\n\t\tdefault:\n\t\t\tbreak\n\t}\n\tif (value instanceof Date) return `date:${value.getTime()}`\n\tif (ArrayBuffer.isView(value)) {\n\t\tconst bytes = new Uint8Array(value.buffer, value.byteOffset, value.byteLength)\n\t\treturn `bytes:${Array.from(bytes).join(',')}`\n\t}\n\tif (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`\n\tif (typeof value === 'object') {\n\t\tconst entries = Object.entries(value as Record<string, unknown>)\n\t\t\t.filter(([, item]) => item !== undefined)\n\t\t\t.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n\t\treturn `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(',')}}`\n\t}\n\treturn `?:${typeof value}`\n}\n"]}