"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getMediaSingleDimensions = void 0;
var _editorSharedStyles = require("@atlaskit/editor-shared-styles");
var _mediaSingle = require("../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
 */
var getMediaSingleDimensions = exports.getMediaSingleDimensions = function getMediaSingleDimensions(mediaSingle) {
  var editorWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _editorSharedStyles.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 = (0, _mediaSingle.getMediaSinglePixelWidth)(mediaSingle.attrs.width, editorWidth, mediaSingle.attrs.widthType);
  return {
    width: (0, _mediaSingle.roundToNearest)(pixelWidth),
    height: (0, _mediaSingle.roundToNearest)(pixelWidth * ratio),
    originalWidth: width,
    originalHeight: height,
    ratio: ratio
  };
};