'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var formatting = require('@transferwise/formatting');
var reactIntl = require('react-intl');
var getFocusableTime = require('../../getFocusableTime/getFocusableTime.js');
var TableLink = require('../../tableLink/TableLink.js');
var jsxRuntime = require('react/jsx-runtime');

const ROWS = 5;
const COLS = 4;
const YEAR_ONLY_FORMAT = {
  year: 'numeric'
};
const YearCalendarTable = ({
  selectedDate,
  min,
  max,
  viewYear,
  placeholder,
  onSelect
}) => {
  const {
    locale
  } = reactIntl.useIntl();
  const startYear = viewYear - viewYear % 20;
  const getLink = year => {
    return /*#__PURE__*/jsxRuntime.jsx(TableLink.default, {
      item: year,
      type: "year",
      title: formatting.formatDate(new Date(year, 0), locale, YEAR_ONLY_FORMAT),
      active: isActive(year),
      disabled: isDisabled(year),
      today: isThisYear(year),
      autofocus: autofocusYear === year,
      onClick: onSelect
    });
  };
  const isActive = year => {
    return !!(selectedDate && year === selectedDate.getFullYear());
  };
  const isThisYear = year => {
    return year === new Date().getFullYear();
  };
  const isDisabled = year => {
    return !!(min && year < min.getFullYear() || max && year > max.getFullYear());
  };
  const autofocusYear = (() => {
    const years = Array.from({
      length: ROWS * COLS
    }, (_, index) => startYear + index);
    return getFocusableTime.getFocusableTime({
      isActive,
      isNow: isThisYear,
      isDisabled,
      timeSpan: years
    });
  })();
  return /*#__PURE__*/jsxRuntime.jsxs("table", {
    className: "table table-condensed table-bordered tw-date-lookup-calendar m-b-0",
    children: [/*#__PURE__*/jsxRuntime.jsx("thead", {
      className: "sr-only",
      children: /*#__PURE__*/jsxRuntime.jsx("tr", {
        children: /*#__PURE__*/jsxRuntime.jsx("th", {
          colSpan: COLS,
          children: placeholder
        })
      })
    }), /*#__PURE__*/jsxRuntime.jsx("tbody", {
      children: Array.from({
        length: ROWS
      }, (_, rowIndex) => /*#__PURE__*/jsxRuntime.jsx("tr", {
        children: Array.from({
          length: COLS
        }, (_, colIndex) => /*#__PURE__*/jsxRuntime.jsx("td", {
          children: getLink(startYear + rowIndex * COLS + colIndex)
        }, colIndex))
      }, rowIndex))
    })]
  });
};

exports.default = YearCalendarTable;
//# sourceMappingURL=YearCalendarTable.js.map
