import { take, switchMap } from 'rxjs/operators';
import { Subject, fromEvent, race } from 'rxjs';
import { __decorate } from 'tslib';
import { EventEmitter, ElementRef, Input, Output, HostListener, Component, ChangeDetectionStrategy, ViewEncapsulation, HostBinding, ViewContainerRef, Directive, ChangeDetectorRef, ComponentFactoryResolver, ViewChild, Injector, ApplicationRef, Injectable, NgModule } from '@angular/core';
import { trigger, transition, group, query, style, animate } from '@angular/animations';
import { Patch } from '@bravobit/bb-foundation';
import { BbButton, ElementsModule } from '@bravobit/bb-foundation/elements';
import { LocalizeModule } from '@bravobit/bb-foundation/localize';
import { CommonModule } from '@angular/common';

var BbDialogRef = /** @class */ (function () {
    function BbDialogRef() {
        // Data.
        this._afterClosed$ = new Subject();
        this._overlayClicked$ = new Subject();
    }
    Object.defineProperty(BbDialogRef.prototype, "afterClosed", {
        get: function () {
            return this._afterClosed$.pipe(take(1));
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BbDialogRef.prototype, "overlayClicked", {
        get: function () {
            return this._overlayClicked$.pipe(take(1));
        },
        enumerable: true,
        configurable: true
    });
    BbDialogRef.prototype.onOverlayClicked = function () {
        this._overlayClicked$.next();
    };
    BbDialogRef.prototype.close = function (result) {
        this._afterClosed$.next(result);
    };
    return BbDialogRef;
}());

var BbDialogConfig = /** @class */ (function () {
    function BbDialogConfig() {
    }
    return BbDialogConfig;
}());

var BbDialogOverlay = /** @class */ (function () {
    function BbDialogOverlay(_elementRef) {
        this._elementRef = _elementRef;
        // Inputs.
        this.animations = true;
        // Outputs.
        this.closeRequested = new EventEmitter();
        // State.
        this._state = 'open';
        this._closed$ = new Subject();
    }
    Object.defineProperty(BbDialogOverlay.prototype, "state", {
        get: function () {
            return this.animations
                ? this._state
                : 'none';
        },
        enumerable: true,
        configurable: true
    });
    BbDialogOverlay.prototype.ngAfterViewInit = function () {
        this.listenForClicks();
    };
    BbDialogOverlay.prototype.ngOnDestroy = function () {
        this._subscription && this._subscription.unsubscribe();
    };
    BbDialogOverlay.prototype.onKeyEvent = function (event) {
        if (event.key === 'Escape') {
            this.closeRequested.emit();
        }
    };
    BbDialogOverlay.prototype.onAnimationDone = function (event) {
        // Check if the dialog is being destroyed.
        if (!['close', 'none'].includes(event.toState)) {
            return;
        }
        this._closed$.next();
        this._closed$.complete();
    };
    BbDialogOverlay.prototype.close = function () {
        this._state = 'close';
        return this._closed$.pipe(take(1)).toPromise();
    };
    BbDialogOverlay.prototype.listenForClicks = function () {
        var _this = this;
        if (!this._elementRef || !this._elementRef.nativeElement) {
            return;
        }
        var mouseDown$ = fromEvent(this._elementRef.nativeElement, 'mousedown');
        var touchStart$ = fromEvent(this._elementRef.nativeElement, 'touchstart');
        var start$ = race([mouseDown$, touchStart$]);
        var mouseUp$ = fromEvent(this._elementRef.nativeElement, 'mouseup');
        var touchEnd$ = fromEvent(this._elementRef.nativeElement, 'touchend');
        var end$ = race([mouseUp$, touchEnd$]);
        this._subscription = start$.pipe(switchMap(function () { return end$; })).subscribe(function () {
            _this.closeRequested.next();
        });
    };
    BbDialogOverlay.ctorParameters = function () { return [
        { type: ElementRef }
    ]; };
    __decorate([
        Input()
    ], BbDialogOverlay.prototype, "animations", void 0);
    __decorate([
        Output()
    ], BbDialogOverlay.prototype, "closeRequested", void 0);
    __decorate([
        HostListener('window:keyup', ['$event'])
    ], BbDialogOverlay.prototype, "onKeyEvent", null);
    __decorate([
        HostListener('@bbDialogAnimation.done', ['$event'])
    ], BbDialogOverlay.prototype, "onAnimationDone", null);
    BbDialogOverlay = __decorate([
        Component({
            selector: 'bb-dialog-overlay',
            template: "<div class=\"bb-dialog-overlay-backdrop\"></div>\n\n<ng-content select=\"[bb-dialog-modal]\"></ng-content>\n",
            changeDetection: ChangeDetectionStrategy.OnPush,
            encapsulation: ViewEncapsulation.None,
            preserveWhitespaces: false,
            host: { 'class': 'bb-dialog-overlay', '[@bbDialogAnimation]': 'state' },
            animations: [
                trigger('bbDialogAnimation', [
                    transition('* => none', []),
                    transition(':enter, * => open', [
                        group([
                            query('.bb-dialog-overlay-backdrop', [
                                style({ opacity: 0 }),
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({ opacity: 1 }))
                            ]),
                            query('.bb-dialog-modal', [
                                style({ transform: 'scale(0.95)' }),
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({ transform: 'scale(1)' }))
                            ])
                        ])
                    ]),
                    transition(':leave, * => close', [
                        group([
                            query('.bb-dialog-overlay-backdrop', [
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({ opacity: 0 }))
                            ]),
                            query('.bb-dialog-modal', [
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({ opacity: 0, transform: 'scale(0.95)' }))
                            ])
                        ])
                    ])
                ])
            ],
            styles: [".bb-dialog-overlay,.bb-dialog-overlay-backdrop{top:0;left:0;right:0;bottom:0;display:-webkit-box;display:-ms-flexbox;display:flex;z-index:1000;position:fixed;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column}.bb-dialog-overlay{padding:20px}.bb-dialog-overlay-backdrop{background-color:rgba(0,0,0,.5)}.bb-dialog-overlay-component{z-index:1001;display:block;position:relative}"]
        })
    ], BbDialogOverlay);
    return BbDialogOverlay;
}());

