"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isVerticalPosition = exports.isRange = exports.isNumber = exports.getMediaSingleDimensions = exports.getGuidelineTypeFromKey = exports.getContainerWidthOrFullEditorWidth = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _editorSharedStyles = require("@atlaskit/editor-shared-styles");
var _mediaSingle = require("../media-single");
var isNumber = exports.isNumber = function isNumber(x) {
  return typeof x === 'number' && !isNaN(x) && isFinite(x);
};
var isRange = exports.isRange = function isRange(range) {
  return !!((0, _typeof2.default)(range) === 'object' && range && 'start' in range && 'end' in range);
};
var isVerticalPosition = exports.isVerticalPosition = function isVerticalPosition(pos) {
  return isNumber(pos.x);
};

/**
 * Returns the type of guideline based on a guideline key and a collection of guidelines
 */
var getGuidelineTypeFromKey = exports.getGuidelineTypeFromKey = function getGuidelineTypeFromKey(keys, guidelines) {
  if (guidelines.length === 0) {
    return 'none';
  }

  // Check for default guidelines on key
  if (keys.some(function (key) {
    return ['grid_', 'wide_', 'full_width'].some(function (defaultGuideline) {
      return key.indexOf(defaultGuideline) >= 0;
    });
  })) {
    return 'default';
  }

  // Check for temporary guidelines
  if (keys.some(function (key) {
    return ['media_'].some(function (temoporaryGuideline) {
      return key.indexOf(temoporaryGuideline) >= 0;
    });
  })) {
    return 'temporary';
  }

  // Check for relative guidelines
  if (keys.some(function (key) {
    return ['relative_'].some(function (relativeGuideline) {
      return key.indexOf(relativeGuideline) >= 0;
    });
  })) {
    return 'relative';
  }
  return 'none';
};

/**
 * Calculates container or full editor width taking in account editor full width layout
 * width and editor gutter padding.
 */
var getContainerWidthOrFullEditorWidth = exports.getContainerWidthOrFullEditorWidth = function getContainerWidthOrFullEditorWidth(containerWidth) {
  return Math.min(containerWidth - _editorSharedStyles.akEditorGutterPadding * 2, _editorSharedStyles.akEditorFullWidthLayoutWidth) / 2;
};

/**
 *
 * @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
  };
};