var _excluded = ["render"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
import React, { useContext, memo } from 'react';
import { differenceInDays, endOfMonth, min as minDate, max } from 'date-fns';
import { getKey } from '../../helpers';
import { TimelineContext } from './context';
import { TimelineHeaderRow } from './TimelineHeaderRow';
import { MONTH_SIZE, TimelineMonth } from './TimelineMonth';
import { StyledMonths } from './partials/StyledMonths';
var MONTH_SMALL_THRESHOLD = 30;
var MONTH_MEDIUM_THRESHOLD = 160;
function getSize(monthWidths) {
  var smallest = Math.min.apply(Math, _toConsumableArray(monthWidths.map(function (_ref) {
    var monthWidth = _ref.monthWidth;
    return monthWidth;
  })));
  if (smallest < MONTH_SMALL_THRESHOLD) {
    return MONTH_SIZE.SMALL;
  }
  if (smallest < MONTH_MEDIUM_THRESHOLD) {
    return MONTH_SIZE.MEDIUM;
  }
  return MONTH_SIZE.LARGE;
}
export var TimelineMonths = /*#__PURE__*/memo(function (_ref2) {
  var render = _ref2.render,
    rest = _objectWithoutProperties(_ref2, _excluded);
  var _useContext = useContext(TimelineContext),
    _useContext$state = _useContext.state,
    currentScaleOption = _useContext$state.currentScaleOption,
    days = _useContext$state.days,
    months = _useContext$state.months;
  if (!currentScaleOption) {
    return null;
  }
  var monthWidths = months.map(function (_ref3) {
    var startDate = _ref3.startDate;
    var firstDateDisplayed = max([startDate, days[0].date]);
    var lastDateDisplayed = minDate([endOfMonth(startDate), days[days.length - 1].date]);
    var daysTotal = differenceInDays(lastDateDisplayed, firstDateDisplayed) + 1;
    var monthWidth = currentScaleOption.widths.day * daysTotal;
    return {
      daysTotal: daysTotal,
      monthWidth: monthWidth
    };
  });
  var size = getSize(monthWidths);
  return /*#__PURE__*/React.createElement(TimelineHeaderRow, _extends({
    className: "timeline__months",
    name: "Months"
  }, rest), /*#__PURE__*/React.createElement(StyledMonths, null, months.map(function (_ref4, index) {
    var startDate = _ref4.startDate;
    return /*#__PURE__*/React.createElement(TimelineMonth, {
      days: days,
      dayWidth: currentScaleOption.widths.day,
      daysTotal: monthWidths[index].daysTotal,
      index: index,
      key: getKey('timeline-month', startDate.toString()),
      render: render,
      size: size,
      startDate: startDate
    });
  })));
});
TimelineMonths.displayName = 'TimelineMonths';