{"version":3,"file":"registry.mjs","names":["Registry","factories","Map","highestPriority","parent","has","token","id","get","factory","Error","toString","getAll","records","length","sort","a","b","priority","set","scope","target","type","record","originalToken","existing","push","currentHighest","delete","deletedHighest","remaining","filter","r","newHighest","reduce","max","current","updateScope","forEach","highest","globalRegistry"],"sources":["../../../src/token/registry.mts"],"sourcesContent":["import type { ClassType, InjectionToken } from './injection-token.mjs'\n\nimport { InjectableScope, InjectableType } from '../enums/index.mjs'\n\nexport type FactoryRecord<Instance = any, Schema = any> = {\n  scope: InjectableScope\n  originalToken: InjectionToken<Instance, Schema>\n  target: ClassType\n  type: InjectableType\n  priority: number\n}\n\nexport class Registry {\n  private readonly factories = new Map<string, FactoryRecord[]>()\n  private readonly highestPriority = new Map<string, FactoryRecord>()\n\n  constructor(private readonly parent?: Registry) {}\n\n  has(token: InjectionToken<any, any>): boolean {\n    if (this.factories.has(token.id)) {\n      return true\n    }\n    if (this.parent) {\n      return this.parent.has(token)\n    }\n    return false\n  }\n\n  get<Instance, Schema>(\n    token: InjectionToken<Instance, Schema>,\n  ): FactoryRecord<Instance, Schema> {\n    const factory = this.highestPriority.get(token.id)\n    if (!factory) {\n      if (this.parent) {\n        return this.parent.get(token)\n      }\n      throw new Error(`[Registry] No factory found for ${token.toString()}`)\n    }\n    return factory\n  }\n\n  getAll<Instance, Schema>(\n    token: InjectionToken<Instance, Schema>,\n  ): FactoryRecord<Instance, Schema>[] {\n    const records = this.factories.get(token.id)\n    if (!records || records.length === 0) {\n      if (this.parent) {\n        return this.parent.getAll(token)\n      }\n      return []\n    }\n    // Return sorted by priority (highest first)\n    return [...records].sort((a, b) => b.priority - a.priority)\n  }\n\n  set<Instance, Schema>(\n    token: InjectionToken<Instance, Schema>,\n    scope: InjectableScope,\n    target: ClassType,\n    type: InjectableType,\n    priority: number = 0,\n  ) {\n    const record: FactoryRecord<Instance, Schema> = {\n      scope,\n      originalToken: token,\n      target,\n      type,\n      priority,\n    }\n\n    // Add to factories array\n    const existing = this.factories.get(token.id) || []\n    existing.push(record)\n    this.factories.set(token.id, existing)\n\n    // Update highest priority cache if needed\n    const currentHighest = this.highestPriority.get(token.id)\n    if (!currentHighest || priority > currentHighest.priority) {\n      this.highestPriority.set(token.id, record)\n    }\n  }\n\n  delete(token: InjectionToken<any, any>) {\n    const records = this.factories.get(token.id)\n    if (records) {\n      const deletedHighest = this.highestPriority.get(token.id)\n      this.factories.delete(token.id)\n      this.highestPriority.delete(token.id)\n\n      // If we deleted the highest priority record, recalculate from remaining records\n      if (deletedHighest && records.length > 1) {\n        const remaining = records.filter(\n          (r) =>\n            r.originalToken.id !== deletedHighest.originalToken.id ||\n            r.priority !== deletedHighest.priority,\n        )\n        if (remaining.length > 0) {\n          const newHighest = remaining.reduce((max, current) =>\n            current.priority > max.priority ? current : max,\n          )\n          this.highestPriority.set(token.id, newHighest)\n          this.factories.set(token.id, remaining)\n        }\n      }\n    }\n  }\n\n  /**\n   * Updates the scope of an already registered factory.\n   * This is useful when you need to dynamically change a service's scope\n   * (e.g., when a singleton controller has request-scoped dependencies).\n   *\n   * @param token The injection token to update\n   * @param scope The new scope to set\n   * @returns true if the scope was updated, false if the token was not found\n   */\n  updateScope(\n    token: InjectionToken<any, any>,\n    scope: InjectableScope,\n  ): boolean {\n    const records = this.factories.get(token.id)\n    if (records && records.length > 0) {\n      // Update all records\n      records.forEach((record) => {\n        record.scope = scope\n      })\n      // Update highest priority cache if it exists\n      const highest = this.highestPriority.get(token.id)\n      if (highest) {\n        highest.scope = scope\n      }\n      return true\n    }\n    if (this.parent) {\n      return this.parent.updateScope(token, scope)\n    }\n    return false\n  }\n}\n\nexport const globalRegistry = /* #__PURE__ */ new Registry()\n"],"mappings":";AAYA,IAAaA,WAAb,MAAaA;;CACMC,4BAAY,IAAIC,KAAAA;CAChBC,kCAAkB,IAAID,KAAAA;CAEvC,YAAY,QAAoC;OAAnBE,SAAAA;;CAE7BC,IAAIC,OAA0C;AAC5C,MAAI,KAAKL,UAAUI,IAAIC,MA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