import { akEditorDefaultLayoutWidth } from '@atlaskit/editor-shared-styles';
import { getMediaSinglePixelWidth, roundToNearest } from '../media-single';

/**
 *
 * @param mediaSingle the mediaSingle node
 * @param editorWidth default 760, only use default if the mediaSingle is using pixel width
 * @returns null or dimensions info
 */
export var getMediaSingleDimensions = function getMediaSingleDimensions(mediaSingle) {
  var editorWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : akEditorDefaultLayoutWidth;
  if (mediaSingle.type !== mediaSingle.type.schema.nodes.mediaSingle) {
    return null;
  }
  var mediaNode = mediaSingle.firstChild;
  var _ref = (mediaNode === null || mediaNode === void 0 ? void 0 : mediaNode.attrs) || {},
    width = _ref.width,
    height = _ref.height;

  // e.g. external image
  if (!width || !height) {
    return null;
  }
  var ratio = parseFloat((height / width).toFixed(2));
  if (!mediaSingle.attrs.width) {
    return {
      width: width,
      height: height,
      originalWidth: width,
      originalHeight: height,
      ratio: ratio
    };
  }
  var pixelWidth = getMediaSinglePixelWidth(mediaSingle.attrs.width, editorWidth, mediaSingle.attrs.widthType);
  return {
    width: roundToNearest(pixelWidth),
    height: roundToNearest(pixelWidth * ratio),
    originalWidth: width,
    originalHeight: height,
    ratio: ratio
  };
};