import { NgTemplateOutlet } from '@angular/common';
import * as i0 from '@angular/core';
import { input, inject, TemplateRef, Directive, contentChildren, computed, Component, ChangeDetectionStrategy, Renderer2, viewChildren, effect, afterNextRender, NgModule } from '@angular/core';

/**
 * Directive for defining a dynamic translation slot.
 */
class KbqDynamicTranslationSlot {
    constructor() {
        /**
         * The name of the dynamic translation slot.
         */
        this.name = input.required({ alias: 'kbqDynamicTranslationSlot' });
        /**
         * @docs-private
         */
        this.templateRef = inject(TemplateRef);
    }
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationSlot, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
    /** @nocollapse */ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "18.2.14", type: KbqDynamicTranslationSlot, isStandalone: true, selector: "[kbqDynamicTranslationSlot]", inputs: { name: { classPropertyName: "name", publicName: "kbqDynamicTranslationSlot", isSignal: true, isRequired: true, transformFunction: null } }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationSlot, decorators: [{
            type: Directive,
            args: [{
                    standalone: true,
                    selector: '[kbqDynamicTranslationSlot]'
                }]
        }] });
/**
 * Component for dynamic translation.
 */
class KbqDynamicTranslation {
    constructor() {
        this.slots = contentChildren(KbqDynamicTranslationSlot);
        /**
         * The text which will be translated.
         */
        this.text = input.required();
        /**
         * @docs-private
         */
        this.parsedSlots = computed(() => this.parseSlots(this.text(), this.slots()));
    }
    parseSlots(text, slots) {
        const slotTemplatesByName = slots.reduce((map, { name, templateRef }) => {
            const slotName = name();
            if (slotName)
                map[slotName] = templateRef;
            return map;
        }, {});
        const parsedSlots = [];
        const slotSelector = new RegExp(`\\[\\[(${Object.keys(slotTemplatesByName).join('|')})(?::(.+?))?\\]\\]`, 'g');
        let match;
        let lastIndex = 0;
        while ((match = slotSelector.exec(text)) !== null) {
            const [slotMatch, slotName, slotText] = match;
            const [listMatch, listString] = new RegExp('^\\((.*?)\\)$', 'g').exec(slotText) ?? [];
            const textBeforeSlot = text.substring(lastIndex, +match.index);
            if (textBeforeSlot) {
                parsedSlots.push({ type: 'text', text: textBeforeSlot, template: null });
            }
            parsedSlots.push({
                type: 'slot',
                template: slotTemplatesByName[slotName] || null,
                context: { $implicit: listMatch && listString ? listString.split(',') : slotText }
            });
            lastIndex = match.index + slotMatch.length;
        }
        const textAfterLastSlot = text.substring(lastIndex);
        if (textAfterLastSlot) {
            parsedSlots.push({ type: 'text', text: textAfterLastSlot, template: null });
        }
        return parsedSlots;
    }
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslation, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    /** @nocollapse */ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "18.2.14", type: KbqDynamicTranslation, isStandalone: true, selector: "kbq-dynamic-translation", inputs: { text: { classPropertyName: "text", publicName: "text", isSignal: true, isRequired: true, transformFunction: null } }, queries: [{ propertyName: "slots", predicate: KbqDynamicTranslationSlot, isSignal: true }], ngImport: i0, template: `
        @for (slot of parsedSlots(); track $index) {
            @switch (slot.type) {
                @case ('slot') {
                    <ng-container *ngTemplateOutlet="slot.template; context: slot.context" />
                }
                @default {
                    <ng-container>{{ slot.text }}</ng-container>
                }
            }
        }
    `, isInline: true, dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslation, decorators: [{
            type: Component,
            args: [{
                    standalone: true,
                    imports: [NgTemplateOutlet],
                    selector: 'kbq-dynamic-translation',
                    template: `
        @for (slot of parsedSlots(); track $index) {
            @switch (slot.type) {
                @case ('slot') {
                    <ng-container *ngTemplateOutlet="slot.template; context: slot.context" />
                }
                @default {
                    <ng-container>{{ slot.text }}</ng-container>
                }
            }
        }
    `,
                    changeDetection: ChangeDetectionStrategy.OnPush
                }]
        }] });
