import { __decorate } from 'tslib';
import { ɵɵdefineInjectable, Injectable, EventEmitter, ElementRef, ViewChild, Input, Output, HostListener, Component, Directive, NgModule } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { DatepickerModule } from 'ngx-bootstrap/datepicker';
import { TimepickerModule } from 'ngx-bootstrap/timepicker';
import { BsDropdownModule } from 'ngx-bootstrap/dropdown';

var DatetimePopupService = /** @class */ (function () {
    function DatetimePopupService() {
        this.autoCloseSubject = new BehaviorSubject(0);
    }
    DatetimePopupService.ɵprov = ɵɵdefineInjectable({ factory: function DatetimePopupService_Factory() { return new DatetimePopupService(); }, token: DatetimePopupService, providedIn: "root" });
    DatetimePopupService = __decorate([
        Injectable({
            providedIn: 'root'
        })
    ], DatetimePopupService);
    return DatetimePopupService;
}());

var DatetimePopupComponent = /** @class */ (function () {
    function DatetimePopupComponent(elementRef) {
        this.elementRef = elementRef;
        this.appendTo = undefined;
        this.valueChange = new EventEmitter();
        this.showPopup = false;
        this.showPopupChange = new EventEmitter();
        this.showDate = true;
        this.showTime = true;
        this.showWeeks = false;
        this.showMeridian = false;
        this.showSeconds = false;
        this.datepickerMode = 'day';
        this.initDate = null;
        this.minDate = null;
        this.maxDate = null;
        this.dateDisabled = [];
        this.minuteStep = 5;
        this.hourStep = 1;
        this.secondsStep = 1;
        this.localValue = null;
        this.isOpening = false;
        this.isDropUp = false;
    }
    DatetimePopupComponent.prototype.ngOnChanges = function (changes) {
        if (!this.nowButton) {
            this.nowButton = { show: true, label: 'Now', cssClass: 'btn btn-secondary btn-sm' };
        }
        if (!this.clearButton) {
            this.clearButton = { show: true, label: 'Clear', cssClass: 'btn btn-secondary btn-sm' };
        }
        if (!this.closeButton) {
            this.closeButton = { show: true, label: 'Close', cssClass: 'btn btn-secondary btn-sm' };
        }
        // user maybe typing a value into an input box, so would come in as string
        if (typeof this.value === 'string') {
            // check if the string is a valid date
            if (!isNaN(new Date(this.value).getTime())) {
                this.localValue = new Date(this.value);
            }
        }
        else if (this.value) {
            this.localValue = this.value;
        }
        if (changes.showPopup && this.dropdown) {
            if (changes.showPopup.currentValue === true) {
                this.dropdown.show();
            }
            else if (changes.showPopup.currentValue === false) {
                this.dropdown.hide();
            }
        }
    };
    DatetimePopupComponent.prototype.onWindowScroll = function () {
        var nativeEl = this.elementRef.nativeElement;
        var clientRect = nativeEl.getBoundingClientRect();
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var offsetTop = clientRect.top + window.pageYOffset;
        var height = clientRect.height;
        var dropdownEl = nativeEl.children.item(0);
        var menuEl = dropdownEl.children.length > 0 ? dropdownEl.children.item(0) : null;
        var menuHeight = this.showDate && this.showTime ? 402 : this.showDate ? 300 : 102;
        if (menuEl != null) {
            // get the style
            var display = menuEl.style.display;
            menuEl.style.display = 'block';
            menuHeight = menuEl.getBoundingClientRect().height;
            menuEl.style.display = display;
        }
        if ((offsetTop - menuHeight) <= 0) {
            this.isDropUp = false;
        }
        else {
            this.isDropUp = ((offsetTop + height + menuHeight) > (scrollTop + document.documentElement.clientHeight));
        }
    };
    DatetimePopupComponent.prototype.onOpenChange = function (opened) {
        var _this = this;
        if (opened === true) {
            this.isOpening = true;
            this.onWindowScroll();
            this.dropdown.show();
            setTimeout(function () { return _this.isOpening = false; }, 250);
        }
        else {
            this.showPopupChange.emit(false);
        }
    };
    DatetimePopupComponent.prototype.onNow = function () {
        this.localValue = new Date();
        this.onPickerChange('timepicker');
    };
    DatetimePopupComponent.prototype.onClear = function () {
        this.valueChange.emit(null);
    };
    DatetimePopupComponent.prototype.onClose = function () {
        this.showPopupChange.emit(false);
    };
    DatetimePopupComponent.prototype.onOffClick = function ($event) {
        this.showPopupChange.emit(false);
    };
    DatetimePopupComponent.prototype.onPickerChange = function (picker) {
        if (this.isOpening === true) {
            return;
        }
        if (picker === 'datepicker' && this.value != null) {
            var hours = this.value.getHours();
            var minutes = this.value.getMinutes();
            var seconds = this.value.getSeconds();
            var milliseconds = this.value.getMilliseconds();
            this.localValue.setHours(hours, minutes, seconds, milliseconds);
        }
        this.valueChange.emit(this.localValue);
        if (this.showDate === true && this.showTime === false) {
            this.dropdown.hide();
        }
    };
    DatetimePopupComponent.ctorParameters = function () { return [
        { type: ElementRef }
    ]; };
    __decorate([
        ViewChild('dropdown', { static: true })
    ], DatetimePopupComponent.prototype, "dropdown", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "appendTo", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "value", void 0);
    __decorate([
        Output()
    ], DatetimePopupComponent.prototype, "valueChange", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "showPopup", void 0);
    __decorate([
        Output()
    ], DatetimePopupComponent.prototype, "showPopupChange", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "showDate", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "showTime", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "showWeeks", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "showMeridian", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "showSeconds", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "datepickerMode", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "initDate", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "minDate", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "maxDate", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "dateDisabled", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "nowButton", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "clearButton", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "closeButton", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "minuteStep", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "hourStep", void 0);
    __decorate([
        Input()
    ], DatetimePopupComponent.prototype, "secondsStep", void 0);
    __decorate([
        HostListener('window:scroll', []),
        HostListener('window:resize', [])
    ], DatetimePopupComponent.prototype, "onWindowScroll", null);
    DatetimePopupComponent = __decorate([
        Component({
            selector: 'datetime-popup',
            template: "<div [container]=\"appendTo\" (isOpenChange)=\"onOpenChange($event)\" [dropup]=\"isDropUp\" dropdown #dropdown=\"bs-dropdown\" [autoClose]=\"false\" (offClick)=\"onOffClick($event)\">\r\n  <ul class=\"datetime-popup dropdown-menu\" role=\"menu\" [ngClass]=\"{ 'show': dropdown.isOpen === true, 'body-container': appendTo == 'body', 'dropdown': !isDropUp, 'dropup': isDropUp }\" *dropdownMenu>\r\n    <li class=\"my-2 mx-2\">\r\n      <datepicker *ngIf=\"showDate\"\r\n                  [(ngModel)]=\"localValue\"\r\n                  (ngModelChange)=\"onPickerChange('datepicker')\"\r\n                  [showWeeks]=\"showWeeks\"\r\n                  [datepickerMode]=\"datepickerMode\"\r\n                  [minDate]=\"minDate\"\r\n                  [maxDate]=\"maxDate\"\r\n                  [dateDisabled]=\"dateDisabled\"></datepicker>\r\n      <timepicker *ngIf=\"showTime\"\r\n                  [(ngModel)]=\"localValue\"\r\n                  [min]=\"minDate\"\r\n                  [max]=\"maxDate\"\r\n                  [hourStep]=\"hourStep\"\r\n                  [minuteStep]=\"minuteStep\"\r\n                  [secondsStep]=\"secondsStep\"\r\n                  [showSeconds]=\"showSeconds\"\r\n                  [showMeridian]=\"showMeridian\"\r\n                  (ngModelChange)=\"onPickerChange('timepicker')\"></timepicker>\r\n    </li>\r\n    <li class=\"mx-2 mb-2\">\r\n      <span class=\"btn-group float-left\">\r\n          <button type=\"button\"\r\n                  (click)=\"onNow()\"\r\n                  *ngIf=\"nowButton.show\"\r\n                  [ngClass]=\"nowButton.cssClass\">{{ nowButton.label }}</button>\r\n          <button type=\"button\"\r\n                  (click)=\"onClear()\"\r\n                  *ngIf=\"clearButton.show\"\r\n                  [ngClass]=\"clearButton.cssClass\">{{ clearButton.label }}</button>\r\n      </span>\r\n      <span class=\"btn-group float-right\">\r\n          <button type=\"button\"\r\n                  (click)=\"onClose()\"\r\n                  *ngIf=\"closeButton.show\"\r\n                  [ngClass]=\"closeButton.cssClass\">{{ closeButton.label }}</button>\r\n      </span>\r\n    </li>\r\n  </ul>\r\n</div>\r\n"
        })
    ], DatetimePopupComponent);
    return DatetimePopupComponent;
}());

