import registerCSSInterfaceWithDefaultTheme from 'react-dates/lib/utils/registerCSSInterfaceWithDefaultTheme';
import moment from 'moment';
import _ from 'lodash';
registerCSSInterfaceWithDefaultTheme(); // need this to check control is blur when TAB key away

export var isElementFocused = function isElementFocused(container) {
  var activeElement = document.activeElement;

  if (container === activeElement) {
    return true;
  }

  return activeElement && container ? container.contains(activeElement) : false;
};
export var isSameDate = function isSameDate(oldDate, newDate) {
  if (oldDate && newDate) {
    return moment(oldDate).isSame(newDate, 'date');
  }

  return !oldDate && !newDate;
};
export function dateRangePickerValueFormatter(startDate, endDate) {
  if (isSameDate(startDate, endDate)) {
    return startDate.format('L');
  }

  var formatDate = function formatDate(date) {
    return date ? date.format('L') : null;
  };

  return formatDate(startDate) + " - " + formatDate(endDate);
}
export var REACT_DATES_START_DATE_INPUT_MODE = 'startDate';
export function is24HoursMode(timeFormat) {
  return /H/.test(timeFormat);
}
export function getSiblingYearList(currentYear, yearBeforeCurrent, yearAfterCurrent) {
  return _.range(currentYear - yearAfterCurrent, currentYear + yearAfterCurrent);
}
export function isActiveRange(_ref) {
  var startDate = _ref.startDate,
      endDate = _ref.endDate,
      range = _ref.range;

  if (!startDate || !endDate) {
    return false;
  }

  var rangeStart = range.startDate,
      rangeEnd = range.endDate;
  return isSameDate(startDate, rangeStart) && isSameDate(endDate, rangeEnd);
}
export function parsePreDefinedRangeConfig(rangeConfig) {
  var range = _.clone(rangeConfig);

  if (_.isFunction(rangeConfig.startDate)) {
    range.startDate = rangeConfig.startDate(moment());
  }

  if (_.isFunction(rangeConfig.endDate)) {
    range.endDate = rangeConfig.endDate(moment());
  }

  return range;
}