import { wrappedLayouts } from '../ui/MediaSingle/wrappedLayouts';
import { nonWrappedLayouts } from './nonWrappedLayouts';
import { shouldAddDefaultWrappedWidth } from './shouldAddDefaultWrappedWidth';
export const alignAttributes = (layout, oldAttrs, gridSize = 12, originalWidth, lineLength) => {
  let width = oldAttrs.width;
  const oldLayout = oldAttrs.layout;
  const oldLayoutIsNonWrapped = nonWrappedLayouts.indexOf(oldLayout) > -1;
  const newLayoutIsNonWrapped = nonWrappedLayouts.indexOf(layout) > -1;
  const newLayoutIsWrapped = wrappedLayouts.indexOf(layout) > -1;
  const oldLayoutIsWrapped = wrappedLayouts.indexOf(oldLayout) > -1;
  if (oldLayoutIsNonWrapped && shouldAddDefaultWrappedWidth(layout, originalWidth, lineLength)) {
    // 'full-width' or 'wide' or 'center' -> 'wrap-left' or 'wrap-right' or 'align-end' or 'align-start'
    if (!width || width >= 100 || oldLayout !== 'center' // == 'full-width' or 'wide'
    ) {
      width = 50;
    }
  } else if (layout !== oldLayout && ['full-width', 'wide'].indexOf(oldLayout) > -1) {
    // 'full-width' -> 'center' or 'wide'
    // 'wide' -> 'center' or 'full-width'
    // unset width
    width = undefined;
  } else if (width) {
    const cols = Math.round(width / 100 * gridSize);
    let targetCols = cols;
    if (oldLayoutIsWrapped && newLayoutIsNonWrapped) {
      // wrap -> center needs to align to even grid
      targetCols = Math.floor(targetCols / 2) * 2;
      width = undefined;
    } else if (oldLayoutIsNonWrapped && newLayoutIsWrapped) {
      // Can be here only if
      // 'full-width' or 'wide' or 'center' -> 'wrap-left' or 'wrap-right' or 'align-end' or 'align-start'
      // AND
      // !originalWidth || !lineLength || small image
      // AND
      // width defined!

      // cannot resize to full column width, and cannot resize to 1 column
      if (cols <= 1) {
        targetCols = 2;
      } else if (cols >= gridSize) {
        targetCols = 10;
      }
    }
    if (targetCols !== cols) {
      width = targetCols / gridSize * 100;
    }
  }
  return {
    ...oldAttrs,
    layout,
    width
  };
};
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { shouldAddDefaultWrappedWidth } from './shouldAddDefaultWrappedWidth';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { nonWrappedLayouts } from './nonWrappedLayouts';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { floatingLayouts } from './floatingLayouts';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { isRichMediaInsideOfBlockNode } from './isRichMediaInsideOfBlockNode';
// eslint-disable-next-line @atlaskit/editor/no-re-export
export { calculateSnapPoints } from './calculateSnapPoints';