"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "calcMediaSingleMaxWidth", {
  enumerable: true,
  get: function get() {
    return _calcMediaSingleMaxWidth.calcMediaSingleMaxWidth;
  }
});
exports.calcMediaSinglePixelWidth = void 0;
Object.defineProperty(exports, "calcMinWidth", {
  enumerable: true,
  get: function get() {
    return _calcMinWidth.calcMinWidth;
  }
});
Object.defineProperty(exports, "calculateOffsetLeft", {
  enumerable: true,
  get: function get() {
    return _calculateOffsetLeft.calculateOffsetLeft;
  }
});
Object.defineProperty(exports, "currentMediaNodeWithPos", {
  enumerable: true,
  get: function get() {
    return _currentMediaNodeWithPos.currentMediaNodeWithPos;
  }
});
Object.defineProperty(exports, "getMaxWidthForNestedNode", {
  enumerable: true,
  get: function get() {
    return _getMaxWidthForNestedNode.getMaxWidthForNestedNode;
  }
});
exports.getMaxWidthForNestedNodeNext = void 0;
Object.defineProperty(exports, "getMediaSingleInitialWidth", {
  enumerable: true,
  get: function get() {
    return _getMediaSingleInitialWidth.getMediaSingleInitialWidth;
  }
});
Object.defineProperty(exports, "getMediaSinglePixelWidth", {
  enumerable: true,
  get: function get() {
    return _getMediaSinglePixelWidth.getMediaSinglePixelWidth;
  }
});
exports.getParentWidthForNestedMediaSingleNodeForInsertion = exports.getParentWidthForNestedMediaSingleNode = void 0;
Object.defineProperty(exports, "roundToNearest", {
  enumerable: true,
  get: function get() {
    return _roundToNearest.roundToNearest;
  }
});
var _editorSharedStyles = require("@atlaskit/editor-shared-styles");
var _floatingLayouts = require("../utils/floatingLayouts");
var _calcMediaSingleMaxWidth = require("./calcMediaSingleMaxWidth");
var _constants = require("./constants");
var _getMediaSingleInitialWidth = require("./getMediaSingleInitialWidth");
var _getMediaSinglePixelWidth = require("./getMediaSinglePixelWidth");
var _calculateOffsetLeft = require("./calculateOffsetLeft");
var _roundToNearest = require("./roundToNearest");
var _calcMinWidth = require("./calcMinWidth");
var _getMaxWidthForNestedNode = require("./getMaxWidthForNestedNode");
var _currentMediaNodeWithPos = require("./currentMediaNodeWithPos");
/**
 * Convert width attribute to pixel value for legacy (resized or not resisized) and new media single node for new experience
 * @param width node width attribute
 * @param widthType node widthType attribute
 * @param origWidth original media width
 * @param layout node layout attribute
 * @param contentWidth editor content width
 * @param containerWidth editor container width
 * @param gutterOffset gap between resizer handle and media
 * @returns pixel width of the node
 */
var calcMediaSinglePixelWidth = exports.calcMediaSinglePixelWidth = function calcMediaSinglePixelWidth(_ref) {
  var width = _ref.width,
    _ref$widthType = _ref.widthType,
    widthType = _ref$widthType === void 0 ? 'percentage' : _ref$widthType,
    origWidth = _ref.origWidth,
    layout = _ref.layout,
    contentWidth = _ref.contentWidth,
    containerWidth = _ref.containerWidth,
    _ref$gutterOffset = _ref.gutterOffset,
    gutterOffset = _ref$gutterOffset === void 0 ? 0 : _ref$gutterOffset;
  if (widthType === 'pixel' && width) {
    return width;
  }
  switch (layout) {
    case 'wide':
      return calcLegacyWideWidth(containerWidth, origWidth, contentWidth);
    case 'full-width':
      // legacy and new experience have different definitions of full-width,
      // since it's for new experience, we convert to the new definition
      return (0, _calcMediaSingleMaxWidth.calcMediaSingleMaxWidth)(containerWidth);
    default:
      if (width) {
        return Math.ceil(((contentWidth || containerWidth) + gutterOffset) * (width / 100) - gutterOffset);
      }
  }

  // Handle the case of not resized node with wrapped layout
  // It's possible that the node is first inserted with align layout (e.g. jira)
  // in which the legacy image would render the width as min(origWidth, halfContentWidth).
  // However, new experience won't be able to distinguish the two. Thus, we render halfContentWidth
  // to make sure confluence legacy node is renderered correctly
  if (_constants.wrappedLayouts.includes(layout)) {
    return Math.ceil((contentWidth || containerWidth) / 2);
  }

  // set initial width for not resized legacy image
  return (0, _getMediaSingleInitialWidth.getMediaSingleInitialWidth)(origWidth,
  // in case containerWidth is 0, we fallback to undefined to use akEditorDefaultLayoutWidth
  contentWidth || containerWidth || undefined);
};

