import * as tslib_1 from "tslib";
import { Injector, Injectable, TemplateRef, RendererFactory2, ComponentFactoryResolver } from '@angular/core';
import { Subscription } from 'rxjs';
/**
 * Class that allows for inserting Text or TemplateRef.
 */
var ContentRef = /** @class */ (function () {
    function ContentRef(nodes, viewRef, _componentRef) {
        this.nodes = nodes;
        this.viewRef = viewRef;
        this._componentRef = _componentRef;
    }
    return ContentRef;
}());
export { ContentRef };
var DynamicComponentService = /** @class */ (function () {
    function DynamicComponentService(_injector, rendererFactory, _componentFactoryResolver) {
        this._injector = _injector;
        this.rendererFactory = rendererFactory;
        this._componentFactoryResolver = _componentFactoryResolver;
        /** Subscription to viewport resize events. */
        this._resizeSubscription = Subscription.EMPTY;
        this._renderer = this.rendererFactory.createRenderer(null, null);
    }
    /**
     * create the given component or text to DOM element using the ComponentFactoryResolver.
     *
     * @param { any } type
     * @param { string | TemplateRef<any> } content
     * @param { Element } hostDomElement
     * @param { any } context
     * @return { ComponentRef<T> } _componentRef
     */
    DynamicComponentService.prototype.createDynamicComponent = function (viewContainerRef, type, content, hostDomElement, context) {
        this._viewContainerRef = viewContainerRef;
        var windowFactory;
        if (!this._componentRef) {
            this._contentRef = this._getContentRef(content, context);
            windowFactory = this._componentFactoryResolver.resolveComponentFactory(type);
            this._componentRef =
                this._viewContainerRef.createComponent(windowFactory, 0, this._injector, this._contentRef.nodes);
        }
        if (hostDomElement) {
            var overlayRootNode = this.getComponentRootNode(this._componentRef);
            hostDomElement.appendChild(overlayRootNode);
        }
        return this._componentRef;
    };
    /**
     * dispose the created component and remove from the body and unsubscribe event
     */
    DynamicComponentService.prototype.dispose = function () {
        if (this._componentRef) {
            this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._componentRef.hostView));
            this._componentRef = null;
            var viewRef = this._contentRef.viewRef;
            if (viewRef) {
                this._viewContainerRef.remove(this._viewContainerRef.indexOf(viewRef));
                this._contentRef = null;
            }
        }
        this._resizeSubscription.unsubscribe();
    };
    /**
     * get content from given content
     * @param { string | TemplateRef<any> } content
     * @param { any } context
     * @return { ContentRef }
     */
    DynamicComponentService.prototype._getContentRef = function (content, context) {
        if (!content) {
            return new ContentRef([]);
        }
        else if (content instanceof TemplateRef) {
            var viewRef = this._viewContainerRef.createEmbeddedView(content, context);
            return new ContentRef([viewRef.rootNodes], viewRef);
        }
        else {
            return new ContentRef([[this._renderer.createText("" + content)]]);
        }
    };
    /**
     * get component's root node for appending given element
     * @param { string | TemplateRef<any> } content
     * @return { HTMLElement }
     */
    DynamicComponentService.prototype.getComponentRootNode = function (_componentRef) {
        return _componentRef.hostView.rootNodes[0];
    };
    DynamicComponentService = tslib_1.__decorate([
        Injectable(),
        tslib_1.__metadata("design:paramtypes", [Injector,
            RendererFactory2,
            ComponentFactoryResolver])
    ], DynamicComponentService);
    return DynamicComponentService;
}());
export { DynamicComponentService };
//# sourceMappingURL=dynamic-component.service.js.map