{"version":3,"file":"live-query-collection.cjs","names":["CollectionConfigBuilder","createCollection","getBuilderFromConfig"],"sources":["../../../src/query/live-query-collection.ts"],"sourcesContent":["import { createCollection } from '../collection/index.js'\nimport { CollectionConfigBuilder } from './live/collection-config-builder.js'\nimport {\n  getBuilderFromConfig,\n  registerCollectionBuilder,\n} from './live/collection-registry.js'\nimport type { LiveQueryCollectionUtils } from './live/collection-config-builder.js'\nimport type { LiveQueryCollectionConfig } from './live/types.js'\nimport type {\n  ExtractContext,\n  InitialQueryBuilder,\n  QueryBuilder,\n} from './builder/index.js'\nimport type { Collection } from '../collection/index.js'\nimport type {\n  CollectionConfig,\n  CollectionConfigSingleRowOption,\n  NonSingleResult,\n  SingleResult,\n  UtilsRecord,\n} from '../types.js'\nimport type {\n  Context,\n  RootObjectResultConstraint,\n  RootQueryBuilder,\n  RootQueryFn,\n  RootQueryResult,\n} from './builder/types.js'\n\ntype CollectionConfigForContext<\n  TContext extends Context,\n  TResult extends object,\n  TUtils extends UtilsRecord = {},\n> = TContext extends SingleResult\n  ? CollectionConfigSingleRowOption<TResult, string | number, never, TUtils> &\n      SingleResult\n  : CollectionConfigSingleRowOption<TResult, string | number, never, TUtils> &\n      NonSingleResult\n\ntype CollectionForContext<\n  TContext extends Context,\n  TResult extends object,\n  TUtils extends UtilsRecord = {},\n> = TContext extends SingleResult\n  ? Collection<TResult, string | number, TUtils> & SingleResult\n  : Collection<TResult, string | number, TUtils> & NonSingleResult\n\n/**\n * Creates live query collection options for use with createCollection\n *\n * @example\n * ```typescript\n * const options = liveQueryCollectionOptions({\n *   // id is optional - will auto-generate if not provided\n *   query: (q) => q\n *     .from({ post: postsCollection })\n *     .where(({ post }) => eq(post.published, true))\n *     .select(({ post }) => ({\n *       id: post.id,\n *       title: post.title,\n *       content: post.content,\n *     })),\n *   // getKey is optional - will use stream key if not provided\n * })\n *\n * const collection = createCollection(options)\n * ```\n *\n * @param config - Configuration options for the live query collection\n * @returns Collection options that can be passed to createCollection\n */\nexport function liveQueryCollectionOptions<\n  TQuery extends QueryBuilder<any>,\n  TContext extends Context = ExtractContext<TQuery>,\n  TResult extends object = RootQueryResult<TContext>,\n>(\n  config: LiveQueryCollectionConfig<TContext, TResult> & {\n    query: RootQueryFn<TQuery> | RootQueryBuilder<TQuery>\n  },\n): CollectionConfigForContext<TContext, TResult> & {\n  utils: LiveQueryCollectionUtils\n} {\n  const collectionConfigBuilder = new CollectionConfigBuilder<\n    TContext,\n    TResult\n  >(config)\n  return collectionConfigBuilder.getConfig() as CollectionConfigForContext<\n    TContext,\n    TResult\n  > & { utils: LiveQueryCollectionUtils }\n}\n\n/**\n * Creates a live query collection directly\n *\n * @example\n * ```typescript\n * // Minimal usage - just pass a query function\n * const activeUsers = createLiveQueryCollection(\n *   (q) => q\n *     .from({ user: usersCollection })\n *     .where(({ user }) => eq(user.active, true))\n *     .select(({ user }) => ({ id: user.id, name: user.name }))\n * )\n *\n * // Full configuration with custom options\n * const searchResults = createLiveQueryCollection({\n *   id: \"search-results\", // Custom ID (auto-generated if omitted)\n *   query: (q) => q\n *     .from({ post: postsCollection })\n *     .where(({ post }) => like(post.title, `%${searchTerm}%`))\n *     .select(({ post }) => ({\n *       id: post.id,\n *       title: post.title,\n *       excerpt: post.excerpt,\n *     })),\n *   getKey: (item) => item.id, // Custom key function (uses stream key if omitted)\n *   utils: {\n *     updateSearchTerm: (newTerm: string) => {\n *       // Custom utility functions\n *     }\n *   }\n * })\n * ```\n */\n\n// Overload 1: Accept just the query function\nexport function createLiveQueryCollection<\n  TQueryFn extends (q: InitialQueryBuilder) => QueryBuilder<any>,\n  TQuery extends QueryBuilder<any> = ReturnType<TQueryFn>,\n>(\n  query: TQueryFn & RootQueryFn<TQuery>,\n): CollectionForContext<\n  ExtractContext<TQuery>,\n  RootQueryResult<ExtractContext<TQuery>>\n> & {\n  utils: LiveQueryCollectionUtils\n}\n\n// Overload 2: Accept full config object with optional utilities\nexport function createLiveQueryCollection<\n  TQuery extends QueryBuilder<any>,\n  TContext extends Context = ExtractContext<TQuery>,\n  TUtils extends UtilsRecord = {},\n>(\n  config: LiveQueryCollectionConfig<TContext, RootQueryResult<TContext>> & {\n    query: RootQueryFn<TQuery> | RootQueryBuilder<TQuery>\n    utils?: TUtils\n  },\n): CollectionForContext<TContext, RootQueryResult<TContext>> & {\n  utils: LiveQueryCollectionUtils & TUtils\n}\n\n// Implementation\nexport function createLiveQueryCollection<\n  TContext extends Context,\n  TResult extends object = RootQueryResult<TContext>,\n  TUtils extends UtilsRecord = {},\n>(\n  configOrQuery:\n    | (LiveQueryCollectionConfig<TContext, TResult> & { utils?: TUtils })\n    | ((\n        q: InitialQueryBuilder,\n      ) => QueryBuilder<TContext> & RootObjectResultConstraint<TContext>),\n): CollectionForContext<TContext, TResult> & {\n  utils: LiveQueryCollectionUtils & TUtils\n} {\n  // Determine if the argument is a function (query) or a config object\n  if (typeof configOrQuery === `function`) {\n    // Simple query function case\n    const config: LiveQueryCollectionConfig<TContext, TResult> = {\n      query: configOrQuery as (\n        q: InitialQueryBuilder,\n      ) => QueryBuilder<TContext> & RootObjectResultConstraint<TContext>,\n    }\n    // The implementation accepts both overload shapes, but TypeScript cannot\n    // preserve the overload-specific query-builder inference through this branch.\n    const options = liveQueryCollectionOptions(config as any)\n    return bridgeToCreateCollection(options) as CollectionForContext<\n      TContext,\n      TResult\n    > & { utils: LiveQueryCollectionUtils & TUtils }\n  } else {\n    // Config object case\n    const config = configOrQuery as LiveQueryCollectionConfig<\n      TContext,\n      TResult\n    > & { utils?: TUtils }\n    // Same overload implementation limitation as above: the config has already\n    // been validated by the public signatures, but the branch loses that precision.\n    const options = liveQueryCollectionOptions(config as any)\n\n    // Merge custom utils without evaluating internal getters such as\n    // lastSubsetError into stale data properties.\n    if (config.utils) {\n      Object.defineProperties(\n        options.utils,\n        Object.getOwnPropertyDescriptors(config.utils),\n      )\n    }\n\n    return bridgeToCreateCollection(options) as CollectionForContext<\n      TContext,\n      TResult\n    > & { utils: LiveQueryCollectionUtils & TUtils }\n  }\n}\n\n/**\n * Bridge function that handles the type compatibility between query2's TResult\n * and core collection's output type without exposing ugly type assertions to users\n */\nfunction bridgeToCreateCollection<\n  TResult extends object,\n  TUtils extends UtilsRecord = {},\n>(\n  options: CollectionConfig<TResult> & { utils: TUtils },\n): Collection<TResult, string | number, TUtils> {\n  const collection = createCollection(options as any) as unknown as Collection<\n    TResult,\n    string | number,\n    LiveQueryCollectionUtils\n  >\n\n  const builder = getBuilderFromConfig(options)\n  if (builder) {\n    registerCollectionBuilder(collection, builder)\n  }\n\n  return collection as unknown as Collection<TResult, string | number, TUtils>\n}\n"],"mappings":";;;AAuEA,SAAgB,2BAKd,QAKA;CAKA,OAAO,IAJ6BA,kCAAAA,wBAGlC,MACK,CAAA,CAAwB,UAAU;AAI3C;AAgEA,SAAgB,0BAKd,eAOA;CAEA,IAAI,OAAO,kBAAkB,YAU3B,OAAO,yBADS,2BAA2B,EANzC,OAAO,cAMkC,CACX,CAAO;MAIlC;EAEL,MAAM,SAAS;EAMf,MAAM,UAAU,2BAA2B,MAAa;EAIxD,IAAI,OAAO,OACT,OAAO,iBACL,QAAQ,OACR,OAAO,0BAA0B,OAAO,KAAK,CAC/C;EAGF,OAAO,yBAAyB,OAAO;CAIzC;AACF;AAMA,SAAS,yBAIP,SAC8C;CAC9C,MAAM,aAAaC,cAAAA,iBAAiB,OAAc;CAMlD,MAAM,UAAUC,4BAAAA,qBAAqB,OAAO;CAC5C,IAAI,SACF,4BAAA,0BAA0B,YAAY,OAAO;CAG/C,OAAO;AACT"}