"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.layoutToWidth = exports.getTableWidthWithNumberColumn = exports.getTableContainerWidth = exports.getParentNodeWidth = void 0;
var _utils = require("@atlaskit/editor-prosemirror/utils");
var _editorSharedStyles = require("@atlaskit/editor-shared-styles");
var _constants = require("@atlaskit/theme/constants");
var _extension = require("../styles/shared/extension");
var _layout = require("../styles/shared/layout");
var _breakout = require("../utils/breakout");
// eslint-disable-next-line @atlaskit/design-system/no-deprecated-imports

var layoutToWidth = exports.layoutToWidth = {
  default: _editorSharedStyles.akEditorDefaultLayoutWidth,
  wide: _editorSharedStyles.akEditorWideLayoutWidth,
  'full-width': _editorSharedStyles.akEditorFullWidthLayoutWidth
};

/**
 * Calculates width of parent node of a nested node (inside layouts, extension)
 * If current node selection is not nested will return undefined
 */
var getParentNodeWidth = exports.getParentNodeWidth = function getParentNodeWidth(pos, state, containerWidth, isFullWidthModeEnabled) {
  if (!pos) {
    return;
  }
  var node = getNestedParentNode(pos, state);
  if (!node) {
    return;
  }
  var layout = node.attrs.layout || 'default';
  var schema = state.schema;
  var breakoutMark = schema.marks.breakout && schema.marks.breakout.isInSet(node.marks);
  if (breakoutMark && breakoutMark.attrs.mode) {
    layout = breakoutMark.attrs.mode;
  }
  var parentWidth = calcBreakoutNodeWidth(layout, containerWidth, isFullWidthModeEnabled);

  // TODO: ED-15663
  // Please, do not copy or use this kind of code below
  // @ts-ignore
  var contextPanelPluginKey = {
    key: 'contextPanelPluginKey$',
    getState: function getState(state) {
      return state['contextPanelPluginKey$'];
    }
  };
  if (node.type === schema.nodes.layoutSection) {
    var _contextPanelPluginKe, _contextPanelPluginKe2;
    parentWidth += _editorSharedStyles.akLayoutGutterOffset * 2; // extra width that gets added to layout

    // Calculate width of parent layout column when
    // Parallel layout with viewport greater than 1024px
    // OR side panel of an extension is open and change the node width to smaller than containerWidth
    if (containerWidth.width > _editorSharedStyles.gridMediumMaxWidth || ((_contextPanelPluginKe = contextPanelPluginKey.getState(state)) === null || _contextPanelPluginKe === void 0 ? void 0 : _contextPanelPluginKe.contents.length) > 0 && ((_contextPanelPluginKe2 = contextPanelPluginKey.getState(state)) === null || _contextPanelPluginKe2 === void 0 ? void 0 : _contextPanelPluginKe2.contents[0]) !== undefined) {
      parentWidth -= (_layout.LAYOUT_SECTION_MARGIN + 2) * (node.childCount - 1); // margin between sections
      var $pos = state.doc.resolve(pos);
      var column = (0, _utils.findParentNodeOfTypeClosestToPos)($pos, [state.schema.nodes.layoutColumn]);
      if (column && column.node && !isNaN(column.node.attrs.width)) {
        // get exact width of parent layout column using node attrs
        parentWidth = Math.round(parentWidth * column.node.attrs.width * 0.01);
      }
    }
  }

  // account for the padding of the parent node
  switch (node.type) {
    case schema.nodes.layoutSection:
      parentWidth -= _layout.LAYOUT_COLUMN_PADDING * 2;
      break;
    case schema.nodes.bodiedExtension:
      parentWidth -= _extension.BODIED_EXT_PADDING * 2;
      break;
    case schema.nodes.extensionFrame:
      parentWidth -= _extension.BODIED_EXT_PADDING * 2;
      break;

    // TODO: Migrate away from gridSize
    // Recommendation: Replace gridSize with 8
    case schema.nodes.expand:
      // padding
      parentWidth -= (0, _constants.gridSize)() * 2;
      // gutter offset
      parentWidth += (0, _constants.gridSize)() * 1.5 * 2;
      // padding right
      parentWidth -= (0, _constants.gridSize)();
      // padding left
      parentWidth -= (0, _constants.gridSize)() * 4 - (0, _constants.gridSize)() / 2;
      break;
  }
  parentWidth -= 2; // border

  return parentWidth;
};
var getNestedParentNode = function getNestedParentNode(tablePos, state) {
  if (tablePos === undefined) {
    return null;
  }
  var $pos = state.doc.resolve(tablePos);
  var parent = (0, _utils.findParentNodeOfTypeClosestToPos)($pos, [state.schema.nodes.bodiedExtension, state.schema.nodes.extensionFrame, state.schema.nodes.layoutSection, state.schema.nodes.expand]);
  return parent ? parent.node : null;
};
var calcBreakoutNodeWidth = function calcBreakoutNodeWidth(layout, containerWidth, isFullWidthModeEnabled) {
  return isFullWidthModeEnabled ? Math.min(containerWidth.lineLength, _editorSharedStyles.akEditorFullWidthLayoutWidth) : (0, _breakout.absoluteBreakoutWidth)(layout, containerWidth.width);
};
var getTableContainerWidth = exports.getTableContainerWidth = function getTableContainerWidth(node) {
  if (node !== null && node !== void 0 && node.attrs.width) {
    return node.attrs.width;
  }
  return layoutToWidth[node === null || node === void 0 ? void 0 : node.attrs.layout] || _editorSharedStyles.akEditorDefaultLayoutWidth;
};
var getTableWidthWithNumberColumn = exports.getTableWidthWithNumberColumn = function getTableWidthWithNumberColumn(node, offset) {
  var isNumberColumnEnabled = node.attrs.isNumberColumnEnabled;
  if (isNumberColumnEnabled && offset > 0) {
    return getTableContainerWidth(node) - offset;
  }
  return getTableContainerWidth(node);
};