import * as tslib_1 from "tslib";
import { Component, Input, Output, EventEmitter, Renderer2, ViewChild, ElementRef, ViewEncapsulation, ChangeDetectionStrategy, ChangeDetectorRef, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { format } from 'date-fns';
import { OverlayComponent } from '../overlay';
import { OverlayOriginDirective } from '../overlay/overlay-origin.directive';
import { OnChange } from '../core/decorators';
/*
 * Provider Expression that allows component to register as a ControlValueAccessor.
 * This allows it to support [(ngModel)].
 * @docs-private
 */
var DATEPICKER_VALUE_ACCESSOR = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(function () { return DatePickerComponent; }),
    multi: true
};
/**
 * DatePicker Component
 */
var DatePickerComponent = /** @class */ (function () {
    function DatePickerComponent(render, cd, el) {
        this.render = render;
        this.cd = cd;
        this.el = el;
        /** datepicker change event */
        this.change = new EventEmitter();
        /**
         * selected date
         */
        this.value = new Date();
        /** whether the datepicker is disabled */
        this.disabled = false;
        /** whether the panel is show */
        this._showPanel = false;
    }
    Object.defineProperty(DatePickerComponent.prototype, "valueText", {
        /**
         * show text in input box
         * @readonly
         * @docs-private
         */
        get: function () {
            return format(this.value, 'yyyy-MM-dd');
        },
        enumerable: true,
        configurable: true
    });
    DatePickerComponent.prototype.ngOnInit = function () {
        this.overlay.origin = this.origin;
    };
    DatePickerComponent.prototype.ngOnDestroy = function () {
    };
    DatePickerComponent.prototype.onShowCalendar = function () {
        if (this._showPanel) {
            this._hideOverlay();
        }
        else {
            if (!this.disabled) {
                this._showOverlay();
            }
        }
    };
    DatePickerComponent.prototype._hideOverlay = function () {
        this.overlay.hide();
        this._showPanel = false;
    };
    DatePickerComponent.prototype._showOverlay = function () {
        this.overlay.show();
        this._showPanel = true;
    };
    DatePickerComponent.prototype.onSelectDate = function (date) {
        this.value = date;
        this.change.emit(this.value);
        this._hideOverlay();
        this._markForCheck();
    };
    DatePickerComponent.prototype.onFilterPanelHide = function (_panel) {
        this._showPanel = false;
    };
    /**
     * Registers a callback that will be triggered when the value has changed.
     * Implemented as part of ControlValueAccessor.
     * @param fn On change callback function.
     */
    DatePickerComponent.prototype.registerOnChange = function (fn) {
        this._onModelChange = fn;
    };
    /**
     * Registers a callback that will be triggered when the control has been touched.
     * Implemented as part of ControlValueAccessor.
     * @param fn On touch callback function.
     */
    DatePickerComponent.prototype.registerOnTouched = function (fn) {
        this._onTouch = fn;
    };
    /**
     * Sets the model value. Implemented as part of ControlValueAccessor.
     * @param {any} value - value to be set to the model.
     */
    DatePickerComponent.prototype.writeValue = function (value) {
        if (value) {
            this.value = value;
            this.cd.markForCheck();
        }
    };
    /**
     * Toggles the disabled state of the component. Implemented as part of ControlValueAccessor.
     * @param {boolean} isDisabled - Whether the component should be disabled.
     */
    DatePickerComponent.prototype.setDisabledState = function (isDisabled) {
        this.disabled = isDisabled;
    };
    /**
     * update form model value and mark for check
     * @docs-private
     */
    DatePickerComponent.prototype._markForCheck = function () {
        if (this._onModelChange) {
            this._onModelChange(this.value);
        }
        if (this._onTouch) {
            this._onTouch(this.value);
        }
        this.cd.markForCheck();
    };
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", EventEmitter)
    ], DatePickerComponent.prototype, "change", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Date)
    ], DatePickerComponent.prototype, "value", void 0);
    tslib_1.__decorate([
        OnChange(true),
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], DatePickerComponent.prototype, "disabled", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Function)
    ], DatePickerComponent.prototype, "disabledStrategy", void 0);
    tslib_1.__decorate([
        ViewChild('origin', { static: true }),
        tslib_1.__metadata("design:type", OverlayOriginDirective)
    ], DatePickerComponent.prototype, "origin", void 0);
    tslib_1.__decorate([
        ViewChild('overlay', { static: true }),
        tslib_1.__metadata("design:type", OverlayComponent)
    ], DatePickerComponent.prototype, "overlay", void 0);
    DatePickerComponent = tslib_1.__decorate([
        Component({
            selector: 'nb-datepicker',
            template: "<div class=\"nb-datepicker-input-area\" [class.disabled]=\"disabled\" (click)=\"onShowCalendar()\"><input nbOverlayOrigin #origin=\"nbOverlayOrigin\" readonly=\"readonly\" type=\"text\" class=\"nb-datepicker-input\" [class.disabled]=\"disabled\" [ngModel]=\"valueText\" [disabled]=\"disabled\"></div><nb-overlay #overlay placement=\"bottom-left\" (closeHandler)=\"onFilterPanelHide($event)\"><div class=\"nb-datepicker-calendar-panel\"><nb-calendar [disabled]=\"disabled\" [value]=\"value\" [disabledStrategy]=\"disabledStrategy\" (select)=\"onSelectDate($event)\"></nb-calendar></div></nb-overlay>",
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: false,
            providers: [DATEPICKER_VALUE_ACCESSOR],
            host: {
                'class': 'nb-widget nb-datepicker'
            },
            exportAs: 'nbDatePicker'
        }),
        tslib_1.__metadata("design:paramtypes", [Renderer2,
            ChangeDetectorRef,
            ElementRef])
    ], DatePickerComponent);
    return DatePickerComponent;
}());
export { DatePickerComponent };
//# sourceMappingURL=datepicker.js.map