{"version":3,"file":"auto-index.cjs","names":["hasVirtualPropPath","isDevModeEnabled","DEFAULT_COMPARE_OPTIONS","findIndexForField","codedWarning","indexableOperations"],"sources":["../../../src/indexes/auto-index.ts"],"sourcesContent":["import { DEFAULT_COMPARE_OPTIONS } from '../utils'\nimport { indexableOperations } from '../query/builder/functions.js'\nimport { hasVirtualPropPath } from '../virtual-props'\nimport { findIndexForField } from '../utils/index-optimization.js'\nimport { codedWarning, devBuild } from '../error-message.js'\nimport { checkCollectionSizeForIndex, isDevModeEnabled } from './index-registry'\nimport type { CompareOptions } from '../query/builder/types'\nimport type { BasicExpression } from '../query/ir'\nimport type { CollectionImpl } from '../collection/index.js'\nimport type { IndexReader } from './base-index.js'\n\nfunction shouldAutoIndex(collection: CollectionImpl<any, any, any, any, any>) {\n  // Only proceed if auto-indexing is enabled\n  // Note: autoIndex: 'eager' without defaultIndexType is caught at construction time\n  // in CollectionImpl, so we don't need to check for it here.\n  return collection.config.autoIndex === `eager`\n}\n\nexport function ensureIndexForField<\n  T extends Record<string, any>,\n  TKey extends string | number,\n>(\n  fieldName: string,\n  fieldPath: Array<string>,\n  collection: CollectionImpl<T, TKey, any, any, any>,\n  compareOptions?: CompareOptions,\n  canUse?: (index: IndexReader<TKey>) => boolean,\n) {\n  if (hasVirtualPropPath(fieldPath)) {\n    return\n  }\n  const autoIndex = shouldAutoIndex(collection)\n  if (!autoIndex && !isDevModeEnabled()) {\n    return\n  }\n\n  const compareOpts = compareOptions ?? {\n    ...DEFAULT_COMPARE_OPTIONS,\n    ...collection.compareOptions,\n  }\n\n  // Check if we already have an index for this field\n  const existingIndex = findIndexForField(\n    collection,\n    fieldPath,\n    compareOpts,\n    canUse,\n  )\n\n  if (existingIndex) {\n    return // Index already exists\n  }\n\n  if (!autoIndex) {\n    checkCollectionSizeForIndex(\n      collection.id || `unknown`,\n      collection.size,\n      fieldPath,\n    )\n    return\n  }\n\n  // Create a new index for this field using the collection's createIndex method\n  // The collection will use its defaultIndexType\n  try {\n    collection.createIndex(\n      (row) => {\n        // Navigate through the field path\n        let current: any = row\n        for (const part of fieldPath) {\n          current = current[part]\n        }\n        return current\n      },\n      {\n        name: `auto:${fieldPath.join(`.`)}`,\n        options: { compareOptions: compareOpts },\n      },\n    )\n  } catch (error) {\n    console.warn(\n      devBuild() && process.env.NODE_ENV !== `production`\n        ? `${collection.id ? `[${collection.id}] ` : ``}Failed to create auto-index for field path \"${fieldPath.join(`.`)}\":`\n        : codedWarning(215, { id: collection.id, value: fieldPath.join(`.`) }),\n      error,\n    )\n  }\n}\n\n/** Check indexable WHERE fields for eager indexes or development advice. */\nexport function ensureIndexForExpression<\n  T extends Record<string, any>,\n  TKey extends string | number,\n>(\n  expression: BasicExpression,\n  collection: CollectionImpl<T, TKey, any, any, any>,\n): void {\n  if (!shouldAutoIndex(collection) && !isDevModeEnabled()) return\n\n  const indexableExpressions = extractIndexableExpressions(expression)\n\n  for (const { fieldName, fieldPath } of indexableExpressions) {\n    ensureIndexForField(fieldName, fieldPath, collection)\n  }\n}\n\n/**\n * Extracts all indexable expressions from a where expression\n */\nfunction extractIndexableExpressions(\n  expression: BasicExpression,\n): Array<{ fieldName: string; fieldPath: Array<string> }> {\n  const results: Array<{ fieldName: string; fieldPath: Array<string> }> = []\n\n  function extractFromExpression(expr: BasicExpression): void {\n    if (expr.type !== `func`) {\n      return\n    }\n\n    const func = expr as any\n\n    // Handle 'and' expressions by recursively processing all arguments\n    if (func.name === `and`) {\n      for (const arg of func.args) {\n        extractFromExpression(arg)\n      }\n      return\n    }\n\n    // Check if this is a supported operation\n    if (!indexableOperations.includes(func.name)) {\n      return\n    }\n\n    // Check if the first argument is a property reference\n    if (func.args.length < 1 || func.args[0].type !== `ref`) {\n      return\n    }\n\n    const fieldRef = func.args[0]\n    const fieldPath = fieldRef.path\n\n    // Skip if the path is empty\n    if (fieldPath.length === 0) {\n      return\n    }\n\n    // For nested paths, use the full path joined with underscores as the field name\n    // For simple paths, use the first (and only) element\n    const fieldName = fieldPath.join(`_`)\n    results.push({ fieldName, fieldPath })\n  }\n\n  extractFromExpression(expression)\n  return results\n}\n"],"mappings":";;;;;;AAWA,SAAS,gBAAgB,YAAqD;CAI5E,OAAO,WAAW,OAAO,cAAc;AACzC;AAEA,SAAgB,oBAId,WACA,WACA,YACA,gBACA,QACA;CACA,IAAIA,sBAAAA,mBAAmB,SAAS,GAC9B;CAEF,MAAM,YAAY,gBAAgB,UAAU;CAC5C,IAAI,CAAC,aAAa,CAACC,uBAAAA,iBAAiB,GAClC;CAGF,MAAM,cAAc,kBAAkB;EACpC,GAAGC,cAAAA;EACH,GAAG,WAAW;CAChB;CAUA,IAPsBC,2BAAAA,kBACpB,YACA,WACA,aACA,MAGE,GACF;CAGF,IAAI,CAAC,WAAW;EACd,uBAAA,4BACE,WAAW,MAAM,WACjB,WAAW,MACX,SACF;EACA;CACF;CAIA,IAAI;EACF,WAAW,aACR,QAAQ;GAEP,IAAI,UAAe;GACnB,KAAA,MAAW,QAAQ,WACjB,UAAU,QAAQ;GAEpB,OAAO;EACT,GACA;GACE,MAAM,QAAQ,UAAU,KAAK,GAAG;GAChC,SAAS,EAAE,gBAAgB,YAAY;EACzC,CACF;CACF,SAAS,OAAO;EACd,QAAQ,KACN,sCAAA,SAAS,KAAA,QAAA,IAAA,aAA8B,eACnC,GAAG,WAAW,KAAK,IAAI,WAAW,GAAA,MAAS,GAAA,8CAAiD,UAAU,KAAK,GAAG,EAAA,MAC9GC,sBAAAA,aAAa,KAAK;GAAE,IAAI,WAAW;GAAI,OAAO,UAAU,KAAK,GAAG;EAAE,CAAC,GACvE,KACF;CACF;AACF;AAGA,SAAgB,yBAId,YACA,YACM;CACN,IAAI,CAAC,gBAAgB,UAAU,KAAK,CAACH,uBAAAA,iBAAiB,GAAG;CAEzD,MAAM,uBAAuB,4BAA4B,UAAU;CAEnE,KAAA,MAAW,EAAE,WAAW,eAAe,sBACrC,oBAAoB,WAAW,WAAW,UAAU;AAExD;AAKA,SAAS,4BACP,YACw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