{"version":3,"file":"index.cjs","sources":["../libs/resolver.ts","../libs/index.ts"],"sourcesContent":["import type { ResolveTarget } from './interface';\n\n/**\n * Resolver class implementing the Chain of Responsibility pattern\n * Resolves handlers for specific types\n */\nclass Resolver<\n  TBase extends ResolveTarget<any[], any, any> = ResolveTarget<any[], any, any>,\n  TType = string,\n> {\n  /**\n   * Array of registered resolver targets\n   * @private\n   */\n  private updaters: TBase[] = [];\n\n  /**\n   * Fallback handler function\n   * @private\n   */\n  private fallbackHandler?: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>;\n\n  /**\n   * Initializes the resolver\n   * @param args Initial resolver targets\n   */\n  constructor(...args: TBase[]) {\n    if (args.length > 0) {\n      this.set(args);\n    }\n  }\n\n  /**\n   * Processes an array of arguments\n   * @param args Array of resolver targets\n   * @returns Processed array\n   * @private\n   */\n  private getArgs(args: TBase[]): TBase[] {\n    return [...args];\n  }\n\n  /**\n   * Sets resolver targets\n   * @param updaters Array of resolver targets\n   */\n  public set(updaters: TBase[]): void {\n    this.updaters = updaters;\n  }\n\n  /**\n   * Sets resolver targets (variadic version)\n   * @param args Resolver targets\n   */\n  public setUpdaters(...args: TBase[]): void {\n    this.set(this.getArgs(args));\n  }\n\n  /**\n   * Adds a resolver target\n   * @param updater Resolver target to add\n   */\n  public addUpdater(updater: TBase): this {\n    this.updaters.push(updater);\n    return this;\n  }\n\n  /**\n   * Sets a fallback handler for unsupported types\n   * @param handler Fallback handler function\n   * @returns This resolver instance for method chaining\n   */\n  public setFallbackHandler(\n    handler: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>\n  ): this {\n    this.fallbackHandler = handler;\n    return this;\n  }\n\n  /**\n   * Gets the priority of a handler\n   * @param handler The handler to get priority for\n   * @returns Priority value (0 if not defined)\n   * @private\n   */\n  private getPriority(handler: TBase): number {\n    const handlerWithPriority = handler as Partial<{ priority: number }>;\n    return typeof handlerWithPriority.priority === 'number' ? handlerWithPriority.priority : 0;\n  }\n\n  /**\n   * Sorts handlers by priority (highest first), maintaining registration order for equal priorities\n   * @param handlers Array of handlers to sort\n   * @returns Sorted array of handlers\n   * @private\n   */\n  private sortByPriority(handlers: TBase[]): TBase[] {\n    return [...handlers].sort((a, b) => {\n      const priorityA = this.getPriority(a);\n      const priorityB = this.getPriority(b);\n      return priorityB - priorityA; // Higher priority first\n    });\n  }\n\n  /**\n   * Resolves a resolver target for the specified type\n   * @param type Type to resolve\n   * @returns Resolved resolver target\n   * @throws {Error} When no resolver targets are registered\n   * @throws {Error} When no resolver target supporting the specified type is found and no fallback is set\n   */\n  public resolve(type: TType): TBase {\n    if (this.updaters.length < 1) {\n      throw new Error('Unassigned resolve target.');\n    }\n\n    // Get all matching handlers and sort by priority\n    const matchingHandlers = this.updaters.filter((updater) => updater.supports(type));\n    const sortedHandlers = this.sortByPriority(matchingHandlers);\n    const target = sortedHandlers[0];\n\n    if (!target) {\n      // If fallback handler is set, create a temporary target that uses it\n      if (this.fallbackHandler) {\n        return {\n          supports: () => true,\n          handle: this.fallbackHandler,\n        } as unknown as TBase;\n      }\n\n      // Determine the string representation of the unsupported type\n      // If it's a non-null object, use JSON.stringify for detailed output\n      // Otherwise, use String() for basic conversion\n      const typeString =\n        typeof type === 'object' && type !== null ? JSON.stringify(type) : String(type);\n      throw new Error(`Unsupported type: ${typeString}`);\n    }\n\n    return target;\n  }\n\n  /**\n   * Resolves all resolver targets for the specified type\n   * @param type Type to resolve\n   * @returns Array of all matching resolver targets sorted by priority (highest first).\n   *          Returns an empty array if no handlers match and no fallback is set.\n   *          Note: Unlike resolve(), this method does not throw when no handlers match.\n   * @throws {Error} When no resolver targets are registered\n   */\n  public resolveAll(type: TType): TBase[] {\n    if (this.updaters.length < 1) {\n      throw new Error('Unassigned resolve target.');\n    }\n\n    const targets = this.updaters.filter((updater) => updater.supports(type));\n\n    if (targets.length === 0) {\n      // If fallback handler is set, return it as a single-element array\n      if (this.fallbackHandler) {\n        return [\n          {\n            supports: () => true,\n            handle: this.fallbackHandler,\n          } as unknown as TBase,\n        ];\n      }\n\n      // Return empty array if no handlers match\n      return [];\n    }\n\n    // Sort by priority (highest first)\n    return this.sortByPriority(targets);\n  }\n\n  /**\n   * Executes all matching handlers for the specified type\n   * @param type Type to resolve\n   * @param args Arguments to pass to the handlers\n   * @returns Array of results from all matching handlers\n   * @throws {Error} When no resolver targets are registered\n   */\n  public handleAll(\n    type: TType,\n    ...args: Parameters<TBase['handle']>\n  ): ReturnType<TBase['handle']>[] {\n    const targets = this.resolveAll(type);\n    return targets.map((target) => target.handle(...args));\n  }\n\n  /**\n   * Executes all matching async handlers in parallel for the specified type\n   * @param type Type to resolve\n   * @param args Arguments to pass to the handlers\n   * @returns Promise that resolves to array of results from all matching handlers\n   * @throws {Error} When no resolver targets are registered\n   */\n  public async handleAllAsync(\n    type: TType,\n    ...args: Parameters<TBase['handle']>\n  ): Promise<Awaited<ReturnType<TBase['handle']>>[]> {\n    const targets = this.resolveAll(type);\n    const promises = targets.map((target) => target.handle(...args));\n    return Promise.all(promises);\n  }\n\n  /**\n   * Executes all matching async handlers sequentially for the specified type\n   * Stops on first error\n   * @param type Type to resolve\n   * @param args Arguments to pass to the handlers\n   * @returns Promise that resolves to array of results from all matching handlers\n   * @throws {Error} When no resolver targets are registered\n   * @throws {Error} When any handler throws an error\n   */\n  public async handleAllSequential(\n    type: TType,\n    ...args: Parameters<TBase['handle']>\n  ): Promise<Awaited<ReturnType<TBase['handle']>>[]> {\n    const targets = this.resolveAll(type);\n    const results: Awaited<ReturnType<TBase['handle']>>[] = [];\n\n    for (const target of targets) {\n      const result = await target.handle(...args);\n      results.push(result);\n    }\n\n    return results;\n  }\n}\n\nexport default Resolver;\n","export * from './interface';\nimport Resolver from './resolver';\nexport default Resolver;\n// Export for CommonJS compatibility\n// @ts-ignore\nmodule.exports = Resolver;\n"],"names":["Resolver","args","updaters","updater","handler","handlerWithPriority","handlers","a","b","priorityA","type","matchingHandlers","target","typeString","targets","promises","results","result"],"mappings":"aAMA,MA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