/*
 * @license
 * Copyright Endlessjs. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */
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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Component, ComponentFactoryResolver, EventEmitter, Inject, Input, Output, Type, Optional, } from '@angular/core';
import { takeWhile } from 'rxjs/operators';
import { ReplaySubject, Subject } from 'rxjs';
import { ElComponentPortal } from '../cdk/overlay/mapping';
import { ElAdjustment, ElPosition, ElPositionBuilderService, } from '../cdk/overlay/overlay-position';
import { ElOverlayService, patch } from '../cdk/overlay/overlay-service';
import { ElTrigger, ElTriggerStrategyBuilderService } from '../cdk/overlay/overlay-trigger';
import { ElDatepickerContainerComponent } from './datepicker-container.component';
import { EL_DOCUMENT } from '../../theme.options';
import { ElCalendarRangeComponent } from '../calendar/calendar-range.component';
import { ElCalendarComponent } from '../calendar/calendar.component';
import { ElCalendarSize, ElCalendarViewMode, } from '../calendar-kit/model';
import { ElDateService } from '../calendar-kit/services/date.service';
import { EL_DATE_SERVICE_OPTIONS, ElDatepicker } from './datepicker.directive';
import { convertToBoolProperty } from '../helpers';
/**
 * The `ElBasePicker` component concentrates overlay manipulation logic.
 * */
