import { __extends } from "tslib";
import ModulDate, { DatePrecision } from './../../../../utils/modul-date/modul-date';
import AbstractCalendarState from './abstract-calendar-state';
import { CalendarEvent, CalendarType } from './calendar-state';
var DateRangePosition;
(function (DateRangePosition) {
    DateRangePosition["BEGIN"] = "begin";
    DateRangePosition["END"] = "end";
})(DateRangePosition || (DateRangePosition = {}));
var CalendarRangeDateState = /** @class */ (function (_super) {
    __extends(CalendarRangeDateState, _super);
    function CalendarRangeDateState() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.currentDateHiglighted = new ModulDate();
        return _this;
    }
    CalendarRangeDateState.prototype.updateState = function (value, minDate, maxDate) {
        this.initDates(value, minDate, maxDate);
    };
    CalendarRangeDateState.prototype.assembleValue = function () {
        return {
            begin: this.currentRange.begin ? this.currentRange.begin.toString() : '',
            end: this.currentRange.end ? this.currentRange.end.toString() : ''
        };
    };
    CalendarRangeDateState.prototype.selectDay = function (selectedDay) {
        _super.prototype.selectDay.call(this, selectedDay);
        if (!selectedDay.isDisabled) {
            var newDate = this.selectedDayToDate(selectedDay);
            this.currentRange = this.updateRangeModel(this.currentRange, newDate);
            this.currentRange = this.reOrderRangeDates(this.currentRange);
            this.updateCurrentlyDisplayedDate(newDate.fullYear(), newDate.month(), newDate.day());
            this.emitUpdate(this.assembleValue());
        }
    };
    CalendarRangeDateState.prototype.highlightDate = function (selectedDay) {
        this.currentDateHiglighted = this.selectedDayToDate(selectedDay);
    };
    CalendarRangeDateState.prototype.overrideCalendarEvents = function (events) {
        events[CalendarEvent.DAY_MOUSE_ENTER] = this.highlightDate.bind(this);
        events[CalendarEvent.DAY_KEYBOARD_TAB] = this.highlightDate.bind(this);
        return events;
    };
    CalendarRangeDateState.prototype.initCurrentValue = function (value) {
        var rangeCache = Object.assign({}, this.currentRange);
        if (value) {
            this.currentRange = {
                begin: this.initDateRange(value, DateRangePosition.BEGIN),
                end: this.initDateRange(value, DateRangePosition.END)
            };
            if (this.currentRange.begin) {
                this.currentDateHiglighted = new ModulDate(this.currentRange.begin);
            }
            else if (this.currentRange.end) {
                this.currentDateHiglighted = new ModulDate(this.currentRange.end);
            }
        }
        else {
            this.currentRange = { begin: undefined, end: undefined };
        }
    };
    CalendarRangeDateState.prototype.initCurrentlyDisplayedDate = function () {
        if (this.currentRange.begin) {
            this.updateCurrentlyDisplayedDate(this.currentRange.begin.fullYear(), this.currentRange.begin.month(), this.currentRange.begin.day());
        }
        else if (this.currentRange.end) {
            this.updateCurrentlyDisplayedDate(this.currentRange.end.fullYear(), this.currentRange.end.month(), this.currentRange.end.day());
        }
        else {
            this.updateCurrentlyDisplayedDate(this.now.fullYear(), this.now.month(), this.now.day());
        }
    };
    CalendarRangeDateState.prototype.isDaySelected = function (date) {
        return (!!this.currentRange.begin && date.isSame(this.currentRange.begin, DatePrecision.DAY))
            || (!!this.currentRange.end && date.isSame(this.currentRange.end, DatePrecision.DAY));
    };
    CalendarRangeDateState.prototype.isHighlighted = function (date) {
        return ((!!this.currentRange.begin && !this.currentRange.end) && this.betweenBeginAndHightlight(date))
            || ((!!this.currentRange.begin && !!this.currentRange.end) && date.isBetween(this.currentRange.begin, this.currentRange.end, DatePrecision.DAY));
    };
    CalendarRangeDateState.prototype.isSelectionStart = function (date) {
        return (!!this.currentRange.begin && date.isSame(this.currentRange.begin, DatePrecision.DAY));
    };
    CalendarRangeDateState.prototype.isSelectionEnd = function (date) {
        return (!!this.currentRange.end && date.isSame(this.currentRange.end, DatePrecision.DAY));
    };
    CalendarRangeDateState.prototype.calendarType = function () {
        return CalendarType.DATE_RANGE;
    };
    CalendarRangeDateState.prototype.betweenBeginAndHightlight = function (date) {
        if (this.currentRange.begin) {
            if (this.currentDateHiglighted.isBefore(this.currentRange.begin)) {
                return date.isBetween(this.currentDateHiglighted, this.currentRange.begin, DatePrecision.DAY);
            }
            else {
                return date.isBetween(this.currentRange.begin, this.currentDateHiglighted, DatePrecision.DAY);
            }
        }
        return false;
    };
    CalendarRangeDateState.prototype.initDateRange = function (dates, position) {
        return (dates[position]) ? new ModulDate(dates[position]) : undefined;
    };
    CalendarRangeDateState.prototype.updateRangeModel = function (range, date) {
        if (range.end) {
            range.end = undefined;
            range.begin = date;
        }
        else if (range.begin) {
            range.end = date;
        }
        else {
            range.begin = date;
        }
        return range;
    };
    CalendarRangeDateState.prototype.reOrderRangeDates = function (range) {
        if (range.begin
            && range.end
            && range.end.isBefore(range.begin, DatePrecision.DAY)) {
            range.end = [range.begin, range.begin = range.end][0];
        }
        return range;
    };
    return CalendarRangeDateState;
}(AbstractCalendarState));
export default CalendarRangeDateState;
//# sourceMappingURL=calendar-range-date-state.js.map