/**
 * Calculate pixel width for legacy media single
 * @param contentWidth editor content width
 * @param containerWidth editor container width
 */
var calcLegacyWideWidth = function calcLegacyWideWidth(containerWidth, origWidth, contentWidth) {
  if (contentWidth) {
    var wideWidth = Math.ceil(contentWidth * _editorSharedStyles.breakoutWideScaleRatio);
    return wideWidth > containerWidth ? contentWidth : wideWidth;
  }
  return origWidth;
};
var calcParentPadding = function calcParentPadding(view, resolvedPos) {
  // since table has constant padding, use hardcoded constant instead of query the dom
  var tablePadding = 8;
  var _view$state$schema$no = view.state.schema.nodes,
    tableCell = _view$state$schema$no.tableCell,
    tableHeader = _view$state$schema$no.tableHeader;
  return [tableCell, tableHeader].includes(resolvedPos.parent.type) ? tablePadding * 2 : 0;
};

/**
 * Get parent width for a nested media single node for new experience
 * We don't check for mediaSingle selection in this function.
 * @param view Editor view
 * @param pos node position
 * @param forInsertion for insertion
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var getMaxWidthForNestedNodeNext = exports.getMaxWidthForNestedNodeNext = function getMaxWidthForNestedNodeNext(view, pos, forInsertion) {
  if (typeof pos !== 'number') {
    return null;
  }
  var $pos = view.state.doc.resolve(pos);
  if ($pos && $pos.parent.type.name !== 'doc') {
    return forInsertion ? getParentWidthForNestedMediaSingleNodeForInsertion($pos, view) : getParentWidthForNestedMediaSingleNode($pos, view);
  }
  return null;
};

/**
 * Get parent content width for nested media single node.
 * @param resolvedPos resolved Position of the node
 * @param view editor view
 * @returns parent content width for nested node
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var getParentWidthForNestedMediaSingleNode = exports.getParentWidthForNestedMediaSingleNode = function getParentWidthForNestedMediaSingleNode(resolvedPos, view) {
  var domNode = view.nodeDOM(resolvedPos.pos);
  if (resolvedPos.nodeAfter && _floatingLayouts.floatingLayouts.includes(resolvedPos.nodeAfter.attrs.layout) && domNode && domNode.parentElement) {
    var parentPadding = calcParentPadding(view, resolvedPos);
    return domNode.parentElement.offsetWidth - parentPadding;
  }
  if (domNode instanceof HTMLElement) {
    return domNode.offsetWidth;
  }
  return null;
};

/**
 * Get parent width for nested media single nodes
 * @param resolvedPos resolved Position of the node
 * @param view editor view
 * @returns parent width used for media single initial width on insertion
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var getParentWidthForNestedMediaSingleNodeForInsertion = exports.getParentWidthForNestedMediaSingleNodeForInsertion = function getParentWidthForNestedMediaSingleNodeForInsertion(resolvedPos, view) {
  var parentPos = resolvedPos.before(resolvedPos.depth);
  var parentDomNode = view.nodeDOM(parentPos);
  var parentPadding = calcParentPadding(view, resolvedPos);
  if (parentDomNode instanceof HTMLElement) {
    return parentDomNode.offsetWidth - parentPadding;
  }
  return null;
};
// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export

// eslint-disable-next-line @atlaskit/editor/no-re-export