let ElBasePicker = class ElBasePicker extends ElDatepicker {
    constructor(document, positionBuilder, triggerStrategyBuilder, overlay, cfr, dateService, dateServiceOptions) {
        super();
        this.document = document;
        this.positionBuilder = positionBuilder;
        this.triggerStrategyBuilder = triggerStrategyBuilder;
        this.overlay = overlay;
        this.cfr = cfr;
        this.dateService = dateService;
        this.dateServiceOptions = dateServiceOptions;
        /**
         * Defines if we should render previous and next months
         * in the current month view.
         * */
        this.boundingMonth = true;
        /**
         * Defines starting view for calendar.
         * */
        this.startView = ElCalendarViewMode.DATE;
        /**
         * Size of the calendar and entire components.
         * Can be 'medium' which is default or 'large'.
         * */
        this.size = ElCalendarSize.MEDIUM;
        /**
         * Hide picker when a date or a range is selected, `true` by default
         * @type {boolean}
         */
        this.hideOnSelect = true;
        /**
         * Determines should we show calendars header or not.
         * @type {boolean}
         */
        this.showHeader = true;
        /**
         * Sets symbol used as a header for week numbers column
         * */
        this.weekNumberSymbol = '#';
        this._showWeekNumber = false;
        this.init$ = new ReplaySubject();
        /**
         * Stream of picker changes. Required to be the subject because picker hides and shows and picker
         * change stream becomes recreated.
         * */
        this.onChange$ = new Subject();
        this.alive = true;
        this.blur$ = new Subject();
    }
    /**
     * Determines should we show week numbers column.
     * False by default.
     * */
    get showWeekNumber() {
        return this._showWeekNumber;
    }
    set showWeekNumber(value) {
        this._showWeekNumber = convertToBoolProperty(value);
    }
    /**
     * Returns picker instance.
     * */
    get picker() {
        return this.pickerRef && this.pickerRef.instance;
    }
    /**
     * Stream of picker value changes.
     * */
    get valueChange() {
        return this.onChange$.asObservable();
    }
    get isShown() {
        return this.ref && this.ref.hasAttached();
    }
    get init() {
        return this.init$.asObservable();
    }
    /**
     * Emits when datepicker looses focus.
     */
    get blur() {
        return this.blur$.asObservable();
    }
    ngOnInit() {
        this.checkFormat();
    }
    ngOnChanges(changes) {
        if (changes.format && !changes.format.isFirstChange()) {
            this.checkFormat();
        }
    }
    ngAfterViewInit() {
        this.init$.next();
    }
    ngOnDestroy() {
        this.alive = false;
        this.hide();
        this.init$.complete();
        if (this.ref) {
            this.ref.dispose();
        }
        if (this.triggerStrategy) {
            this.triggerStrategy.destroy();
        }
    }
    /**
     * Datepicker knows nothing about host html input element.
     * So, attach method attaches datepicker to the host input element.
     * */
    attach(hostRef) {
        this.hostRef = hostRef;
        this.subscribeOnTriggers();
    }
    getValidatorConfig() {
        return { min: this.min, max: this.max, filter: this.filter };
    }
    show() {
        if (!this.ref) {
            this.createOverlay();
        }
        this.openDatepicker();
    }
    shouldHide() {
        return this.hideOnSelect && !!this.value;
    }
    hide() {
        if (this.ref) {
            this.ref.detach();
        }
        // save current value if picker was rendered
        if (this.picker) {
            this.queue = this.value;
            this.pickerRef.destroy();
            this.pickerRef = null;
            this.container = null;
        }
    }
    createOverlay() {
        this.positionStrategy = this.createPositionStrategy();
        this.ref = this.overlay.create({
            positionStrategy: this.positionStrategy,
            scrollStrategy: this.overlay.scrollStrategies.reposition(),
        });
        this.subscribeOnPositionChange();
    }
    openDatepicker() {
        this.container = this.ref.attach(new ElComponentPortal(ElDatepickerContainerComponent, null, null, this.cfr));
        this.instantiatePicker();
        this.subscribeOnValueChange();
        this.writeQueue();
        this.patchWithInputs();
    }
    createPositionStrategy() {
        return this.positionBuilder
            .connectedTo(this.hostRef)
            .position(ElPosition.BOTTOM)
            .adjustment(ElAdjustment.COUNTERCLOCKWISE);
    }
    subscribeOnPositionChange() {
        this.positionStrategy.positionChange
            .pipe(takeWhile(() => this.alive))
            .subscribe((position) => patch(this.container, { position }));
    }
    createTriggerStrategy() {
        return this.triggerStrategyBuilder
            .trigger(ElTrigger.FOCUS)
            .host(this.hostRef.nativeElement)
            .container(() => this.container)
            .build();
    }
    subscribeOnTriggers() {
        this.triggerStrategy = this.createTriggerStrategy();
        this.triggerStrategy.show$.subscribe(() => this.show());
        this.triggerStrategy.hide$.subscribe(() => {
            this.blur$.next();
            this.hide();
        });
    }
    instantiatePicker() {
        this.pickerRef = this.container.instance.attach(new ElComponentPortal(this.pickerClass, null, null, this.cfr));
    }
    /**
     * Subscribes on picker value changes and emit data through this.onChange$ subject.
     * */
    subscribeOnValueChange() {
        this.pickerValueChange.subscribe(date => {
            this.onChange$.next(date);
        });
    }
    patchWithInputs() {
        this.picker.boundingMonth = this.boundingMonth;
        this.picker.startView = this.startView;
        this.picker.min = this.min;
        this.picker.max = this.max;
        this.picker.filter = this.filter;
        this.picker._cellComponent = this.dayCellComponent;
        this.picker.monthCellComponent = this.monthCellComponent;
        this.picker._yearCellComponent = this.yearCellComponent;
        this.picker.size = this.size;
        this.picker.showHeader = this.showHeader;
        this.picker.visibleDate = this.visibleDate;
        this.picker.showWeekNumber = this.showWeekNumber;
        this.picker.weekNumberSymbol = this.weekNumberSymbol;
    }
    checkFormat() {
        if (this.dateService.getId() === 'native' && this.format) {
            throw new Error('Can\'t format native date. To use custom formatting you have to install @endlessjs/moment or ' +
                '@endlessjs/date-fns package and import ElMomentDateModule or ElDateFnsDateModule accordingly.' +
                'More information at "Formatting issue" ' +
                'https://endlessjs.github.io/endlessjs/docs/components/datepicker/overview#eldatepickercomponent');
        }
        const isFormatSet = this.format || (this.dateServiceOptions && this.dateServiceOptions.format);
        if (this.dateService.getId() === 'date-fns' && !isFormatSet) {
            throw new Error('format is required when using ElDateFnsDateModule');
        }
    }
};
__decorate([
    Input(),
    __metadata("design:type", String)
], ElBasePicker.prototype, "format", void 0);
__decorate([
    Input(),
    __metadata("design:type", Boolean)
], ElBasePicker.prototype, "boundingMonth", void 0);
__decorate([
    Input(),
    __metadata("design:type", String)
], ElBasePicker.prototype, "startView", void 0);
__decorate([
    Input(),
    __metadata("design:type", Object)
], ElBasePicker.prototype, "min", void 0);
__decorate([
    Input(),
    __metadata("design:type", Object)
], ElBasePicker.prototype, "max", void 0);
__decorate([
    Input(),
    __metadata("design:type", Function)
], ElBasePicker.prototype, "filter", void 0);
__decorate([
    Input(),
    __metadata("design:type", Type)
], ElBasePicker.prototype, "dayCellComponent", void 0);
__decorate([
    Input(),
    __metadata("design:type", Type)
], ElBasePicker.prototype, "monthCellComponent", void 0);
__decorate([
    Input(),
    __metadata("design:type", Type)
], ElBasePicker.prototype, "yearCellComponent", void 0);
__decorate([
    Input(),
    __metadata("design:type", String)
], ElBasePicker.prototype, "size", void 0);
__decorate([
    Input(),
    __metadata("design:type", Object)
], ElBasePicker.prototype, "visibleDate", void 0);
__decorate([
    Input(),
    __metadata("design:type", Boolean)
], ElBasePicker.prototype, "hideOnSelect", void 0);
__decorate([
    Input(),
    __metadata("design:type", Boolean)
], ElBasePicker.prototype, "showHeader", void 0);
__decorate([
    Input(),
    __metadata("design:type", String)
], ElBasePicker.prototype, "weekNumberSymbol", void 0);
__decorate([
    Input(),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ElBasePicker.prototype, "showWeekNumber", null);
ElBasePicker = __decorate([
    __param(0, Inject(EL_DOCUMENT)),
    __param(6, Optional()), __param(6, Inject(EL_DATE_SERVICE_OPTIONS)),
    __metadata("design:paramtypes", [Object, ElPositionBuilderService,
        ElTriggerStrategyBuilderService,
        ElOverlayService,
        ComponentFactoryResolver,
        ElDateService, Object])
], ElBasePicker);
export { ElBasePicker };
/**
 * The DatePicker components itself.
 * Provides a proxy to `ElCalendar` options as well as custom picker options.
 */
let ElDatepickerComponent = class ElDatepickerComponent extends ElBasePicker {
    /**
     * The DatePicker components itself.
     * Provides a proxy to `ElCalendar` options as well as custom picker options.
     */
    constructor() {
        super(...arguments);
        this.pickerClass = ElCalendarComponent;
    }
    /**
     * Date which will be rendered as selected.
     * */
    set date(date) {
        this.value = date;
    }
    /**
     * Emits date when selected.
     * */
    get dateChange() {
        return this.valueChange;
    }
    get value() {
        return this.picker ? this.picker.date : undefined;
    }
    set value(date) {
        if (!this.picker) {
            this.queue = date;
            return;
        }
        if (date) {
            this.visibleDate = date;
            this.picker.visibleDate = date;
            this.picker.date = date;
        }
    }
    get pickerValueChange() {
        return this.picker.dateChange;
    }
    writeQueue() {
        if (this.queue) {
            const date = this.queue;
            this.queue = null;
            this.value = date;
        }
    }
};
__decorate([
    Input(),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], ElDatepickerComponent.prototype, "date", null);
__decorate([
    Output(),
    __metadata("design:type", EventEmitter),
    __metadata("design:paramtypes", [])
], ElDatepickerComponent.prototype, "dateChange", null);
ElDatepickerComponent = __decorate([
    Component({
        selector: 'el-datepicker',
        template: ''
    })
], ElDatepickerComponent);
export { ElDatepickerComponent };
/**
 * The RangeDatePicker components itself.
 * Provides a proxy to `ElCalendarRange` options as well as custom picker options.
 */
let ElRangepickerComponent = class ElRangepickerComponent extends ElBasePicker {
    /**
     * The RangeDatePicker components itself.
     * Provides a proxy to `ElCalendarRange` options as well as custom picker options.
     */
    constructor() {
        super(...arguments);
        this.pickerClass = ElCalendarRangeComponent;
    }
    /**
     * Range which will be rendered as selected.
     * */
    set range(range) {
        this.value = range;
    }
    /**
     * Emits range when start selected and emits again when end selected.
     * */
    get rangeChange() {
        return this.valueChange;
    }
    get value() {
        return this.picker ? this.picker.range : undefined;
    }
    set value(range) {
        if (!this.picker) {
            this.queue = range;
            return;
        }
        if (range) {
            const visibleDate = range && range.start;
            this.visibleDate = visibleDate;
            this.picker.visibleDate = visibleDate;
            this.picker.range = range;
        }
    }
    get pickerValueChange() {
        return this.picker.rangeChange;
    }
    shouldHide() {
        return super.shouldHide() && !!(this.value && this.value.start && this.value.end);
    }
    writeQueue() {
        if (this.queue) {
            const range = this.queue;
            this.queue = null;
            this.value = range;
        }
    }
};
__decorate([
    Input(),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], ElRangepickerComponent.prototype, "range", null);
__decorate([
    Output(),
    __metadata("design:type", EventEmitter),
    __metadata("design:paramtypes", [])
], ElRangepickerComponent.prototype, "rangeChange", null);
ElRangepickerComponent = __decorate([
    Component({
        selector: 'el-rangepicker',
        template: ''
    })
], ElRangepickerComponent);
export { ElRangepickerComponent };
//# sourceMappingURL=datepicker.component.js.map