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;
};
import { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { MonthViewGenerator } from '../../../../../helpers/calendar';
import { createEnterKeyHandlerToTriggerClick } from '../../../../../utils/accessibility';
import { isCalendarDateType } from '../../../../../types/type-guard';
export class VegaDatePickerMonthItemRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.monthViewGenerator = new MonthViewGenerator();
  }
  render(type) {
    const period = type === 'end' ? this.currentPeriod.next() : this.currentPeriod;
    const periodView = this.monthViewGenerator.generate(period);
    return (h("div", { class: "month-item" },
      this.renderCurrentYearMonth(period),
      this.renderWeek(periodView),
      this.renderDays(periodView, period)));
  }
  renderCurrentYearMonth(period) {
    return (this.isDualMonths && (h("vega-box", { class: "current-month-year" },
      this.translationSlimmer.t(period.current.getMonthName()),
      ' ',
      period.current.year)));
  }
  renderWeek(periodView) {
    return (h("vega-grid", { class: "month-item-weekdays-row", column: '7-auto', row: 1, "aria-hidden": "true" }, periodView.weekdays.map((weekDay) => {
      return (h("div", { key: weekDay }, this.translationSlimmer.t(`${weekDay}-short`)));
    })));
  }
  renderDays(periodView, period) {
    return (h("vega-grid", { class: "container__days", column: '7-auto', row: this.staticHeight ? 6 : '6-auto' }, periodView.dateItems
      .flat()
      .filter((day) => day.month <= period.current.month || day.year < period.current.year)
      .map((day) => this.renderItem(day, period))));
  }
  renderItem(day, period) {
    const dateIndicator = this.dateIndicator(day.toDate());
    const dayElement = this.renderDay(day, period);
    return dateIndicator && dateIndicator.text
      ? this.renderDayWithIndicator(dateIndicator, dayElement)
      : dayElement;
  }
  renderDayWithIndicator(dateIndicator, dayElement) {
    return h("vega-tooltip", Object.assign({}, dateIndicator), dayElement);
  }
  renderDay(day, period) {
    const isCurrentMonth = day.month === period.current.month;
    const isLockedDate = this.isBeforeMinDate(day) || this.isAfterMaxDate(day);
    const currentRangeDates = this.getCurrentRangeDates();
    const isDisabled = this.isDateDisabled(day.toDate());
    const isInRange = day.isAfter(currentRangeDates[0]) && day.isBefore(currentRangeDates[1]);
    const canInterActive = isCurrentMonth && !isLockedDate && !isDisabled;
    const weekDayShortName = this.translationSlimmer.t(`${day.getWeekDayShortName()}-full`);
    return (h("div", { key: `${day.month}-${day.date}`, class: isCurrentMonth && {
        'day-item': true,
        'is-today': day.equal(this.today),
        'is-start-date': day.equal(currentRangeDates[0]),
        'is-in-range': isInRange,
        'is-end-date': day.equal(currentRangeDates[1]),
        'is-locked': isLockedDate,
        'is-disabled': isDisabled,
      }, 
      /* eslint-disable-next-line react/jsx-no-bind */
      onClick: () => canInterActive && this.selectionController.onDayClick(day), 
      /* eslint-disable-next-line react/jsx-no-bind */
      onMouseEnter: () => canInterActive && this.selectionController.onDayHover(day), onKeyDown: createEnterKeyHandlerToTriggerClick(), role: "menuitem", "aria-label": `${day.getMonthName()} ${day.date} ${day.year} ${weekDayShortName}`, "aria-hidden": isCurrentMonth ? undefined : 'true', "aria-disabled": isLockedDate || isDisabled ? 'true' : undefined, tabIndex: canInterActive ? 0 : -1 },
      h("div", { class: "day-content", "aria-hidden": "true" }, isCurrentMonth ? day.date : '')));
  }
  getCurrentRangeDates() {
    const startDate = this.calendarSelection.startDate;
    const endDate = this.calendarSelection.endDate;
    if (isCalendarDateType(startDate) && !endDate && this.hoverDate) {
      return this.hoverDate.isBefore(startDate)
        ? [this.hoverDate, startDate]
        : [startDate, this.hoverDate];
    }
    else if (!isCalendarDateType(startDate) && endDate && this.hoverDate) {
      return this.hoverDate.isBefore(endDate) ? [this.hoverDate, endDate] : [endDate, this.hoverDate];
    }
    else {
      return [startDate, endDate];
    }
  }
  isBeforeMinDate(day) {
    return this.minCalendarDate && day.isBefore(this.minCalendarDate);
  }
  isAfterMaxDate(day) {
    return this.maxCalendarDate && day.isAfter(this.maxCalendarDate);
  }
}
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "currentPeriod", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "today", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "translationSlimmer", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "hoverDate", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "maxCalendarDate", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "minCalendarDate", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "calendarSelection", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "staticHeight", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "mode", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "size", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "isDualMonths", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "isDateDisabled", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerMonthItemRenderer.prototype, "dateIndicator", void 0);