/**
 * @docs-private
 */
class KbqDynamicTranslationHelper {
    constructor() {
        this.renderer = inject(Renderer2);
        this.replaceableSlotContainers = viewChildren('replaceableSlotContainer');
        this.slotsByName = new Map();
        this.text = input.required();
        this.slots = input.required();
        effect(() => {
            this.slotsByName.clear();
            this.slots().forEach((slot) => this.slotsByName.set(slot.name, slot));
        });
        afterNextRender(() => {
            this.replaceSlots();
        });
    }
    replaceSlots() {
        this.replaceableSlotContainers().forEach(({ nativeElement }) => {
            const slotName = nativeElement.getAttribute('data-slot-name');
            const slotContent = nativeElement.getAttribute('data-slot-content');
            const { tag, attributes } = this.slotsByName.get(slotName);
            const elementToRender = this.renderer.createElement(tag);
            this.renderer.appendChild(elementToRender, this.renderer.createText(slotContent));
            attributes?.forEach(({ key, value }) => this.renderer.setAttribute(elementToRender, key, value));
            nativeElement.replaceWith(elementToRender);
        });
    }
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationHelper, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    /** @nocollapse */ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "18.2.14", type: KbqDynamicTranslationHelper, isStandalone: true, selector: "kbq-dynamic-translation-with-dynamic-component-creation", inputs: { text: { classPropertyName: "text", publicName: "text", isSignal: true, isRequired: true, transformFunction: null }, slots: { classPropertyName: "slots", publicName: "slots", isSignal: true, isRequired: true, transformFunction: null } }, viewQueries: [{ propertyName: "replaceableSlotContainers", predicate: ["replaceableSlotContainer"], descendants: true, isSignal: true }], ngImport: i0, template: `
        <kbq-dynamic-translation [text]="text()">
            @for (slot of slots(); track $index) {
                <ng-container *kbqDynamicTranslationSlot="slot.name; let content">
                    <span
                        #replaceableSlotContainer
                        [attr.data-slot-name]="slot.name"
                        [attr.data-slot-content]="slot.content || content"
                    ></span>
                </ng-container>
            }
        </kbq-dynamic-translation>
    `, isInline: true, dependencies: [{ kind: "component", type: KbqDynamicTranslation, selector: "kbq-dynamic-translation", inputs: ["text"] }, { kind: "directive", type: KbqDynamicTranslationSlot, selector: "[kbqDynamicTranslationSlot]", inputs: ["kbqDynamicTranslationSlot"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationHelper, decorators: [{
            type: Component,
            args: [{
                    standalone: true,
                    imports: [KbqDynamicTranslation, KbqDynamicTranslationSlot],
                    selector: 'kbq-dynamic-translation-with-dynamic-component-creation',
                    template: `
        <kbq-dynamic-translation [text]="text()">
            @for (slot of slots(); track $index) {
                <ng-container *kbqDynamicTranslationSlot="slot.name; let content">
                    <span
                        #replaceableSlotContainer
                        [attr.data-slot-name]="slot.name"
                        [attr.data-slot-content]="slot.content || content"
                    ></span>
                </ng-container>
            }
        </kbq-dynamic-translation>
    `,
                    changeDetection: ChangeDetectionStrategy.OnPush
                }]
        }], ctorParameters: () => [] });

const COMPONENTS = [
    KbqDynamicTranslationSlot,
    KbqDynamicTranslationHelper,
    KbqDynamicTranslation
];
class KbqDynamicTranslationModule {
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    /** @nocollapse */ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationModule, imports: [KbqDynamicTranslationSlot,
            KbqDynamicTranslationHelper,
            KbqDynamicTranslation], exports: [KbqDynamicTranslationSlot,
            KbqDynamicTranslationHelper,
            KbqDynamicTranslation] }); }
    /** @nocollapse */ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqDynamicTranslationModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: COMPONENTS,
                    exports: COMPONENTS
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { KbqDynamicTranslation, KbqDynamicTranslationHelper, KbqDynamicTranslationModule, KbqDynamicTranslationSlot };
//# sourceMappingURL=koobiq-components-dynamic-translation.mjs.map
