import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, NgZone, inject, signal } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { registerStreamingSlot } from './core/streamingSlotRegistrar.js';
import * as i0 from "@angular/core";
const resolvePayloadHtml = (payload) => {
    if (payload &&
        typeof payload === 'object' &&
        typeof payload.html === 'string') {
        return payload.html;
    }
    return typeof payload === 'string' ? payload : '';
};
export class StreamSlotComponent {
    constructor() {
        this.cdr = inject(ChangeDetectorRef);
        this.sanitizer = inject(DomSanitizer);
        this.zone = inject(NgZone);
        this.slotConsumer = (payload) => {
            this.zone.run(() => {
                this.currentHtml.set(this.sanitizer.bypassSecurityTrustHtml(resolvePayloadHtml(payload)));
                this.cdr.markForCheck();
            });
            return true;
        };
        this.fallbackHtml = '';
        this.currentHtml = signal('', ...(ngDevMode ? [{ debugName: "currentHtml" }] : /* istanbul ignore next */ []));
    }
    ngOnInit() {
        if (typeof window === 'undefined') {
            this.currentHtml.set(this.sanitizer.bypassSecurityTrustHtml(this.fallbackHtml));
            registerStreamingSlot({
                errorHtml: this.errorHtml,
                fallbackHtml: this.fallbackHtml,
                id: this.id,
                resolve: this.resolve,
                timeoutMs: this.timeoutMs
            });
            return;
        }
        const consumers = (window.__ABS_SLOT_CONSUMERS__ =
            window.__ABS_SLOT_CONSUMERS__ ?? {});
        consumers[this.id] = this.slotConsumer;
        this.currentHtml.set(this.sanitizer.bypassSecurityTrustHtml(this.fallbackHtml));
        const pendingPayload = window.__ABS_SLOT_PENDING__?.[this.id];
        if (pendingPayload !== undefined) {
            this.slotConsumer(pendingPayload);
            delete window.__ABS_SLOT_PENDING__?.[this.id];
        }
    }
    ngOnDestroy() {
        if (typeof window === 'undefined')
            return;
        if (window.__ABS_SLOT_CONSUMERS__) {
            delete window.__ABS_SLOT_CONSUMERS__[this.id];
        }
    }
    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]="id"
			[attr.class]="className"
			data-absolute-raw-slot="true"
			data-absolute-slot="true"
			[innerHTML]="currentHtml()"
		></div>
	`, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.6", ngImport: i0, type: StreamSlotComponent, decorators: [{
            type: Component,
            args: [{
                    changeDetection: ChangeDetectionStrategy.OnPush,
                    selector: 'abs-stream-slot',
                    standalone: true,
                    template: `
		<div
			[attr.id]="id"
			[attr.class]="className"
			data-absolute-raw-slot="true"
			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
            }] } });
