/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { EventEmitter, ApplicationRef, Component, ViewChild, ChangeDetectionStrategy, ViewContainerRef } from '@angular/core';
import { TypeRegistrar } from './type';
import * as i0 from "@angular/core";
var IgxComponentRendererContainerComponent = /** @class */ /*@__PURE__*/ (function () {
    function IgxComponentRendererContainerComponent(_renderer, _elRef, _ngZone, _componentFactoryResolver, _injector) {
        this._renderer = _renderer;
        this._elRef = _elRef;
        this._ngZone = _ngZone;
        this._componentFactoryResolver = _componentFactoryResolver;
        this._injector = _injector;
        this.listeners = [];
        this._currentRoot = null;
        this._currentRootRef = null;
    }
    IgxComponentRendererContainerComponent.isEvent = function (ev) {
        return ev instanceof EventEmitter;
    };
    IgxComponentRendererContainerComponent.prototype.determineInjector = function (origName, typeName, type) {
        return this._currentRootRef.injector;
    };
    IgxComponentRendererContainerComponent.prototype.getSpecificAnchor = function (origName, typeName, type, context) {
        var _a;
        var meta = context.getMetadata(origName, "__mustUseNGParentAnchor");
        if (meta) {
            return (_a = this._currentRootRef.instance.anchor) !== null && _a !== void 0 ? _a : this._currentRootRef.instance.___viewContainerRef;
        }
        return null;
    };
    IgxComponentRendererContainerComponent.prototype.createObject = function (type, container, context) {
        var origName = type;
        var meta = context.getMetadata(type, "__qualifiedNameTS");
        if (meta) {
            type = meta.specificExternalType;
        }
        var meta = context.getMetadata(origName, "__qualifiedNameNG");
        if (meta) {
            type = meta.specificExternalType;
        }
        var typeName = "Igx" + type + "Component";
        if (TypeRegistrar.isRegistered(typeName)) {
            var C = TypeRegistrar.get(typeName);
            var cf = this._componentFactoryResolver.resolveComponentFactory(C);
            var injector = this.determineInjector(origName, typeName, C);
            var parentViewRef = injector.get(ViewContainerRef);
            var cr = cf.create(parentViewRef.injector);
            cr.instance.___owningRef = cr;
            cr.instance.___parentViewRef = parentViewRef;
            cr.instance.___hostView = cr.hostView;
            var specificAnchor = this.getSpecificAnchor(origName, typeName, C, context);
            if (!specificAnchor) {
                var appRef = parentViewRef.injector.get(ApplicationRef);
                appRef.attachView(cr.hostView);
                this._currentRootRef.changeDetectorRef.detectChanges();
                cr.changeDetectorRef.detectChanges();
            }
            else {
                var domParent = cr.location.nativeElement.parentElement;
                if (domParent) {
                    var nextSibling = cr.location.nativeElement.nextSibling;
                    specificAnchor.insert(cr.hostView);
                    domParent.insertBefore(cr.hostView, nextSibling);
                }
                else {
                    specificAnchor.insert(cr.hostView);
                }
                this._currentRootRef.changeDetectorRef.detectChanges();
            }
            return cr.instance;
        }
        return null;
    };
    IgxComponentRendererContainerComponent.prototype.replaceRootItem = function (t, deferAttach, continueActions) {
        if (this._currentRoot != null) {
            this._currentRoot;
        }
        var cf = this._componentFactoryResolver.resolveComponentFactory(t);
        var cr = this._dynamicContent.createComponent(cf);
        this._currentRootRef = cr;
        this._currentRoot = cr.instance;
        cr.instance.___owningRef = cr;
        cr.instance.___viewContainerRef = cr.injector.get(ViewContainerRef);
        this._currentRoot.width = "100%";
        this._currentRoot.height = "100%";
        continueActions(false);
    };
    IgxComponentRendererContainerComponent.prototype.clearContainer = function (continueActions) {
        if (this._currentRootRef) {
            this._currentRootRef.destroy();
            this._currentRootRef = null;
            this._currentRoot = null;
        }
        continueActions(false);
    };
    IgxComponentRendererContainerComponent.prototype.getRootObject = function () {
        return this._currentRoot;
    };
    IgxComponentRendererContainerComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: IgxComponentRendererContainerComponent, deps: [{ token: i0.Renderer2 }, { token: i0.ViewContainerRef }, { token: i0.NgZone }, { token: i0.ComponentFactoryResolver }, { token: i0.Injector }], target: i0.ɵɵFactoryTarget.Component });
    IgxComponentRendererContainerComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.2.17", type: IgxComponentRendererContainerComponent, selector: "igx-component-renderer-container", viewQueries: [{ propertyName: "_dynamicContent", first: true, predicate: ["dynamicContent"], descendants: true, read: ViewContainerRef, static: true }], ngImport: i0, template: "\n    <ng-container #dynamicContent></ng-container>\n  ", isInline: true, styles: ["\n  :host {\n    display: block;\n  }"], changeDetection: i0.ChangeDetectionStrategy.OnPush });
    return IgxComponentRendererContainerComponent;
}());
export { IgxComponentRendererContainerComponent };
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.17", ngImport: i0, type: IgxComponentRendererContainerComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'igx-component-renderer-container',
                    changeDetection: ChangeDetectionStrategy.OnPush,
                    template: "\n    <ng-container #dynamicContent></ng-container>\n  ",
                    styles: ["\n  :host {\n    display: block;\n  }"]
                }]
        }], ctorParameters: function () { return [{ type: i0.Renderer2 }, { type: i0.ViewContainerRef }, { type: i0.NgZone }, { type: i0.ComponentFactoryResolver }, { type: i0.Injector }]; }, propDecorators: { _dynamicContent: [{
                type: ViewChild,
                args: ["dynamicContent", { read: ViewContainerRef, static: true }]
            }] } });
