{"version":3,"file":"taiga-ui-polymorpheus.mjs","sources":["../../../projects/ng-polymorpheus/src/tokens/context.ts","../../../projects/ng-polymorpheus/src/classes/component.ts","../../../projects/ng-polymorpheus/src/classes/context.ts","../../../projects/ng-polymorpheus/src/utils/is-primitive.ts","../../../projects/ng-polymorpheus/src/directives/template.ts","../../../projects/ng-polymorpheus/src/directives/outlet.ts","../../../projects/ng-polymorpheus/src/index.ts","../../../projects/ng-polymorpheus/src/taiga-ui-polymorpheus.ts"],"sourcesContent":["/// <reference types=\"@taiga-ui/tsconfig/ng-dev-mode\" />\nimport {inject, InjectionToken, type InjectOptions, type Provider} from '@angular/core';\n\n/**\n * Use this token to access context within your components when\n * instantiating them through {@link PolymorpheusOutlet}\n */\nexport const POLYMORPHEUS_CONTEXT = new InjectionToken<Record<any, any>>(\n    ngDevMode ? 'POLYMORPHEUS_CONTEXT' : '',\n);\n\nexport function provideContext<T = Record<any, any>>(useValue: T): Provider {\n    return {\n        provide: POLYMORPHEUS_CONTEXT,\n        useValue,\n    };\n}\n\nexport function injectContext<T>(options?: InjectOptions & {optional?: false}): T;\nexport function injectContext<T>(options?: InjectOptions & {optional: true}): T | null;\nexport function injectContext<T>(options: InjectOptions = {}): T | null {\n    return inject(POLYMORPHEUS_CONTEXT, options);\n}\n","import {Injector, type Type} from '@angular/core';\n\nimport {provideContext} from '../tokens/context';\n\n/**\n * Wrapper class for a component that will be used as content for {@link PolymorpheusOutlet}\n *\n * @param component — an Angular component to be dynamically created\n * @param injector — optional {@link Injector} for lazy loaded module case\n */\nexport class PolymorpheusComponent<T> {\n    constructor(\n        public readonly component: Type<T>,\n        private readonly i?: Injector,\n    ) {}\n\n    public createInjector<C>(injector: Injector, useValue?: C): Injector {\n        return Injector.create({\n            parent: this.i || injector,\n            providers: useValue == null ? [] : [provideContext(useValue)],\n        });\n    }\n}\n","export class PolymorpheusContext<T> {\n    constructor(public readonly $implicit: T) {}\n\n    public get polymorpheusOutlet(): T {\n        return this.$implicit;\n    }\n}\n","export function isPrimitive(value: unknown): boolean {\n    return Object(value) !== value;\n}\n","/* eslint-disable @angular-eslint/prefer-inject */\nimport {ChangeDetectorRef, Directive, inject, TemplateRef} from '@angular/core';\n\n/**\n * ng-template wrapper directive also stores {@link ChangeDetectorRef} to properly handle change detection.\n */\n@Directive({\n    selector: 'ng-template[polymorpheus]',\n    inputs: ['polymorpheus'],\n    exportAs: 'polymorpheus',\n})\nexport class PolymorpheusTemplate<C = any> {\n    public polymorpheus: C | '' = '';\n\n    constructor(\n        public readonly template: TemplateRef<C> = inject(TemplateRef<C>, {self: true}),\n        private readonly cdr: ChangeDetectorRef = inject(ChangeDetectorRef),\n    ) {}\n\n    public static ngTemplateContextGuard<T>(\n        _dir: PolymorpheusTemplate<T>,\n        _ctx: any,\n    ): _ctx is T extends '' ? any : T {\n        return true;\n    }\n\n    public check(): void {\n        this.cdr.markForCheck();\n    }\n}\n","/* eslint-disable @angular-eslint/no-conflicting-lifecycle */\nimport {\n    ChangeDetectorRef,\n    type ComponentRef,\n    Directive,\n    type DoCheck,\n    inject,\n    INJECTOR,\n    type OnChanges,\n    reflectComponentType,\n    type SimpleChanges,\n    TemplateRef,\n    ViewContainerRef,\n} from '@angular/core';\n\nimport {PolymorpheusComponent} from '../classes/component';\nimport {PolymorpheusContext} from '../classes/context';\nimport {type PolymorpheusContent} from '../types/content';\nimport {type PolymorpheusPrimitive} from '../types/primitive';\nimport {isPrimitive} from '../utils/is-primitive';\nimport {PolymorpheusTemplate} from './template';\n\n@Directive({\n    selector: '[polymorpheusOutlet]',\n    inputs: ['content: polymorpheusOutlet', 'context: polymorpheusOutletContext'],\n})\nexport class PolymorpheusOutlet<C> implements OnChanges, DoCheck {\n    private readonly vcr = inject(ViewContainerRef);\n    private readonly i = inject(INJECTOR);\n    private readonly t = inject(TemplateRef);\n    private c?: ComponentRef<unknown>;\n    public content: PolymorpheusContent<C> = '';\n    public context?: C;\n\n    public static ngTemplateContextGuard<T>(\n        _dir: PolymorpheusOutlet<T>,\n        _ctx: any,\n    ): _ctx is PolymorpheusContext<T extends PolymorpheusPrimitive ? T : never> {\n        return true;\n    }\n\n    public ngOnChanges({content}: SimpleChanges): void {\n        const context = this.getContext();\n        const component = isComponent(this.content);\n\n        this.update();\n        this.c?.injector.get(ChangeDetectorRef).markForCheck();\n\n        if (!content) {\n            return;\n        }\n\n        this.vcr.clear();\n\n        const proxy = new Proxy(ensureContext(context) as object, {\n            get: (_, key: keyof (C | PolymorpheusContext<any>)) =>\n                ensureContext(component ? this.context : this.getContext())?.[key],\n        }) as C;\n\n        if (isComponent(this.content)) {\n            this.process(this.content, context == null ? undefined : proxy);\n            this.update();\n        } else if (\n            (context instanceof PolymorpheusContext && context.$implicit) != null\n        ) {\n            this.vcr.createEmbeddedView(this.template, proxy, {injector: this.i});\n        }\n    }\n\n    public ngDoCheck(): void {\n        if (isDirective(this.content)) {\n            this.content.check();\n        }\n    }\n\n    private get template(): TemplateRef<unknown> {\n        if (isDirective(this.content)) {\n            return this.content.template;\n        }\n\n        return this.content instanceof TemplateRef ? this.content : this.t;\n    }\n\n    private getContext(): C | PolymorpheusContext<any> | undefined {\n        return isTemplate(this.content) || isComponent(this.content)\n            ? this.context\n            : new PolymorpheusContext(\n                  typeof this.content === 'function'\n                      ? this.content(this.context ?? ({} as any))\n                      : this.content,\n              );\n    }\n\n    private process(content: PolymorpheusComponent<unknown>, proxy?: C): void {\n        this.c = this.vcr.createComponent(content.component, {\n            injector: content.createInjector(this.i, proxy),\n        });\n    }\n\n    private update(): void {\n        if (typeof this.context !== 'object' || !isComponent(this.content)) {\n            return;\n        }\n\n        const {inputs = []} = reflectComponentType(this.content.component) || {};\n\n        for (const {templateName} of inputs) {\n            if (templateName in (this.context ?? {})) {\n                this.c?.setInput(templateName, this.context?.[templateName as keyof C]);\n            }\n        }\n    }\n}\n\nfunction isDirective<C>(\n    content: PolymorpheusContent<C>,\n): content is PolymorpheusTemplate<C> {\n    return content instanceof PolymorpheusTemplate;\n}\n\nfunction isComponent<C>(\n    content: PolymorpheusContent<C>,\n): content is PolymorpheusComponent<any> {\n    return content instanceof PolymorpheusComponent;\n}\n\nfunction isTemplate<C>(\n    content: PolymorpheusContent<C>,\n): content is PolymorpheusTemplate<C> | TemplateRef<C> {\n    return isDirective(content) || content instanceof TemplateRef;\n}\n\nfunction ensureContext<C>(\n    context?: C | PolymorpheusContext<any>,\n): C | PolymorpheusContext<any> | undefined {\n    return isPrimitive(context) ? new PolymorpheusContext(context) : context;\n}\n","/**\n * Public API Surface of ng-polymorpheus\n */\nexport * from './classes/component';\nexport * from './directives/outlet';\nexport * from './directives/template';\nexport * from './tokens/context';\nexport type * from './types/content';\nexport type * from './types/handler';\nexport type * from './types/primitive';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAAA;AAGA;;;AAGG;AACI,MAAM,oBAAoB,GAAG,IAAI,cAAc,CAClD,SAAS,GAAG,sBAAsB,GAAG,EAAE;AAGrC,SAAU,cAAc,CAAuB,QAAW,EAAA;IAC5D,OAAO;AACH,QAAA,OAAO,EAAE,oBAAoB;QAC7B,QAAQ;KACX;AACL;AAIM,SAAU,aAAa,CAAI,OAAA,GAAyB,EAAE,EAAA;AACxD,IAAA,OAAO,MAAM,CAAC,oBAAoB,EAAE,OAAO,CAAC;AAChD;;AClBA;;;;;AAKG;MACU,qBAAqB,CAAA;IAC9B,WAAA,CACoB,SAAkB,EACjB,CAAY,EAAA;QADb,IAAA,CAAA,SAAS,GAAT,SAAS;QACR,IAAA,CAAA,CAAC,GAAD,CAAC;IACnB;IAEI,cAAc,CAAI,QAAkB,EAAE,QAAY,EAAA;QACrD,OAAO,QAAQ,CAAC,MAAM,CAAC;AACnB,YAAA,MAAM,EAAE,IAAI,CAAC,CAAC,IAAI,QAAQ;AAC1B,YAAA,SAAS,EAAE,QAAQ,IAAI,IAAI,GAAG,EAAE,GAAG,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;AAChE,SAAA,CA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