import { ContentObserver, ObserversModule } from '@angular/cdk/observers';
import { CommonModule } from '@angular/common';
import { InjectionToken, ElementRef, Inject, Component, ViewEncapsulation, EventEmitter, TemplateRef, Input, Output, ViewChild, NgModule } from '@angular/core';
import { NtOverlayPosition, NtOverlayComponent, NtOverlayModule } from '@ng-tangram/components/core';
import { NtDropdownModule } from '@ng-tangram/components/dropdown';
import { Subject } from 'rxjs';
import { CdkOverlayOrigin } from '@angular/cdk/overlay';

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
const NT_POPOVER_PARENT_COMPONENT = new InjectionToken('nt-popover-parent-component');
let NtPopoverPaneComponent = class NtPopoverPaneComponent {
    constructor(_contentObserver, _elementRef, _parent) {
        this._contentObserver = _contentObserver;
        this._elementRef = _elementRef;
        this._parent = _parent;
        this._contentChange = new Subject();
    }
    get contentChanged() {
        return this._contentChange.asObservable();
    }
    get textContent() {
        return (this._elementRef.nativeElement.textContent || '').trim();
    }
    ngAfterContentInit() {
        this._contentSubscription = this._contentObserver
            .observe(this._elementRef)
            .subscribe(() => this._checkContentChange());
    }
    ngOnDestroy() {
        if (this._contentSubscription) {
            this._contentSubscription.unsubscribe();
        }
    }
    _checkContentChange() {
        if (this._parent.overlay.opened) {
            const cdkConnectedOverlay = this._parent.overlay.cdkConnectedOverlay;
            if (cdkConnectedOverlay && cdkConnectedOverlay.overlayRef) {
                cdkConnectedOverlay.overlayRef.updatePosition();
            }
        }
    }
};
NtPopoverPaneComponent.ctorParameters = () => [
    { type: ContentObserver },
    { type: ElementRef },
    { type: undefined, decorators: [{ type: Inject, args: [NT_POPOVER_PARENT_COMPONENT,] }] }
];
NtPopoverPaneComponent = __decorate([
    Component({
        selector: 'nt-popover-pane, [nt-popover-pane]',
        template: `
    <ng-content></ng-content>
  `,
        encapsulation: ViewEncapsulation.None
    }),
    __param(2, Inject(NT_POPOVER_PARENT_COMPONENT))
], NtPopoverPaneComponent);

var __decorate$1 = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var NtPopoverComponent_1;
let NtPopoverComponent = NtPopoverComponent_1 = class NtPopoverComponent {
    constructor(_elementRef) {
        this._elementRef = _elementRef;
        this._title = '';
        this.position = NtOverlayPosition.Top;
        this.confirm = new EventEmitter();
        this.cancel = new EventEmitter();
        this.afterOpen = new EventEmitter();
        this.afterClosed = new EventEmitter();
        this.beforeOpen = new EventEmitter();
        this.beforeClosed = new EventEmitter();
        this.positionChange = new EventEmitter();
        this.origin = new CdkOverlayOrigin(this._elementRef);
    }
    set title(value) { this._title = value; }
    get title() { return this._title; }
    set template(value) { this._template = value; }
    get template() { return this._template; }
    set popover(value) {
        if (value instanceof TemplateRef) {
            this._template = value;
        }
        else {
            this._title = value;
            this._template = null;
        }
    }
    _afterOpen(event) {
        this.afterOpen.next(event);
    }
    _afterClosed(event) {
        this.afterClosed.next(event);
    }
    _beforeOpen(event) {
        this.beforeOpen.next(event);
    }
    _beforeClosed(event) {
        this.beforeClosed.next(event);
    }
    _positionChange(change) {
        this.positionChange.next(change);
    }
};
NtPopoverComponent.ctorParameters = () => [
    { type: ElementRef }
];
__decorate$1([
    Input()
], NtPopoverComponent.prototype, "title", null);
__decorate$1([
    Input()
], NtPopoverComponent.prototype, "template", null);
__decorate$1([
    Input('nt-popover')
], NtPopoverComponent.prototype, "popover", null);
__decorate$1([
    Input()
], NtPopoverComponent.prototype, "position", void 0);
__decorate$1([
    Output()
], NtPopoverComponent.prototype, "confirm", void 0);
__decorate$1([
    Output()
], NtPopoverComponent.prototype, "cancel", void 0);
__decorate$1([
    Output()
], NtPopoverComponent.prototype, "afterOpen", void 0);
__decorate$1([
    Output()
], NtPopoverComponent.prototype, "afterClosed", void 0);
__decorate$1([
    Output()
], NtPopoverComponent.prototype, "beforeOpen", void 0);
__decorate$1([
    Output()
], NtPopoverComponent.prototype, "beforeClosed", void 0);
__decorate$1([
    Output()
], NtPopoverComponent.prototype, "positionChange", void 0);
__decorate$1([
    ViewChild(NtOverlayComponent, { static: true })
], NtPopoverComponent.prototype, "overlay", void 0);
NtPopoverComponent = NtPopoverComponent_1 = __decorate$1([
    Component({
        selector: '[nt-popover]',
        template: "<ng-content></ng-content>\n<nt-overlay\n  arrow\n  [origin]=\"origin\"\n  [position]=\"position\"\n  (afterOpen)=\"_afterOpen($event)\"\n  (afterClosed)=\"_afterClosed($event)\"\n  (beforeOpen)=\"_beforeOpen($event)\"\n  (beforeClosed)=\"_beforeClosed($event)\"\n  (positionChange)=\"_positionChange($event)\">\n  <div class=\"nt-popover\">\n    <div class=\"nt-popover-title\" *ngIf=\"template\"><ng-container *ngTemplateOutlet=\"template\"></ng-container></div>\n    <div class=\"nt-popover-title\" *ngIf=\"!template\" [innerHtml]=\"title\"></div>\n    <div class=\"nt-popover-pane\">\n      <ng-content select=\"nt-popover-pane\"></ng-content>\n    </div>\n  </div>\n</nt-overlay>\n",
        encapsulation: ViewEncapsulation.None,
        host: {
            '(click)': 'overlay.toggle()'
        },
        providers: [
            { provide: NT_POPOVER_PARENT_COMPONENT, useExisting: NtPopoverComponent_1 }
        ]
    })
], NtPopoverComponent);

var __decorate$2 = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
let NtPopoverModule = class NtPopoverModule {
};
NtPopoverModule = __decorate$2([
    NgModule({
        imports: [CommonModule, ObserversModule, NtOverlayModule, NtDropdownModule],
        exports: [NtPopoverComponent, NtPopoverPaneComponent],
        declarations: [NtPopoverComponent, NtPopoverPaneComponent]
    })
], NtPopoverModule);

export { NT_POPOVER_PARENT_COMPONENT, NtPopoverComponent, NtPopoverModule, NtPopoverPaneComponent };
//# sourceMappingURL=popover.js.map