var BbDialogModal = /** @class */ (function () {
    function BbDialogModal(_patch) {
        this._patch = _patch;
        // Styling.
        this.maxWidth = '420px';
    }
    BbDialogModal.prototype.onOverlayClickedEvent = function (event) {
        event.stopPropagation();
    };
    BbDialogModal.prototype.ngOnInit = function () {
        this._patch.mobileVerticalHeight();
    };
    BbDialogModal.ctorParameters = function () { return [
        { type: Patch }
    ]; };
    __decorate([
        HostBinding('style.max-width'), Input()
    ], BbDialogModal.prototype, "maxWidth", void 0);
    __decorate([
        HostListener('click', ['$event']),
        HostListener('touchstart', ['$event']),
        HostListener('touchend', ['$event']),
        HostListener('mouseup', ['$event']),
        HostListener('mousedown', ['$event'])
    ], BbDialogModal.prototype, "onOverlayClickedEvent", null);
    BbDialogModal = __decorate([
        Component({
            selector: '[bb-dialog-modal]',
            template: "<ng-content select=\"[bb-dialog-header]\">\n\n</ng-content>\n\n<div class=\"bb-dialog-modal-body\">\n    <ng-content></ng-content>\n</div>\n\n<ng-content select=\"[bb-dialog-actions]\">\n\n</ng-content>\n",
            changeDetection: ChangeDetectionStrategy.OnPush,
            encapsulation: ViewEncapsulation.None,
            preserveWhitespaces: false,
            host: {
                'class': 'bb-dialog-modal',
                'role': 'dialog'
            },
            styles: [".bb-dialog-modal{width:100%;margin:auto;z-index:1001;display:-webkit-box;display:-ms-flexbox;display:flex;max-width:420px;border-radius:4px;position:relative;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;background-color:#fff;max-height:calc((var(--vh,1vh) * 100) - 40px)}.bb-dialog-modal-body{padding:20px;color:#738694;font-size:13px;overflow-y:auto;line-height:1.4;background-color:#f1f3f6}"]
        })
    ], BbDialogModal);
    return BbDialogModal;
}());

