import { formatDate } from '@transferwise/formatting';
import { PureComponent } from 'react';
import { injectIntl } from 'react-intl';
import { getDayNames } from '../../../common/dateUtils/getDayNames/getDayNames.mjs';
import { isWithinRange } from '../../../common/dateUtils/isWithinRange/isWithinRange.mjs';
import { getFocusableTime } from '../../getFocusableTime/getFocusableTime.mjs';
import { getStartOfDay } from '../../getStartOfDay/getStartOfDay.mjs';
import TableLink from '../../tableLink/TableLink.mjs';
import { jsxs, jsx } from 'react/jsx-runtime';

const SHORT_DAY_FORMAT = {
  day: 'numeric'
};
class DayCalendarTable extends PureComponent {
  getTableStructure = () => {
    const {
      viewMonth,
      viewYear
    } = this.props;
    let firstDayOfMonth = new Date(viewYear, viewMonth, 1).getDay();
    // JS Sunday is 0, we're setting it last
    if (firstDayOfMonth === 0) {
      firstDayOfMonth = 7;
    }
    const daysInMonth = new Date(viewYear, viewMonth + 1, 0).getDate();
    let week = [];
    const weeks = [];
    let i;
    // Pad first week
    for (i = 1; i < firstDayOfMonth; i += 1) {
      week.push(-1);
    }
    // Fill in days
    for (i = 1; i <= daysInMonth; i += 1) {
      week.push(i);
      if ((firstDayOfMonth + i - 1) % 7 === 0) {
        weeks.push(week);
        week = [];
      }
    }
    if (week.length > 0) {
      // Pad last week
      for (i = week.length; i < 7; i += 1) {
        week.push(-1);
      }
      weeks.push(week);
    }
    return weeks;
  };
  days = getDayNames(this.props.intl.locale, 'short');
  daysShort = getDayNames(this.props.intl.locale, 'narrow');
  daysLong = getDayNames(this.props.intl.locale, 'long');
  selectDay = day => {
    const {
      viewMonth,
      viewYear,
      onSelect
    } = this.props;
    onSelect(new Date(viewYear, viewMonth, day));
  };
  isDisabled = day => {
    if (day < 0) {
      return true;
    }
    const {
      min,
      max,
      viewMonth,
      viewYear
    } = this.props;
    const date = new Date(viewYear, viewMonth, day);
    return !isWithinRange(date, min, max);
  };
  isActive = day => {
    const {
      selectedDate,
      viewMonth,
      viewYear
    } = this.props;
    return !!(selectedDate && Number(new Date(viewYear, viewMonth, day)) === Number(selectedDate));
  };
  isToday = day => {
    const {
      viewMonth,
      viewYear
    } = this.props;
    return Number(getStartOfDay(new Date())) === Number(new Date(viewYear, viewMonth, day));
  };
  getAutofocusDay = weeks => {
    const days = weeks.flatMap(week => week);
    return getFocusableTime({
      isActive: this.isActive,
      isNow: this.isToday,
      isDisabled: this.isDisabled,
      timeSpan: days
    });
  };
  render() {
    const {
      viewMonth,
      viewYear,
      intl
    } = this.props;
    const weeks = this.getTableStructure();
    const autoFocusDay = this.getAutofocusDay(weeks);
    return /*#__PURE__*/jsxs("table", {
      className: "table table-condensed table-bordered tw-date-lookup-calendar m-b-0",
      children: [/*#__PURE__*/jsx("thead", {
        children: /*#__PURE__*/jsx("tr", {
          children: this.days.map((day, index) => /*#__PURE__*/jsxs("th", {
            className: index > 4 ? 'text-xs-center np-text-body-default' : 'text-xs-center np-text-body-default-bold',
            children: [/*#__PURE__*/jsx("span", {
              className: "hidden-xs",
              children: /*#__PURE__*/jsx("abbr", {
                title: this.daysLong[index],
                children: day.slice(0, 3)
              })
            }), /*#__PURE__*/jsx("span", {
              className: "visible-xs-inline-block",
              children: /*#__PURE__*/jsx("abbr", {
                title: this.daysLong[index],
                children: this.daysShort[index].slice(0, 2)
              })
            })]
          }, day))
        })
      }), /*#__PURE__*/jsx("tbody", {
        children: weeks.map((week, weekIndex) => /*#__PURE__*/jsx("tr", {
          children: week.map((day, dayIndex) => /*#__PURE__*/jsx("td", {
            className: dayIndex > 4 ? 'weekend' : '',
            children: day !== -1 && /*#__PURE__*/jsx(TableLink, {
              item: day,
              type: "day",
              title: formatDate(new Date(viewYear, viewMonth, day), intl.locale, SHORT_DAY_FORMAT),
              autofocus: day === autoFocusDay,
              longTitle: formatDate(new Date(viewYear, viewMonth, day), intl.locale),
              active: this.isActive(day),
              disabled: this.isDisabled(day),
              today: this.isToday(day),
              onClick: this.selectDay
            })
          }, dayIndex))
        }, weekIndex))
      })]
    });
  }
}
var DayCalendarTable$1 = injectIntl(DayCalendarTable);

export { DayCalendarTable$1 as default };
//# sourceMappingURL=DayCalendarTable.mjs.map
