import { Component, EventEmitter, Input, Output, Type } from '@angular/core';
import { RCalendarSize, RCalendarViewMode, } from '../calendar-kit/model';
import { RDateService } from '../calendar-kit/services/date.service';
import { RCalendarRangeDayCellComponent } from './calendar-range-day-cell.component';
import { RCalendarRangeYearCellComponent } from './calendar-range-year-cell.component';
import { RCalendarRangeMonthCellComponent } from './calendar-range-month-cell.component';
export class RCalendarRangeComponent {
    constructor(dateService) {
        this.dateService = dateService;
        /**
         * Defines if we should render previous and next months
         * in the current month view.
         * */
        this.boundingMonth = true;
        /**
         * Defines starting view for the calendar.
         * */
        this.startView = RCalendarViewMode.DATE;
        this.dayCellComponent = RCalendarRangeDayCellComponent;
        this.monthCellComponent = RCalendarRangeMonthCellComponent;
        this.yearCellComponent = RCalendarRangeYearCellComponent;
        /**
         * Size of the calendar and entire components.
         * Can be 'medium' which is default or 'large'.
         * */
        this.size = RCalendarSize.MEDIUM;
        /**
         * Determines should we show calendars navigation or not.
         * */
        this.showNavigation = true;
        this._showWeekNumber = false;
        /**
         * Sets symbol used as a header for week numbers column
         * */
        this.weekNumberSymbol = '#';
        /**
         * Emits range when start selected and emits again when end selected.
         * */
        this.rangeChange = new EventEmitter();
    }
    /**
     * Custom day cell component. Have to implement `RCalendarCell` interface.
     * */
    set _cellComponent(cellComponent) {
        if (cellComponent) {
            this.dayCellComponent = cellComponent;
        }
    }
    /**
     * Custom month cell component. Have to implement `RCalendarCell` interface.
     * */
    set _monthCellComponent(cellComponent) {
        if (cellComponent) {
            this.monthCellComponent = cellComponent;
        }
    }
    /**
     * Custom year cell component. Have to implement `RCalendarCell` interface.
     * */
    set _yearCellComponent(cellComponent) {
        if (cellComponent) {
            this.yearCellComponent = cellComponent;
        }
    }
    /**
     * Determines should we show week numbers column.
     * False by default.
     * */
    get showWeekNumber() {
        return this._showWeekNumber;
    }
    set showWeekNumber(value) {
        this._showWeekNumber = !!value;
    }
    onChange(date) {
        this.initDateIfNull();
        this.handleSelected(date);
    }
    initDateIfNull() {
        if (!this.range) {
            this.range = { start: null, end: null };
        }
    }
    handleSelected(date) {
        if (this.selectionStarted()) {
            this.selectEnd(date);
        }
        else {
            this.selectStart(date);
        }
    }
    selectionStarted() {
        const { start, end } = this.range;
        return start && !end;
    }
    selectStart(start) {
        this.selectRange({ start });
    }
    selectEnd(date) {
        const { start } = this.range;
        if (this.dateService.compareDates(date, start) > 0) {
            this.selectRange({ start, end: date });
        }
        else {
            this.selectRange({ start: date, end: start });
        }
    }
    selectRange(range) {
        this.range = range;
        this.rangeChange.emit(range);
    }
}
RCalendarRangeComponent.decorators = [
    { type: Component, args: [{
                selector: 'r-calendar-range',
                template: `
    <r-base-calendar
      [date]="range"
      (dateChange)="onChange($any($event))"
      [min]="min"
      [max]="max"
      [filter]="filter"
      [startView]="startView"
      [boundingMonth]="boundingMonth"
      [dayCellComponent]="dayCellComponent"
      [monthCellComponent]="monthCellComponent"
      [yearCellComponent]="yearCellComponent"
      [visibleDate]="visibleDate"
      [showNavigation]="showNavigation"
      [size]="size"
      [showWeekNumber]="showWeekNumber"
      [weekNumberSymbol]="weekNumberSymbol"
    ></r-base-calendar>
  `
            },] }
];
RCalendarRangeComponent.ctorParameters = () => [
    { type: RDateService }
];
RCalendarRangeComponent.propDecorators = {
    boundingMonth: [{ type: Input }],
    startView: [{ type: Input }],
    min: [{ type: Input }],
    max: [{ type: Input }],
    filter: [{ type: Input }],
    _cellComponent: [{ type: Input, args: ['dayCellComponent',] }],
    _monthCellComponent: [{ type: Input, args: ['monthCellComponent',] }],
    monthCellComponent: [{ type: Input }],
    _yearCellComponent: [{ type: Input, args: ['yearCellComponent',] }],
    size: [{ type: Input }],
    visibleDate: [{ type: Input }],
    showNavigation: [{ type: Input }],
    range: [{ type: Input }],
    showWeekNumber: [{ type: Input }],
    weekNumberSymbol: [{ type: Input }],
    rangeChange: [{ type: Output }]
};
//# sourceMappingURL=calendar-range.component.js.map