import React from 'react';
import { differenceInDays, endOfWeek, max, min } from 'date-fns';
import { formatPx, isOdd } from './helpers';
import { StyledWeekColumn } from './partials/StyledWeekColumn';
import { StyledWeekColumnsWrapper } from './partials/StyledWeekColumnsWrapper';
import { StyledWeekColumns } from './partials/StyledWeekColumns';
import { TimelineContext } from './context';
import { WEEK_START } from './constants';
import { withKey } from '../../helpers';
function renderDefault(_ref) {
  var _ = _ref.index,
    isOddNumber = _ref.isOddNumber,
    offsetPx = _ref.offsetPx,
    widthPx = _ref.widthPx;
  return /*#__PURE__*/React.createElement(StyledWeekColumn, {
    $isOddNumber: isOddNumber,
    $marginLeftPx: offsetPx,
    $widthPx: widthPx
  });
}
export var TimelineWeekColumns = function TimelineWeekColumns(_ref2) {
  var render = _ref2.render;
  return /*#__PURE__*/React.createElement(TimelineContext.Consumer, null, function (_ref3) {
    var _ref3$state = _ref3.state,
      currentScaleOption = _ref3$state.currentScaleOption,
      days = _ref3$state.days,
      weeks = _ref3$state.weeks;
    return currentScaleOption && /*#__PURE__*/React.createElement(StyledWeekColumnsWrapper, null, /*#__PURE__*/React.createElement(StyledWeekColumns, {
      role: "presentation"
    }, weeks.map(function (_ref4, index) {
      var startDate = _ref4.startDate;
      var lastDateDisplayed = min([endOfWeek(startDate, {
        weekStartsOn: WEEK_START
      }), days[days.length - 1].date]);
      var offsetInDays = differenceInDays(startDate, max([startDate, days[0].date]));
      var offsetPx = formatPx(currentScaleOption.widths.day, offsetInDays);
      var widthPx = formatPx(currentScaleOption.widths.day, differenceInDays(lastDateDisplayed, startDate) + 1);
      var isOddNumber = isOdd(index);
      var column = render ? render({
        index: index,
        isOddNumber: isOddNumber,
        offsetPx: offsetPx,
        widthPx: widthPx
      }) : renderDefault({
        index: index,
        isOddNumber: isOddNumber,
        offsetPx: offsetPx,
        widthPx: widthPx
      });
      return withKey(column, 'timeline-column', startDate.toString());
    })));
  });
};
TimelineWeekColumns.displayName = 'TimelineRowWeeks';