var OffClickDirective = /** @class */ (function () {
    function OffClickDirective(elementRef) {
        this.elementRef = elementRef;
        this.offClick = new EventEmitter();
    }
    OffClickDirective.prototype.ngOnInit = function () {
        var _this = this;
        this.eventHandler = function ($event) {
            $event.stopPropagation();
            if (!_this.elementRef.nativeElement.contains($event.target) && $event.target.localName !== 'button') {
                _this.offClick.emit(null);
            }
        };
        document.addEventListener('mouseup', this.eventHandler);
    };
    OffClickDirective.prototype.ngOnDestroy = function () {
        document.removeEventListener('mouseup', this.eventHandler);
    };
    OffClickDirective.ctorParameters = function () { return [
        { type: ElementRef }
    ]; };
    __decorate([
        Output()
    ], OffClickDirective.prototype, "offClick", void 0);
    OffClickDirective = __decorate([
        Directive({
            selector: '[offClick]'
        })
    ], OffClickDirective);
    return OffClickDirective;
}());

var DatetimePopupModule = /** @class */ (function () {
    function DatetimePopupModule() {
    }
    DatetimePopupModule_1 = DatetimePopupModule;
    DatetimePopupModule.forRoot = function () {
        return {
            ngModule: DatetimePopupModule_1,
            providers: [
                DatetimePopupService
            ]
        };
    };
    var DatetimePopupModule_1;
    DatetimePopupModule = DatetimePopupModule_1 = __decorate([
        NgModule({
            declarations: [
                DatetimePopupComponent,
                OffClickDirective
            ],
            imports: [
                CommonModule,
                FormsModule,
                BrowserAnimationsModule,
                DatepickerModule,
                TimepickerModule,
                BsDropdownModule
            ],
            exports: [
                DatetimePopupComponent
            ],
            entryComponents: [
                DatetimePopupComponent
            ]
        })
    ], DatetimePopupModule);
    return DatetimePopupModule;
}());

/*
 * Public API Surface of ngx-bootstrap-datetime-popup
 */

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

export { DatetimePopupComponent, DatetimePopupModule, DatetimePopupService, OffClickDirective as ɵa };
//# sourceMappingURL=ngx-bootstrap-datetime-popup.js.map