var BbDialogHeader = /** @class */ (function () {
    function BbDialogHeader() {
        // Inputs.
        this.iconName = null;
        this.iconColor = 'black';
        // Outputs.
        this.closeRequested = new EventEmitter();
    }
    __decorate([
        Input()
    ], BbDialogHeader.prototype, "iconName", void 0);
    __decorate([
        Input()
    ], BbDialogHeader.prototype, "iconColor", void 0);
    __decorate([
        Output()
    ], BbDialogHeader.prototype, "closeRequested", void 0);
    BbDialogHeader = __decorate([
        Component({
            selector: '[bb-dialog-header]',
            template: "<div class=\"bb-dialog-header-row\">\n    <div *ngIf=\"iconName as icon\"\n         [style.color]=\"iconColor\"\n         class=\"bb-dialog-header-icon\">\n        <i class=\"material-icons\">{{ icon }}</i>\n    </div>\n\n    <div class=\"bb-dialog-header-content\">\n        <ng-content></ng-content>\n    </div>\n\n    <button *ngIf=\"closeRequested?.observers?.length > 0\"\n            (click)=\"closeRequested?.emit()\"\n            type=\"button\"\n            class=\"bb-dialog-header-close\">\n    </button>\n</div>\n\n<nav class=\"bb-dialog-header-navigation\">\n    <ng-content select=\"[bb-dialog-link]\"></ng-content>\n</nav>\n",
            changeDetection: ChangeDetectionStrategy.OnPush,
            encapsulation: ViewEncapsulation.None,
            preserveWhitespaces: false,
            host: { 'class': 'bb-dialog-header' },
            styles: [".bb-dialog-header{display:-webkit-box;display:-ms-flexbox;display:flex;min-height:60px;-webkit-box-orient:vertical;-webkit-box-direction:normal;-ms-flex-direction:column;flex-direction:column;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;background-color:#fff;border-top-left-radius:4px;border-top-right-radius:4px;border-bottom:1px solid #d8d8d8}.bb-dialog-header .bb-dialog-header-content,.bb-dialog-header .bb-dialog-header-content h1{max-width:100%;font-size:20px;font-weight:500;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}.bb-dialog-header-navigation,.bb-dialog-header-row{width:100%;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bb-dialog-header-row{padding:10px 20px}.bb-dialog-header-navigation{width:100%;padding:0 20px;overflow-x:auto;white-space:nowrap}.bb-dialog-header-navigation:empty{margin:0;height:0;padding:0;visibility:hidden}.bb-dialog-header-content{-webkit-box-flex:1;-ms-flex:1;flex:1}.bb-dialog-header-icon{width:40px;height:40px;display:-webkit-box;display:-ms-flexbox;display:flex;min-width:40px;overflow:hidden;min-height:40px;margin-right:10px;border-radius:50%;position:relative;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center}.bb-dialog-header-icon:after{top:0;left:0;right:0;bottom:0;content:\"\";opacity:.14;position:absolute;background-color:currentColor}.bb-dialog-header-close{width:32px;height:32px;border:none;min-width:32px;min-height:32px;overflow:hidden;margin-left:10px;border-radius:4px;position:relative;margin-right:-10px;display:inline-block;background-color:transparent}.bb-dialog-header-close:after,.bb-dialog-header-close:before{top:50%;left:20%;width:60%;height:2px;content:\"\";margin-top:-1px;position:absolute;background-color:#a2a9ba}.bb-dialog-header-close:before{-webkit-transform:rotate(45deg);-ms-transform:rotate(45deg);transform:rotate(45deg)}.bb-dialog-header-close:after{-webkit-transform:rotate(-45deg);-ms-transform:rotate(-45deg);transform:rotate(-45deg)}.bb-dialog-header-close:hover{background-color:rgba(0,0,0,.08)}.bb-dialog-header-close:focus{background-color:rgba(0,0,0,.12)}.bb-dialog-header-close:active{background-color:rgba(0,0,0,.16)}"]
        })
    ], BbDialogHeader);
    return BbDialogHeader;
}());

