{"version":3,"file":"factory.decorator.mjs","names":["InjectableScope","InjectableType","InjectableTokenMeta","InjectionToken","globalRegistry","Factory","scope","Singleton","token","registry","priority","target","context","kind","Function","Error","injectableToken","create","set"],"sources":["../../../src/decorators/factory.decorator.mts"],"sourcesContent":["import type { Factorable, FactorableWithArgs } from '../interfaces/index.mjs'\nimport type {\n  ClassTypeWithInstance,\n  InjectionTokenSchemaType,\n} from '../token/injection-token.mjs'\nimport type { Registry } from '../token/registry.mjs'\n\nimport { InjectableScope, InjectableType } from '../enums/index.mjs'\nimport { InjectableTokenMeta } from '../symbols/index.mjs'\nimport { InjectionToken } from '../token/injection-token.mjs'\nimport { globalRegistry } from '../token/registry.mjs'\n\nexport interface FactoryOptions {\n  scope?: InjectableScope\n  token?: InjectionToken<any, any>\n  registry?: Registry\n  priority?: number\n}\n\n// #1 Factory without arguments\nexport function Factory<R>(options?: {\n  scope?: InjectableScope\n  registry?: Registry\n  priority?: number\n}): <T extends ClassTypeWithInstance<Factorable<R>>>(\n  target: T,\n  context?: ClassDecoratorContext,\n) => T\n\n// #2 Factory with typed token\nexport function Factory<R, S>(options: {\n  scope?: InjectableScope\n  token: InjectionToken<R, S>\n  registry?: Registry\n  priority?: number\n}): R extends undefined\n  ? never\n  : S extends InjectionTokenSchemaType\n    ? <T extends ClassTypeWithInstance<FactorableWithArgs<R, S>>>(\n        target: T,\n        context?: ClassDecoratorContext,\n      ) => T\n    : S extends undefined\n      ? <T extends ClassTypeWithInstance<Factorable<R>>>(\n          target: T,\n          context?: ClassDecoratorContext,\n        ) => T\n      : never\n\nexport function Factory({\n  scope = InjectableScope.Singleton,\n  token,\n  registry = globalRegistry,\n  priority = 0,\n}: FactoryOptions = {}) {\n  return <\n    T extends ClassTypeWithInstance<\n      Factorable<any> | FactorableWithArgs<any, any>\n    >,\n  >(\n    target: T,\n    context?: ClassDecoratorContext,\n  ): T => {\n    if (\n      (context && context.kind !== 'class') ||\n      (target instanceof Function && !context)\n    ) {\n      throw new Error(\n        '[DI] @Factory decorator can only be used on classes.',\n      )\n    }\n\n    let injectableToken: InjectionToken<any, any> =\n      token ?? InjectionToken.create(target)\n\n    registry.set(injectableToken, scope, target, InjectableType.Factory, priority)\n\n    // @ts-expect-error\n    target[InjectableTokenMeta] = injectableToken\n\n    return target\n  }\n}\n\n"],"mappings":";;;;;;;AAiDA,SAAgBK,QAAQ,EACtBC,QAAQN,gBAAgBO,WACxBC,OACAC,WAAWL,gBACXM,WAAW,MACO,EAAE,EAAA;AACpB,SAKEC,QACAC,YAAAA;AAEA,MACE,WAAYA,QAAQC,SAAS,WAC5BF,kBAAkBG,YAAY,CAACF,QAEhC,OAAM,IAAIG,MACR,uDAAA;EAIJ,IAAIC,kBACFR,SAASL,eAAec,OAAON,OAAAA;AAEjCF,WAASS,IAAIF,iBAAiBV,OAAOK,QAAQV,eAAeI,SAASK,SAAAA;AAGrEC,SAAOT,uBAAuBc;AAE9B,SAAOL"}