import { MEDIA_DYNAMIC_GUIDELINE_PREFIX } from './constants';
/**
 * Returns keys of guidelines that are closest to the image and withthin the snapGap.
 * If both default and dynamic guidelines present, only returns default guidelines
 */
export var findClosestSnap = function findClosestSnap(mediaSingleWidth, snapArray, guidelineSnaps) {
  var snapGap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  var closestGapIndex = snapArray.reduce(function (prev, curr, index) {
    return Math.abs(curr - mediaSingleWidth) < Math.abs(snapArray[prev] - mediaSingleWidth) ? index : prev;
  }, 0);
  var gap = Math.abs(snapArray[closestGapIndex] - mediaSingleWidth);
  if (gap < snapGap) {
    var snappingWidth = snapArray[closestGapIndex];
    var guidelineKeys = [];
    // find wich guideline have the matching snap width
    guidelineSnaps.forEach(function (gs) {
      if (gs.width === snappingWidth) {
        guidelineKeys.push(gs.guidelineKey);
      }
    });
    var defaultGuidelineKeys = guidelineKeys.filter(function (guidelineKey) {
      return !guidelineKey.startsWith(MEDIA_DYNAMIC_GUIDELINE_PREFIX);
    });
    return {
      gap: gap,
      // only highlight default guidelines
      // when there are both default and dynamic guidelines to be highlighted
      keys: defaultGuidelineKeys.length && defaultGuidelineKeys.length < guidelineKeys.length ? defaultGuidelineKeys : guidelineKeys
    };
  }
  return {
    gap: gap,
    keys: []
  };
};