import {
  TIPTAP_CARD_TYPE,
  TIPTAP_LAYOUT_CELL_TYPE,
  TIPTAP_LAYOUT_TYPE,
  generateId
} from "./../../ricos-content/index.js";
import { Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { findParentNodeClosestToPos } from "./../../tiptap/index.js";
import { getImageSrc } from "./../../wix-rich-content-common/lib/image-utils.js";
import { TOOLBARS } from "./../../editor-common/index.js";
import { findNodeById } from "./../../tiptap-editor/helpers/index.js";
import { LAYOUT_DEFAULTS, MAX_CELLS, MAX_COLUMNS } from "./shared-with-viewer.js";
const createEmptyCell = (colSpan) => ({
  type: TIPTAP_LAYOUT_CELL_TYPE,
  attrs: {
    id: generateId(),
    colSpan
  },
  content: [
    {
      type: Node_Type.PARAGRAPH,
      content: []
    }
  ]
});
const createBlankLayoutData = (colCount) => ({
  ...LAYOUT_DEFAULTS,
  id: generateId(),
  containerData: void 0,
  backgroundColor: void 0,
  backdropColor: void 0,
  backgroundImage: LAYOUT_DEFAULTS.backgroundImage ?? void 0,
  borderColor: LAYOUT_DEFAULTS.borderColor ?? void 0,
  backdropImage: LAYOUT_DEFAULTS.backdropImage ?? void 0,
  banner: LAYOUT_DEFAULTS.banner ?? void 0,
  cellPadding: [...LAYOUT_DEFAULTS.cellPadding],
  content: Array.from({ length: colCount }, () => createEmptyCell(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 = MAX_COLUMNS / Math.min(node?.childCount ?? MAX_CELLS, 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 === 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 = 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 = findParentNodeClosestToPos($from, (node) => node.type.name === 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 = [TOOLBARS.FOOTER, TOOLBARS.SIDE, 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 = generateId();
  const { content: _content, ...attrs } = createBlankLayoutData(1);
  return {
    headingId,
    layoutNode: {
      type: TIPTAP_LAYOUT_TYPE,
      attrs: {
        ...attrs,
        backdropColor: void 0,
        banner: { origin: "LAYOUT", position },
        backdropPaddingTop: DEFAULT_BANNER_VERTICAL_SPACING,
        backdropPaddingBottom: DEFAULT_BANNER_VERTICAL_SPACING
      },
      content: [
        {
          type: TIPTAP_LAYOUT_CELL_TYPE,
          attrs: {
            id: generateId(),
            colSpan: MAX_COLUMNS
          },
          content: [
            {
              type: Node_Type.HEADING,
              attrs: {
                id: headingId,
                indentation: null,
                level: 1,
                textStyle: {
                  textAlignment: "CENTER"
                },
                style: null
              },
              content: []
            }
          ]
        }
      ]
    }
  };
};
const getImageUrl = (media) => 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 = findParentNodeClosestToPos($pos, (node) => node.type.name === TIPTAP_CARD_TYPE);
  return parentCard?.node.attrs.id;
};
const getCardStartPos = (tr, cardId) => {
  const card = findNodeById(tr, cardId)[0];
  return card ? card.pos + 1 : 0;
};
const getCardEndPos = (tr, cardId) => {
  const card = findNodeById(tr, cardId)[0];
  return card ? card.pos + card.node.nodeSize - 1 : tr.doc.content.size;
};
export {
  DEFAULT_BACKDROP_VERTICAL_SPACING,
  DEFAULT_BANNER_VERTICAL_SPACING,
  balanceLayoutCells,
  convertPresetToLayoutData,
  createBlankLayoutData,
  createEmptyCell,
  createQuickActionBannerLayout,
  findLayoutOfCell,
  findParentCardId,
  getCardEndPos,
  getCardStartPos,
  getImageUrl,
  getParentNodeId,
  getToolbarReferenceElement,
  toolbarsToAddIn,
  tryParseJson
};
//# sourceMappingURL=shared.js.map
