import { useContext } from 'react';
import { differenceInCalendarDays, endOfDay, isAfter, isBefore } from 'date-fns';
import { TimelineContext } from '../context';
import { formatPx } from '../helpers';
var timeOffset = function timeOffset(date) {
  return 1 / 24 * new Date(date).getHours();
};
function getWidth(startDate, endDate) {
  return differenceInCalendarDays(new Date(endDate), new Date(startDate)) - timeOffset(startDate) + timeOffset(endDate);
}
function getOffset(startDate, timelineStart) {
  return differenceInCalendarDays(new Date(startDate), new Date(timelineStart)) + timeOffset(startDate);
}
export function useTimelinePosition(startDate) {
  var endDate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : startDate;
  var _useContext = useContext(TimelineContext),
    currentScaleOption = _useContext.state.currentScaleOption;
  if (!currentScaleOption) {
    throw new Error('Timeline has no current scale option');
  }
  var firstDateDisplayed = currentScaleOption.from,
    lastDateDisplayed = currentScaleOption.to;
  var startsBeforeStart = isBefore(startDate, firstDateDisplayed);
  var startsAfterEnd = isAfter(startDate, endOfDay(lastDateDisplayed));
  var endsBeforeStart = isBefore(endDate, firstDateDisplayed);
  var endsAfterEnd = isAfter(endDate, endOfDay(lastDateDisplayed));
  var width = startsBeforeStart ? getWidth(firstDateDisplayed, endDate) : getWidth(startDate, endDate);
  var offset = startsBeforeStart ? 0 : getOffset(startDate, firstDateDisplayed);
  var maxWidth = startsBeforeStart && endsAfterEnd ? getWidth(firstDateDisplayed, lastDateDisplayed) + 1 : getWidth(startDate, lastDateDisplayed) + 1;
  return {
    endsAfterEnd: endsAfterEnd,
    endsBeforeStart: endsBeforeStart,
    startsAfterEnd: startsAfterEnd,
    startsBeforeStart: startsBeforeStart,
    maxWidth: formatPx(currentScaleOption.widths.day, maxWidth),
    offset: formatPx(currentScaleOption.widths.day, offset),
    width: formatPx(currentScaleOption.widths.day, width)
  };
}