function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { eachDayOfInterval, eachMonthOfInterval, eachWeekOfInterval } from 'date-fns';
import { clamp, padStart } from 'lodash';
import { TIMELINE_ACTIONS } from './types';
import { WEEK_START } from '../constants';
import { buildScaleOptions } from './timeline_scales';
var HOURS_IN_DAY = 24;
export function getMonths(start, end) {
  return eachMonthOfInterval({
    start: start,
    end: end
  }).map(function (date) {
    return {
      monthIndex: date.getMonth(),
      startDate: date
    };
  });
}
export function getWeeks(start, end) {
  var interval = {
    start: start,
    end: end
  };
  return eachWeekOfInterval(interval, {
    weekStartsOn: WEEK_START
  }).map(function (date, weekIndex) {
    return {
      weekIndex: weekIndex,
      startDate: date
    };
  });
}
export function getDays(start, end) {
  return eachDayOfInterval({
    start: start,
    end: end
  }).map(function (date) {
    return {
      dayIndex: date.getDate() - 1,
      date: date
    };
  });
}
export function getHours(blockSize) {
  if (!blockSize) {
    return [];
  }
  var numberOfBlocks = HOURS_IN_DAY / blockSize;
  return Array.from(Array(numberOfBlocks).keys()).map(function (blockNumber) {
    var timeHours = blockNumber * blockSize;
    return {
      hourIndex: blockNumber,
      time: "".concat(padStart(timeHours.toString(), 2, '0'), ":00")
    };
  });
}
export function buildCalendar(_ref) {
  var from = _ref.from,
    to = _ref.to,
    hoursBlockSize = _ref.hoursBlockSize;
  return {
    months: getMonths(from, to),
    weeks: getWeeks(from, to),
    days: getDays(from, to),
    hours: getHours(hoursBlockSize)
  };
}
function transformOptions(options, currentScaleOption) {
  return _objectSpread(_objectSpread({}, options), {}, {
    endDate: options.endDate ? currentScaleOption.to : null,
    startDate: currentScaleOption.from
  });
}
export function reducer(state, action) {
  switch (action.type) {
    case TIMELINE_ACTIONS.CHANGE_WIDTH:
    case TIMELINE_ACTIONS.INITIALISE:
      {
        var _state$currentScaleIn;
        var scaleOptions = buildScaleOptions(state.options, action.width);
        var currentScaleIndex = (_state$currentScaleIn = state.currentScaleIndex) !== null && _state$currentScaleIn !== void 0 ? _state$currentScaleIn : scaleOptions.findIndex(function (_ref2) {
          var isDefault = _ref2.isDefault;
          return isDefault;
        });
        return _objectSpread(_objectSpread(_objectSpread({}, state), buildCalendar(scaleOptions[currentScaleIndex])), {}, {
          currentScaleIndex: currentScaleIndex,
          scaleOptions: scaleOptions,
          currentScaleOption: scaleOptions[currentScaleIndex],
          width: action.width
        });
      }
    case TIMELINE_ACTIONS.CHANGE_DATE:
    case TIMELINE_ACTIONS.CHANGE_START_DATE:
    case TIMELINE_ACTIONS.GET_PREV:
    case TIMELINE_ACTIONS.GET_NEXT:
      if (state.currentScaleIndex === null) {
        throw new Error('Timeline currentScaleIndex cannot be null');
      }
      return _objectSpread(_objectSpread(_objectSpread({}, state), buildCalendar(action.scaleOptions[state.currentScaleIndex])), {}, {
        currentScaleOption: action.scaleOptions[state.currentScaleIndex],
        options: transformOptions(state.options, action.scaleOptions[state.currentScaleIndex]),
        scaleOptions: action.scaleOptions
      });
    case TIMELINE_ACTIONS.SCALE:
      {
        if (state.currentScaleIndex === null) {
          throw new Error('Timeline currentScaleIndex cannot be null');
        }
        var newScaleIndex = clamp(action.getScaleIndex(state.currentScaleIndex), 0, state.scaleOptions.length - 1);
        return _objectSpread(_objectSpread(_objectSpread({}, state), buildCalendar(state.scaleOptions[newScaleIndex])), {}, {
          currentScaleIndex: newScaleIndex,
          currentScaleOption: state.scaleOptions[newScaleIndex],
          options: transformOptions(state.options, state.scaleOptions[state.currentScaleIndex])
        });
      }
    default:
      throw new Error('Unknown reducer action type');
  }
}