import { ContentObserver, ObserversModule } from '@angular/cdk/observers';
import { CommonModule } from '@angular/common';
import { InjectionToken, ElementRef, Inject, Input, Component, ViewEncapsulation, EventEmitter, Output, ViewChild, ContentChild, NgModule } from '@angular/core';
import { NtOverlayPosition, NtOverlayComponent, NtOverlayModule } from '@ng-tangram/components/core';
import { Subject } from 'rxjs';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
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_DROPDOWN_PARENT_COMPONENT = new InjectionToken('nt-dropdown-parent-component');
let NtDropdownPaneComponent = class NtDropdownPaneComponent {
    constructor(_contentObserver, _elementRef, _parent) {
        this._contentObserver = _contentObserver;
        this._elementRef = _elementRef;
        this._parent = _parent;
        this._arrow = false;
        this._autosize = false;
        this.size = 'small';
        this.class = '';
        this._contentChange = new Subject();
    }
    set autosize(value) {
        this._autosize = coerceBooleanProperty(value);
    }
    get autosize() { return this._autosize; }
    set arrow(value) { this._arrow = coerceBooleanProperty(value); }
    get arrow() { return this._arrow; }
    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();
            }
        }
    }
};
NtDropdownPaneComponent.ctorParameters = () => [
    { type: ContentObserver },
    { type: ElementRef },
    { type: undefined, decorators: [{ type: Inject, args: [NT_DROPDOWN_PARENT_COMPONENT,] }] }
];
__decorate([
    Input()
], NtDropdownPaneComponent.prototype, "size", void 0);
__decorate([
    Input()
], NtDropdownPaneComponent.prototype, "class", void 0);
__decorate([
    Input()
], NtDropdownPaneComponent.prototype, "autosize", null);
__decorate([
    Input()
], NtDropdownPaneComponent.prototype, "arrow", null);
NtDropdownPaneComponent = __decorate([
    Component({
        selector: 'nt-dropdown-pane, [nt-dropdown-pane]',
        template: '<ng-content></ng-content>',
        encapsulation: ViewEncapsulation.None,
        host: {
            '[class]': '["nt-dropdown-pane", size, class].join(" ")',
            '[class.autosize]': 'autosize'
        }
    }),
    __param(2, Inject(NT_DROPDOWN_PARENT_COMPONENT))
], NtDropdownPaneComponent);

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 NtDropdownComponent_1;
let NtDropdownComponent = NtDropdownComponent_1 = class NtDropdownComponent {
    constructor(_elementRef) {
        this.position = NtOverlayPosition.BottomLeft;
        this.trigger = 'hover';
        this.afterOpen = new EventEmitter();
        this.afterClosed = new EventEmitter();
        this.beforeOpen = new EventEmitter();
        this.beforeClosed = new EventEmitter();
        this.positionChange = new EventEmitter();
        this.origin = new CdkOverlayOrigin(_elementRef);
    }
    _onClick(_) {
        if (this.trigger === 'click') {
            this.overlay.toggle();
        }
    }
    _onMouseEnter(_) {
        if (this.trigger === 'hover') {
            this.overlay.markOpen();
        }
    }
    _onMouseLeave(_) {
        if (this.trigger === 'hover') {
            this.overlay.markClose();
        }
    }
    _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);
    }
};
NtDropdownComponent.ctorParameters = () => [
    { type: ElementRef }
];
__decorate$1([
    Input()
], NtDropdownComponent.prototype, "position", void 0);
__decorate$1([
    Input()
], NtDropdownComponent.prototype, "trigger", void 0);
__decorate$1([
    Output()
], NtDropdownComponent.prototype, "afterOpen", void 0);
__decorate$1([
    Output()
], NtDropdownComponent.prototype, "afterClosed", void 0);
__decorate$1([
    Output()
], NtDropdownComponent.prototype, "beforeOpen", void 0);
__decorate$1([
    Output()
], NtDropdownComponent.prototype, "beforeClosed", void 0);
__decorate$1([
    Output()
], NtDropdownComponent.prototype, "positionChange", void 0);
__decorate$1([
    ViewChild(NtOverlayComponent, { static: true })
], NtDropdownComponent.prototype, "overlay", void 0);
__decorate$1([
    ContentChild(NtDropdownPaneComponent)
], NtDropdownComponent.prototype, "pane", void 0);
NtDropdownComponent = NtDropdownComponent_1 = __decorate$1([
    Component({
        selector: 'nt-dropdown, [nt-dropdown]',
        template: "<ng-content></ng-content>\n<nt-overlay\n  [origin]=\"origin\"\n  [position]=\"position\"\n  [arrow]=\"pane?.arrow\"\n  (afterOpen)=\"_afterOpen($event)\"\n  (afterClosed)=\"_afterClosed($event)\"\n  (beforeOpen)=\"_beforeOpen($event)\"\n  (beforeClosed)=\"_beforeClosed($event)\"\n  (overlayEnter)=\"_onMouseEnter($event)\"\n  (overlayLeave)=\"_onMouseLeave($event)\"\n  (positionChange)=\"_positionChange($event)\">\n  <ng-content select=\"nt-dropdown-pane\"></ng-content>\n</nt-overlay>\n",
        encapsulation: ViewEncapsulation.None,
        host: {
            '(click)': '_onClick($event)',
            '(mouseenter)': '_onMouseEnter($event)',
            '(mouseleave)': '_onMouseLeave($event)'
        },
        providers: [
            { provide: NT_DROPDOWN_PARENT_COMPONENT, useExisting: NtDropdownComponent_1 }
        ]
    })
], NtDropdownComponent);

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 NtDropdownModule = class NtDropdownModule {
};
NtDropdownModule = __decorate$2([
    NgModule({
        imports: [
            CommonModule,
            ObserversModule,
            NtOverlayModule
        ],
        exports: [
            NtDropdownComponent,
            NtDropdownPaneComponent
        ],
        declarations: [
            NtDropdownComponent,
            NtDropdownPaneComponent
        ]
    })
], NtDropdownModule);

export { NT_DROPDOWN_PARENT_COMPONENT, NtDropdownComponent, NtDropdownModule, NtDropdownPaneComponent };
//# sourceMappingURL=dropdown.js.map
