import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { wrappedLayouts } from '../ui/MediaSingle/wrappedLayouts';
import { nonWrappedLayouts } from './nonWrappedLayouts';
import { shouldAddDefaultWrappedWidth } from './shouldAddDefaultWrappedWidth';
export var alignAttributes = function alignAttributes(layout, oldAttrs) {
  var gridSize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 12;
  var originalWidth = arguments.length > 3 ? arguments[3] : undefined;
  var lineLength = arguments.length > 4 ? arguments[4] : undefined;
  var width = oldAttrs.width;
  var oldLayout = oldAttrs.layout;
  var oldLayoutIsNonWrapped = nonWrappedLayouts.indexOf(oldLayout) > -1;
  var newLayoutIsNonWrapped = nonWrappedLayouts.indexOf(layout) > -1;
  var newLayoutIsWrapped = wrappedLayouts.indexOf(layout) > -1;
  var 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) {
    var cols = Math.round(width / 100 * gridSize);
    var 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 _objectSpread(_objectSpread({}, oldAttrs), {}, {
    layout: layout,
    width: 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';