var BbDialogLink = /** @class */ (function () {
    function BbDialogLink() {
    }
    BbDialogLink = __decorate([
        Component({
            selector: '[bb-dialog-link]',
            template: "<ng-content></ng-content>\n",
            changeDetection: ChangeDetectionStrategy.OnPush,
            encapsulation: ViewEncapsulation.None,
            preserveWhitespaces: false,
            host: { 'class': 'bb-dialog-link' },
            styles: [".bb-dialog-link{height:30px;display:-webkit-box;display:-ms-flexbox;display:flex;font-size:16px;font-weight:500;white-space:nowrap;-webkit-box-align:center;-ms-flex-align:center;align-items:center;color:#6b5c5c!important;background-color:transparent;text-decoration:none!important;border-bottom:2px solid transparent}.bb-dialog-link+.bb-dialog-link{margin-left:10px}.bb-dialog-link:hover:not(.disabled){border-bottom:2px solid rgba(117,119,148,.2)}.bb-dialog-link.active,.bb-dialog-link.active:hover{color:#5b53ff!important;border-bottom-color:#5b53ff}.bb-dialog-link.disabled,.bb-dialog-link.disabled:active,.bb-dialog-link.disabled:focus,.bb-dialog-link.disabled:hover{opacity:.4;cursor:default;box-shadow:none}"]
        })
    ], BbDialogLink);
    return BbDialogLink;
}());

var BbDialogActions = /** @class */ (function () {
    function BbDialogActions() {
    }
    BbDialogActions = __decorate([
        Component({
            selector: '[bb-dialog-actions]',
            template: "<bb-form-group class=\"end\">\n    <ng-content></ng-content>\n</bb-form-group>\n",
            changeDetection: ChangeDetectionStrategy.OnPush,
            encapsulation: ViewEncapsulation.None,
            preserveWhitespaces: false,
            host: { 'class': 'bb-dialog-actions' },
            styles: [".bb-dialog-actions{padding:20px;display:block;background-color:#fff;border-top:1px solid #d8d8d8;border-bottom-left-radius:4px;border-bottom-right-radius:4px}"]
        })
    ], BbDialogActions);
    return BbDialogActions;
}());

var BbDialogInsertion = /** @class */ (function () {
    function BbDialogInsertion(viewContainerRef) {
        this.viewContainerRef = viewContainerRef;
    }
    BbDialogInsertion.ctorParameters = function () { return [
        { type: ViewContainerRef }
    ]; };
    BbDialogInsertion = __decorate([
        Directive({
            selector: '[bbDialogInsertion]'
        })
    ], BbDialogInsertion);
    return BbDialogInsertion;
}());

