import { Component, ElementRef, Input, inject, signal } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { registerStreamingSlot } from './core/streamingSlotRegistrar.js';
import * as i0 from "@angular/core";
const SLOT_PATCH_EVENT = 'absolutejs:slot-patch';
export class StreamSlotComponent {
    constructor() {
        this.sanitizer = inject(DomSanitizer);
        this.hostElement = inject((ElementRef));
        this.patchListener = (event) => {
            const detail = event.detail;
            if (!detail || detail.id !== this.id || typeof detail.html !== 'string') {
                return;
            }
            this.currentHtml.set(this.sanitizer.bypassSecurityTrustHtml(detail.html));
        };
        this.fallbackHtml = '';
        this.currentHtml = signal('', ...(ngDevMode ? [{ debugName: "currentHtml" }] : /* istanbul ignore next */ []));
    }
    ngOnInit() {
        this.currentHtml.set(this.sanitizer.bypassSecurityTrustHtml(this.fallbackHtml));
        if (typeof window !== 'undefined')
            return;
        registerStreamingSlot({
            errorHtml: this.errorHtml,
            fallbackHtml: this.fallbackHtml,
            id: this.id,
            resolve: this.resolve,
            timeoutMs: this.timeoutMs
        });
    }
    ngAfterViewInit() {
        if (typeof window === 'undefined')
            return;
        window.addEventListener(SLOT_PATCH_EVENT, this.patchListener);
        const slotElement = this.hostElement.nativeElement.querySelector(`#slot-${this.id}`);
        const existingHtml = slotElement?.innerHTML;
        if (existingHtml && existingHtml !== this.fallbackHtml) {
            this.currentHtml.set(this.sanitizer.bypassSecurityTrustHtml(existingHtml));
        }
    }
    ngOnDestroy() {
        if (typeof window === 'undefined')
            return;
        window.removeEventListener(SLOT_PATCH_EVENT, this.patchListener);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.6", ngImport: i0, type: StreamSlotComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "21.2.6", type: StreamSlotComponent, isStandalone: true, selector: "abs-stream-slot", inputs: { className: "className", errorHtml: "errorHtml", fallbackHtml: "fallbackHtml", id: "id", resolve: "resolve", timeoutMs: "timeoutMs" }, ngImport: i0, template: `
		<div
			[attr.id]="'slot-' + id"
			[attr.class]="className"
			data-absolute-slot="true"
			[innerHTML]="currentHtml()"
		></div>
	`, isInline: true }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.6", ngImport: i0, type: StreamSlotComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'abs-stream-slot',
                    standalone: true,
                    template: `
		<div
			[attr.id]="'slot-' + id"
			[attr.class]="className"
			data-absolute-slot="true"
			[innerHTML]="currentHtml()"
		></div>
	`
                }]
        }], propDecorators: { className: [{
                type: Input
            }], errorHtml: [{
                type: Input
            }], fallbackHtml: [{
                type: Input
            }], id: [{
                type: Input,
                args: [{ required: true }]
            }], resolve: [{
                type: Input,
                args: [{ required: true }]
            }], timeoutMs: [{
                type: Input
            }] } });
