"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.dateRangePickerValueFormatter = dateRangePickerValueFormatter;
exports.is24HoursMode = is24HoursMode;
exports.getSiblingYearList = getSiblingYearList;
exports.isActiveRange = isActiveRange;
exports.parsePreDefinedRangeConfig = parsePreDefinedRangeConfig;
exports.REACT_DATES_START_DATE_INPUT_MODE = exports.isSameDate = exports.isElementFocused = void 0;

var _registerCSSInterfaceWithDefaultTheme = _interopRequireDefault(require("react-dates/lib/utils/registerCSSInterfaceWithDefaultTheme"));

var _moment = _interopRequireDefault(require("moment"));

var _lodash = _interopRequireDefault(require("lodash"));

(0, _registerCSSInterfaceWithDefaultTheme.default)(); // need this to check control is blur when TAB key away

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

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

  return activeElement && container ? container.contains(activeElement) : false;
};

exports.isElementFocused = isElementFocused;

var isSameDate = function isSameDate(oldDate, newDate) {
  if (oldDate && newDate) {
    return (0, _moment.default)(oldDate).isSame(newDate, 'date');
  }

  return !oldDate && !newDate;
};

exports.isSameDate = isSameDate;

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);
}

var REACT_DATES_START_DATE_INPUT_MODE = 'startDate';
exports.REACT_DATES_START_DATE_INPUT_MODE = REACT_DATES_START_DATE_INPUT_MODE;

function is24HoursMode(timeFormat) {
  return /H/.test(timeFormat);
}

function getSiblingYearList(currentYear, yearBeforeCurrent, yearAfterCurrent) {
  return _lodash.default.range(currentYear - yearAfterCurrent, currentYear + yearAfterCurrent);
}

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);
}

function parsePreDefinedRangeConfig(rangeConfig) {
  var range = _lodash.default.clone(rangeConfig);

  if (_lodash.default.isFunction(rangeConfig.startDate)) {
    range.startDate = rangeConfig.startDate((0, _moment.default)());
  }

  if (_lodash.default.isFunction(rangeConfig.endDate)) {
    range.endDate = rangeConfig.endDate((0, _moment.default)());
  }

  return range;
}