function closestIndexTo(dirtyDateToCompare, dirtyDatesArray) {
  __WEBPACK_IMPORTED_MODULE_1__lib_requiredArgs_index_js__["a" /* default */](2, arguments);
  var dateToCompare = __WEBPACK_IMPORTED_MODULE_0__toDate_index_js__["a" /* default */](dirtyDateToCompare);

  if (isNaN(dateToCompare)) {
    return NaN;
  }

  var timeToCompare = dateToCompare.getTime();
  var datesArray; // `dirtyDatesArray` is undefined or null

  if (dirtyDatesArray == null) {
    datesArray = []; // `dirtyDatesArray` is Array, Set or Map, or object with custom `forEach` method
  } else if (typeof dirtyDatesArray.forEach === 'function') {
    datesArray = dirtyDatesArray; // If `dirtyDatesArray` is Array-like Object, convert to Array. Otherwise, make it empty Array
  } else {
    datesArray = Array.prototype.slice.call(dirtyDatesArray);
  }

  var result;
  var minDistance;
  datesArray.forEach(function (dirtyDate, index) {
    var currentDate = __WEBPACK_IMPORTED_MODULE_0__toDate_index_js__["a" /* default */](dirtyDate);

    if (isNaN(currentDate)) {
      result = NaN;
      minDistance = NaN;
      return;
    }

    var distance = Math.abs(timeToCompare - currentDate.getTime());

    if (result == null || distance < minDistance) {
      result = index;
      minDistance = distance;
    }
  });
  return result;
}
@name closestTo
@category Common Helpers
@summary Return a date from the array closest to the given date.

@description
Return a date from the array closest to the given date.

### v2.0.0 breaking changes:

- [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).

- Now, `closestTo` doesn't throw an exception
  when the second argument is not an array, and returns Invalid Date instead.

@param {Date|Number} dateToCompare - the date to compare with
@param {Date[]|Number[]} datesArray - the array to search
@returns {Date} the date from the array closest to the given date
@throws {TypeError} 2 arguments required

@example
// Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?
var dateToCompare = new Date(2015, 8, 6)
var result = closestTo(dateToCompare, [
  new Date(2000, 0, 1),
  new Date(2030, 0, 1)
])
//=> Tue Jan 01 2030 00:00:00