import * as i0 from '@angular/core';
import { ViewContainerRef, ElementRef, Directive, ViewChild, Input } from '@angular/core';
import * as i1 from '@angular/platform-browser';

const _c0 = ["dynamicContent"];
const _c1 = ["elementContent"];
class DynamicComponentsBaseComponent {
    set elementContent(value) {
        this.elementContainer = value;
        this.renderNativeElement();
    }
    set element(node) {
        this.nativeElement = node;
        this.renderNativeElement();
    }
    // --- instead deprecated params
    set configuration(data) {
        if (data?.component?.type) {
            this.component = data.component.type;
            this.componentData = data.component.data;
            this.updateComponent(true);
        }
        else if (data?.templateRef) {
            this.templateRef = data.templateRef;
        }
        else if (data?.element) {
            this.nativeElement = data.element;
            this.renderNativeElement();
        }
        else if (data?.htmlSnippet) {
            this.htmlSnippet = this.sanitizer.bypassSecurityTrustHtml(data.htmlSnippet);
        }
    }
    constructor(renderer, sanitizer) {
        this.renderer = renderer;
        this.sanitizer = sanitizer;
        this.componentData = {};
        this.isViewInitialized = false;
    }
    updateComponent(isComponentChanged) {
        if (!this.isViewInitialized) {
            return;
        }
        if (!this.cmpRef || isComponentChanged) {
            if (isComponentChanged) {
                this.cmpRef.destroy();
            }
            this.cmpRef = this.dynamicData.createComponent(this.component);
        }
        this.onComponentChanges();
    }
    ngOnChanges(changes) {
        const isComponentChanged = (changes.component && changes.component.previousValue !== changes.component.currentValue && !changes.component.firstChange) ||
            (changes.componentData &&
                changes.componentData.previousValue !== changes.componentData.currentValue &&
                !changes.componentData.firstChange);
        this.updateComponent(isComponentChanged);
    }
    ngOnInit() {
        if (this.component) {
            this.isViewInitialized = true;
            this.updateComponent();
        }
    }
    onComponentChanges() {
        if (this.componentData) {
            Object.keys(this.componentData).forEach(key => {
                this.cmpRef.instance[key] = this.componentData[key];
                // region fix for dynamic component with formControl
                if (key === 'formControl' && typeof this.cmpRef?.instance['registerOnChange'] === 'function') {
                    this.cmpRef.instance['registerOnChange']((val) => {
                        this.componentData.formControl.setValue(val);
                    });
                    if (typeof this.cmpRef?.instance['writeValue'] === 'function') {
                        this.cmpRef.instance['writeValue'](this.componentData.formControl.value);
                    }
                }
                // endregion
            });
        }
    }
    ngOnDestroy() {
        if (this.cmpRef) {
            this.cmpRef.destroy();
        }
        this.clearContent();
    }
    renderNativeElement() {
        this.clearContent();
        if (this.nativeElement && this.elementContainer && this.elementContainer.nativeElement) {
            const parent = this.elementContainer.nativeElement;
            this.renderer.appendChild(parent, this.nativeElement);
        }
    }
    clearContent() {
        const nativeElem = this.elementContainer ? this.elementContainer.nativeElement : null;
        while (nativeElem && nativeElem.firstChild) {
            this.renderer.removeChild(nativeElem, nativeElem.firstChild);
        }
    }
    static { this.ɵfac = function DynamicComponentsBaseComponent_Factory(t) { return new (t || DynamicComponentsBaseComponent)(i0.ɵɵdirectiveInject(i0.Renderer2), i0.ɵɵdirectiveInject(i1.DomSanitizer)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: DynamicComponentsBaseComponent, viewQuery: function DynamicComponentsBaseComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 7, ViewContainerRef);
            i0.ɵɵviewQuery(_c1, 5, ElementRef);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.dynamicData = _t.first);
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.elementContent = _t.first);
        } }, inputs: { component: "component", componentData: "componentData", element: "element", configuration: "configuration" }, features: [i0.ɵɵNgOnChangesFeature] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(DynamicComponentsBaseComponent, [{
        type: Directive
    }], () => [{ type: i0.Renderer2 }, { type: i1.DomSanitizer }], { dynamicData: [{
            type: ViewChild,
            args: ['dynamicContent', { read: ViewContainerRef, static: true }]
        }], elementContent: [{
            type: ViewChild,
            args: ['elementContent', { read: ElementRef }]
        }], component: [{
            type: Input
        }], componentData: [{
            type: Input
        }], element: [{
            type: Input
        }], configuration: [{
            type: Input
        }] }); })();

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

export { DynamicComponentsBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-dynamic-components-common-base.mjs.map
