"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 _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _extension = require("../styles/shared/extension");
var _layout = require("../styles/shared/layout");
var _table = require("../styles/shared/table");
var _breakout = require("../utils/breakout");
var GRID_SIZE = 8;
var NESTED_DND_GUTTER_OFFSET = 8;
var NESTED_DND_MARGIN_OFFSET = 12;
var layoutToWidth = exports.layoutToWidth = {
  // eslint-disable-next-line @atlaskit/editor/no-re-export
  default: _editorSharedStyles.akEditorDefaultLayoutWidth,
  // eslint-disable-next-line @atlaskit/editor/no-re-export
  wide: _editorSharedStyles.akEditorWideLayoutWidth,
  // eslint-disable-next-line @atlaskit/editor/no-re-export
  '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) {
  var _contextPanelPluginKe, _contextPanelPluginKe2;
  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 breakoutWidth = breakoutMark ? breakoutMark.attrs.width : undefined;
  var parentWidth = calcBreakoutNodeWidth(layout, containerWidth, isFullWidthModeEnabled, breakoutWidth);

  // Please, do not copy or use this kind of code below
  // @ts-ignore
  var contextPanelPluginKey = {
    key: 'contextPanelPluginKey$',
    getState: function getState(state) {
      // Ignored via go/ees005
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      return state['contextPanelPluginKey$'];
    }
  };
  switch (node.type) {
    case schema.nodes.layoutSection:
      // the extra width of the layout does not add to the width of the area the table can be inside
      if (!(0, _expValEquals.expValEquals)('platform_editor_nested_table_refresh_width_fix', 'isEnabled', true)) {
        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) {
        // margin between sections
        parentWidth -= (0, _expValEquals.expValEquals)('platform_editor_nested_table_refresh_width_fix', 'isEnabled', true) && (0, _platformFeatureFlags.fg)('platform_editor_nested_dnd_styles_changes') ? (_layout.LAYOUT_SECTION_MARGIN + NESTED_DND_MARGIN_OFFSET + 2) * (node.childCount - 1) : (_layout.LAYOUT_SECTION_MARGIN + 2) * (node.childCount - 1);
        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
      parentWidth -= (0, _expValEquals.expValEquals)('platform_editor_nested_table_refresh_width_fix', 'isEnabled', true) && (0, _platformFeatureFlags.fg)('platform_editor_nested_dnd_styles_changes') ? (_layout.LAYOUT_COLUMN_PADDING + NESTED_DND_GUTTER_OFFSET) * 2 : _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;
    case schema.nodes.expand:
      // padding
      parentWidth -= GRID_SIZE * 2;
      // gutter offset
      parentWidth += GRID_SIZE * 1.5 * 2;
      // padding right
      parentWidth -= GRID_SIZE;
      // padding left
      parentWidth -= GRID_SIZE * 4 - GRID_SIZE / 2;
      break;
    case schema.nodes.tableCell:
    case schema.nodes.tableHeader:
      // Calculate the available column width
      if (Array.isArray(node.attrs.colwidth)) {
        parentWidth = node.attrs.colwidth.slice(0, node.attrs.colspan).reduce(function (sum, width) {
          return sum + width;
        }, 0);
      }
      // Compensate for padding
      parentWidth -= _table.tableCellPadding * 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, state.schema.nodes.tableCell, state.schema.nodes.tableHeader]);
  return parent ? parent.node : null;
};
var calcBreakoutNodeWidth = function calcBreakoutNodeWidth(layout, containerWidth, isFullWidthModeEnabled, breakoutWidth) {
  if (breakoutWidth && (0, _expValEquals.expValEquals)('platform_editor_nested_table_refresh_width_fix', 'isEnabled', true)) {
    return isFullWidthModeEnabled ? Math.min(containerWidth.lineLength, breakoutWidth) :
    // container width minus breakout padding
    // --ak-editor--breakout-full-page-guttering-padding = (--ak-editor--large-gutter-padding * 2) + --ak-editor--default-gutter-padding
    Math.min(containerWidth.width - ((0, _editorSharedStyles.akEditorGutterPaddingDynamic)() * 2 + _editorSharedStyles.akEditorGutterPadding), breakoutWidth);
  }
  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);
};