var BbDialogContainer = /** @class */ (function () {
    function BbDialogContainer(_elementRef, _changeDetection, _componentFactoryResolver) {
        this._elementRef = _elementRef;
        this._changeDetection = _changeDetection;
        this._componentFactoryResolver = _componentFactoryResolver;
        // Data.
        this.componentRef = null;
        this.childComponentType = null;
        // State.
        this.overlayClicked$ = new Subject();
    }
    Object.defineProperty(BbDialogContainer.prototype, "onOverlayClicked", {
        get: function () {
            return this.overlayClicked$.pipe(take(1));
        },
        enumerable: true,
        configurable: true
    });
    BbDialogContainer.prototype.ngAfterViewInit = function () {
        // Load the child component.
        this.loadChildComponent(this.childComponentType);
        // Listen for mouse click on the dialog container.
        this.listenForClicks();
        // Detect changes to notify change detection.
        this._changeDetection.detectChanges();
    };
    BbDialogContainer.prototype.ngOnDestroy = function () {
        this._subscription && this._subscription.unsubscribe();
    };
    BbDialogContainer.prototype.onAnimationDone = function (event) {
        // Check if the dialog is being destroyed.
        if (event.toState !== 'void') {
            return;
        }
        // Destroy the ref if it exists.
        this.componentRef && this.componentRef.destroy();
    };
    BbDialogContainer.prototype.listenForClicks = function () {
        var _this = this;
        if (!this._elementRef || !this._elementRef.nativeElement) {
            return;
        }
        var mouseDown$ = fromEvent(this._elementRef.nativeElement, 'mousedown');
        var touchStart$ = fromEvent(this._elementRef.nativeElement, 'touchstart');
        var start$ = race([mouseDown$, touchStart$]);
        var mouseUp$ = fromEvent(this._elementRef.nativeElement, 'mouseup');
        var touchEnd$ = fromEvent(this._elementRef.nativeElement, 'touchend');
        var end$ = race([mouseUp$, touchEnd$]);
        this._subscription = start$.pipe(switchMap(function () { return end$; })).subscribe(function () {
            _this.overlayClicked$.next();
        });
    };
    BbDialogContainer.prototype.loadChildComponent = function (componentType) {
        // Resolve the component.
        var componentFactory = this._componentFactoryResolver.resolveComponentFactory(componentType);
        // Get the view container ref and clear it.
        var viewContainerRef = this.insertion.viewContainerRef;
        viewContainerRef.clear();
        // Create an instance of the component.
        this.componentRef = viewContainerRef.createComponent(componentFactory);
    };
    BbDialogContainer.ctorParameters = function () { return [
        { type: ElementRef },
        { type: ChangeDetectorRef },
        { type: ComponentFactoryResolver }
    ]; };
    __decorate([
        ViewChild(BbDialogInsertion, { static: true })
    ], BbDialogContainer.prototype, "insertion", void 0);
    __decorate([
        HostListener('@bbDialogAnimation.done', ['$event'])
    ], BbDialogContainer.prototype, "onAnimationDone", null);
    BbDialogContainer = __decorate([
        Component({
            selector: 'bb-dialog-container',
            template: "<div class=\"bb-dialog-container-backdrop\">\n</div>\n\n<div class=\"bb-dialog-container-component\">\n    <ng-container bbDialogInsertion>\n    </ng-container>\n</div>\n",
            changeDetection: ChangeDetectionStrategy.OnPush,
            encapsulation: ViewEncapsulation.None,
            host: { 'class': 'bb-dialog-container', '[@bbDialogAnimation]': 'true' },
            preserveWhitespaces: false,
            animations: [
                trigger('bbDialogAnimation', [
                    transition(':enter', [
                        group([
                            query('.bb-dialog-container-backdrop', [
                                style({
                                    opacity: 0
                                }),
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({
                                    opacity: 1
                                }))
                            ]),
                            query('.bb-dialog-container-component', [
                                style({
                                    transform: 'scale(0.95)'
                                }),
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({
                                    transform: 'scale(1)'
                                }))
                            ])
                        ])
                    ]),
                    transition(':leave', [
                        group([
                            query('.bb-dialog-container-backdrop', [
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({
                                    opacity: 0
                                }))
                            ]),
                            query('.bb-dialog-container-component', [
                                animate('150ms cubic-bezier(0, 0, 0.2, 1)', style({
                                    opacity: 0,
                                    transform: 'scale(0.95)'
                                }))
                            ])
                        ])
                    ])
                ])
            ],
            styles: [".bb-dialog-container,.bb-dialog-container-backdrop{top:0;left:0;right:0;bottom:0;z-index:1000;position:fixed}.bb-dialog-container{display:-webkit-box;display:-ms-flexbox;display:flex}.bb-dialog-container-backdrop{background-color:rgba(0,0,0,.5)}.bb-dialog-container-component{width:100%;margin:auto;padding:20px;z-index:1001;display:block}"]
        })
    ], BbDialogContainer);
    return BbDialogContainer;
}());

