import { formatDate } from '@transferwise/formatting';
import { useIntl } from 'react-intl';
import { getFocusableTime } from '../../getFocusableTime/getFocusableTime.mjs';
import TableLink from '../../tableLink/TableLink.mjs';
import { jsxs, jsx } from '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
  } = useIntl();
  const startYear = viewYear - viewYear % 20;
  const getLink = year => {
    return /*#__PURE__*/jsx(TableLink, {
      item: year,
      type: "year",
      title: 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 !!(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({
      isActive,
      isNow: isThisYear,
      isDisabled,
      timeSpan: years
    });
  })();
  return /*#__PURE__*/jsxs("table", {
    className: "table table-condensed table-bordered tw-date-lookup-calendar m-b-0",
    children: [/*#__PURE__*/jsx("thead", {
      className: "sr-only",
      children: /*#__PURE__*/jsx("tr", {
        children: /*#__PURE__*/jsx("th", {
          colSpan: COLS,
          children: placeholder
        })
      })
    }), /*#__PURE__*/jsx("tbody", {
      children: Array.from({
        length: ROWS
      }, (_, rowIndex) => /*#__PURE__*/jsx("tr", {
        children: Array.from({
          length: COLS
        }, (_, colIndex) => /*#__PURE__*/jsx("td", {
          children: getLink(startYear + rowIndex * COLS + colIndex)
        }, colIndex))
      }, rowIndex))
    })]
  });
};

export { YearCalendarTable as default };
//# sourceMappingURL=YearCalendarTable.mjs.map
