"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var shared_exports = {};
__export(shared_exports, {
  DEFAULT_BACKDROP_VERTICAL_SPACING: () => DEFAULT_BACKDROP_VERTICAL_SPACING,
  DEFAULT_BANNER_VERTICAL_SPACING: () => DEFAULT_BANNER_VERTICAL_SPACING,
  balanceLayoutCells: () => balanceLayoutCells,
  convertPresetToLayoutData: () => convertPresetToLayoutData,
  createBlankLayoutData: () => createBlankLayoutData,
  createEmptyCell: () => createEmptyCell,
  createQuickActionBannerLayout: () => createQuickActionBannerLayout,
  findLayoutOfCell: () => findLayoutOfCell,
  findParentCardId: () => findParentCardId,
  getCardEndPos: () => getCardEndPos,
  getCardStartPos: () => getCardStartPos,
  getImageUrl: () => getImageUrl,
  getParentNodeId: () => getParentNodeId,
  getToolbarReferenceElement: () => getToolbarReferenceElement,
  toolbarsToAddIn: () => toolbarsToAddIn,
  tryParseJson: () => tryParseJson
});
module.exports = __toCommonJS(shared_exports);
var import_ricos_content = require("./../../ricos-content/index.js");
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_tiptap = require("./../../tiptap/index.js");
var import_image_utils = require("./../../wix-rich-content-common/lib/image-utils.js");
var import_editor_common = require("./../../editor-common/index.js");
var import_helpers = require("./../../tiptap-editor/helpers/index.js");
var import_shared_with_viewer = require("./shared-with-viewer.js");
const createEmptyCell = (colSpan) => ({
  type: import_ricos_content.TIPTAP_LAYOUT_CELL_TYPE,
  attrs: {
    id: (0, import_ricos_content.generateId)(),
    colSpan
  },
  content: [
    {
      type: import_v1.Node_Type.PARAGRAPH,
      content: []
    }
  ]
});
const createBlankLayoutData = (colCount) => ({
  ...import_shared_with_viewer.LAYOUT_DEFAULTS,
  id: (0, import_ricos_content.generateId)(),
  containerData: void 0,
  backgroundColor: void 0,
  backdropColor: void 0,
  backgroundImage: import_shared_with_viewer.LAYOUT_DEFAULTS.backgroundImage ?? void 0,
  borderColor: import_shared_with_viewer.LAYOUT_DEFAULTS.borderColor ?? void 0,
  backdropImage: import_shared_with_viewer.LAYOUT_DEFAULTS.backdropImage ?? void 0,
  banner: import_shared_with_viewer.LAYOUT_DEFAULTS.banner ?? void 0,
  cellPadding: [...import_shared_with_viewer.LAYOUT_DEFAULTS.cellPadding],
  content: Array.from({ length: colCount }, () => createEmptyCell(import_shared_with_viewer.MAX_COLUMNS / colCount))
});
const convertPresetToLayoutData = (presetContent, converters) => {
  const pmContent = converters.toPm(presetContent).content ?? [];
  const layoutNode = pmContent[0];
  if (!layoutNode) {
    return null;
  }
  const { attrs, content } = layoutNode;
  return { ...attrs, content };
};
const balanceLayoutCells = (tr, node, pos) => {
  const colSpan = import_shared_with_viewer.MAX_COLUMNS / Math.min(node?.childCount ?? import_shared_with_viewer.MAX_CELLS, import_shared_with_viewer.MAX_CELLS);
  node?.forEach((_child, childPos) => {
    tr.setNodeAttribute(pos + childPos + 1, "colSpan", colSpan);
  });
};
const findLayoutOfCell = (node, cellId) => {
  let maybeLayoutNode = null;
  node.descendants((child, pos) => {
    if (child.type.name === import_ricos_content.TIPTAP_LAYOUT_TYPE) {
      for (let i = 0; i < child.childCount; i++) {
        if (child.child(i).attrs.id === cellId) {
          maybeLayoutNode = { node: child, pos };
          break;
        }
      }
    }
  });
  return maybeLayoutNode;
};
const getParentNodeId = (tr, nodeId) => {
  const nodeWithPos = (0, import_helpers.findNodeById)(tr, nodeId)[0];
  const $pos = tr.doc.resolve(nodeWithPos.pos);
  return $pos.parent.attrs.id;
};
const getToolbarReferenceElement = (tiptapEditor) => {
  const $from = tiptapEditor.state.selection.$from;
  const closest = (0, import_tiptap.findParentNodeClosestToPos)($from, (node) => node.type.name === import_ricos_content.TIPTAP_LAYOUT_TYPE);
  const startNodePos = closest ? tiptapEditor.$pos(closest.pos) : $from;
  const nodeElement = tiptapEditor.view.nodeDOM(startNodePos.pos);
  const handleElement = nodeElement?.querySelector?.("[data-layout-handle]");
  if (nodeElement) {
    return {
      getBoundingClientRect: () => {
        const wrapperRect = nodeElement.getBoundingClientRect();
        const handleElementRect = handleElement?.getBoundingClientRect();
        return new DOMRect(
          wrapperRect.x,
          wrapperRect.y,
          wrapperRect.width,
          wrapperRect.height + (handleElementRect?.height ?? 0) + 4
        );
      },
      contextElement: nodeElement
    };
  }
  return tiptapEditor.view.nodeDOM(startNodePos.pos - 1);
};
const toolbarsToAddIn = [import_editor_common.TOOLBARS.FOOTER, import_editor_common.TOOLBARS.SIDE, import_editor_common.TOOLBARS.MOBILE];
const tryParseJson = (value) => {
  if (!value) {
    return null;
  }
  try {
    return JSON.parse(value);
  } catch {
    return null;
  }
};
const DEFAULT_BACKDROP_VERTICAL_SPACING = 26;
const DEFAULT_BANNER_VERTICAL_SPACING = 50;
const createQuickActionBannerLayout = (position = "TOP") => {
  const headingId = (0, import_ricos_content.generateId)();
  const { content: _content, ...attrs } = createBlankLayoutData(1);
  return {
    headingId,
    layoutNode: {
      type: import_ricos_content.TIPTAP_LAYOUT_TYPE,
      attrs: {
        ...attrs,
        backdropColor: void 0,
        banner: { origin: "LAYOUT", position },
        backdropPaddingTop: DEFAULT_BANNER_VERTICAL_SPACING,
        backdropPaddingBottom: DEFAULT_BANNER_VERTICAL_SPACING
      },
      content: [
        {
          type: import_ricos_content.TIPTAP_LAYOUT_CELL_TYPE,
          attrs: {
            id: (0, import_ricos_content.generateId)(),
            colSpan: import_shared_with_viewer.MAX_COLUMNS
          },
          content: [
            {
              type: import_v1.Node_Type.HEADING,
              attrs: {
                id: headingId,
                indentation: null,
                level: 1,
                textStyle: {
                  textAlignment: "CENTER"
                },
                style: null
              },
              content: []
            }
          ]
        }
      ]
    }
  };
};
const getImageUrl = (media) => (0, import_image_utils.getImageSrc)({ file_name: media.src?.id }, void 0, {
  requiredQuality: 90,
  requiredWidth: Math.min(media.width ?? 4e3, 4e3),
  requiredHeight: Math.min(media.height ?? 4e3, 4e3),
  encAutoImageUrls: true
});
const findParentCardId = (doc, pos) => {
  const $pos = doc.resolve(pos);
  const parentCard = (0, import_tiptap.findParentNodeClosestToPos)($pos, (node) => node.type.name === import_ricos_content.TIPTAP_CARD_TYPE);
  return parentCard?.node.attrs.id;
};
const getCardStartPos = (tr, cardId) => {
  const card = (0, import_helpers.findNodeById)(tr, cardId)[0];
  return card ? card.pos + 1 : 0;
};
const getCardEndPos = (tr, cardId) => {
  const card = (0, import_helpers.findNodeById)(tr, cardId)[0];
  return card ? card.pos + card.node.nodeSize - 1 : tr.doc.content.size;
};
//# sourceMappingURL=shared.js.map
