import memoizeOne from 'memoize-one';
import { isVerticalPosition } from './utils';
export const getGuidelineSnaps = memoizeOne((guidelines, editorWidth, layout = 'center') => {
  const offset = editorWidth / 2;
  const getPositionX = position => {
    return isVerticalPosition(position) ? position.x : 0;
  };
  const calcXSnaps = guidelineReference => {
    const snapsWidth = guidelineReference.filter(g => g.width > 0).map(g => g.width);
    const uniqueSnapWidth = [...new Set(snapsWidth)];
    return snapsWidth.length ? uniqueSnapWidth : undefined;
  };
  const guidelinesSnapsReference = guidelines.map(guideline => {
    const positionX = getPositionX(guideline.position);
    if (['align-end', 'wrap-right'].includes(layout)) {
      return {
        guidelineKey: guideline.key,
        width: offset - positionX
      };
    } else if (['align-start', 'wrap-left'].includes(layout)) {
      return {
        guidelineKey: guideline.key,
        width: positionX + offset
      };
    }
    return {
      guidelineKey: guideline.key,
      width: Math.abs(positionX) * 2
    };
  });
  return {
    guidelineReference: guidelinesSnapsReference,
    snaps: {
      x: calcXSnaps(guidelinesSnapsReference)
    }
  };
});