var nextUniqueId = 0;
var BbDialogConfirm = /** @class */ (function () {
    function BbDialogConfirm(config, dialogRef) {
        this.config = config;
        this.dialogRef = dialogRef;
        // Id.
        this.dialogId = "bb-dialog-confirm-" + nextUniqueId++;
    }
    BbDialogConfirm.prototype.ngAfterViewInit = function () {
        // Initial focus the button.
        this.confirmButton.focus();
    };
    BbDialogConfirm.prototype.onClose = function (result) {
        this.dialogRef.close(result);
    };
    BbDialogConfirm.ctorParameters = function () { return [
        { type: BbDialogConfig },
        { type: BbDialogRef }
    ]; };
    __decorate([
        ViewChild('confirm', { read: BbButton })
    ], BbDialogConfirm.prototype, "confirmButton", void 0);
    BbDialogConfirm = __decorate([
        Component({
            selector: 'bb-dialog-confirm',
            template: "<div [attr.aria-labelledby]=\"dialogId\"\n     bb-dialog-modal\n     bbFocusTrap>\n    <header bb-dialog-header>\n        <h1 *ngIf=\"config?.data?.title as title\"\n            [id]=\"dialogId\">\n            {{ title | bbLocalize:{optional: true} }}\n        </h1>\n    </header>\n\n    <p>\n        {{ config?.data?.description | bbLocalize:{optional: true} }}\n    </p>\n\n    <footer bb-dialog-actions>\n        <button *ngIf=\"config?.data?.cancelButtonText as cancelButtonText\"\n                (click)=\"onClose(false)\"\n                bb-button\n                type=\"button\">\n            {{ cancelButtonText | bbLocalize:{optional: true} }}\n        </button>\n        <button #confirm\n                *ngIf=\"config?.data?.confirmButtonText as confirmButtonText\"\n                (click)=\"onClose(true)\"\n                bb-button\n                type=\"button\"\n                class=\"destructive\">\n            {{ confirmButtonText | bbLocalize:{optional: true} }}\n        </button>\n    </footer>\n</div>\n",
            changeDetection: ChangeDetectionStrategy.OnPush,
            encapsulation: ViewEncapsulation.None,
            preserveWhitespaces: false,
            host: { 'class': 'bb-dialog-confirm' },
            styles: [".bb-dialog-confirm{display:block}"]
        })
    ], BbDialogConfirm);
    return BbDialogConfirm;
}());

var BbDialogInjector = /** @class */ (function () {
    function BbDialogInjector(_parentInjector, _additionalTokens) {
        this._parentInjector = _parentInjector;
        this._additionalTokens = _additionalTokens;
    }
    BbDialogInjector.prototype.get = function (token, notFoundValue) {
        // First check if the additional token
        // contains the token.
        var value = this._additionalTokens.get(token);
        // If that value exists return it.
        if (value) {
            return value;
        }
        // Else query the parent injector.
        return this._parentInjector.get(token, notFoundValue);
    };
    return BbDialogInjector;
}());

var Dialog = /** @class */ (function () {
    function Dialog(_injector, _applicationRef, _componentFactoryResolver) {
        this._injector = _injector;
        this._applicationRef = _applicationRef;
        this._componentFactoryResolver = _componentFactoryResolver;
    }
    Dialog.prototype.open = function (componentType, config) {
        return this.createDialog(componentType, config);
    };
    Dialog.prototype.confirm = function (title, description, options) {
        if (options === void 0) { options = {}; }
        return this.open(BbDialogConfirm, {
            data: {
                title: title,
                description: description,
                cancelButtonText: options.cancelButtonText || 'dialog.default_cancel_button',
                confirmButtonText: options.confirmButtonText || 'dialog.default_confirm_button'
            }
        });
    };
    Dialog.prototype.createDialog = function (componentType, config) {
        var _this = this;
        // Create a reference.
        var dialogRef = new BbDialogRef();
        // Create a map with the config.
        var map = new WeakMap();
        map.set(BbDialogConfig, config);
        map.set(BbDialogRef, dialogRef);
        // Create a custom injector.
        var dialogInjector = new BbDialogInjector(this._injector, map);
        // Resolve the dialog component reference.
        var dialogComponentRef = this._componentFactoryResolver
            .resolveComponentFactory(BbDialogContainer)
            .create(dialogInjector);
        // Attach the host view to the application.
        this._applicationRef.attachView(dialogComponentRef.hostView);
        try {
            // Remove the current focus.
            var activeElement = document.activeElement;
            activeElement.blur();
            // Append the element to the DOM.
            document.body.appendChild(dialogComponentRef.location.nativeElement);
        }
        catch (_a) {
            // Don't do anything, because it must've failed.
        }
        // Set the child component type on the component.
        dialogComponentRef.instance.childComponentType = componentType;
        dialogComponentRef.instance.onOverlayClicked.subscribe(function () { return dialogRef.onOverlayClicked(); });
        // Listen for close events.
        dialogRef.afterClosed.subscribe(function () { return _this.deleteDialog(dialogComponentRef); });
        // Return the reference.
        return dialogRef;
    };
    Dialog.prototype.deleteDialog = function (dialogComponentRef) {
        // Only continue if the dialog component ref exists.
        if (!dialogComponentRef) {
            return;
        }
        // Detach the dialog.
        this._applicationRef.detachView(dialogComponentRef.hostView);
        // Destroy the dialog.
        dialogComponentRef.destroy();
        dialogComponentRef = null;
    };
    Dialog.ctorParameters = function () { return [
        { type: Injector },
        { type: ApplicationRef },
        { type: ComponentFactoryResolver }
    ]; };
    Dialog = __decorate([
        Injectable()
    ], Dialog);
    return Dialog;
}());

var DialogModule = /** @class */ (function () {
    function DialogModule() {
    }
    DialogModule_1 = DialogModule;
    DialogModule.forRoot = function () {
        return {
            ngModule: DialogModule_1,
            providers: [Dialog]
        };
    };
    var DialogModule_1;
    DialogModule = DialogModule_1 = __decorate([
        NgModule({
            imports: [
                CommonModule,
                LocalizeModule,
                ElementsModule
            ],
            declarations: [
                BbDialogContainer,
                BbDialogOverlay,
                BbDialogModal,
                BbDialogHeader,
                BbDialogLink,
                BbDialogActions,
                BbDialogConfirm,
                BbDialogInsertion
            ],
            entryComponents: [
                BbDialogContainer,
                BbDialogConfirm
            ],
            exports: [
                BbDialogOverlay,
                BbDialogModal,
                BbDialogHeader,
                BbDialogLink,
                BbDialogActions
            ]
        })
    ], DialogModule);
    return DialogModule;
}());

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

export { BbDialogActions, BbDialogConfig, BbDialogHeader, BbDialogLink, BbDialogModal, BbDialogOverlay, BbDialogRef, Dialog, DialogModule, BbDialogContainer as ɵa, BbDialogInsertion as ɵb, BbDialogConfirm as ɵc };
//# sourceMappingURL=bravobit-bb-foundation-dialog.js.map
