import { Node_Type } from "./../../ricos-schema/next-schema/index.js";
import { PmNodeType } from "./../../_shared/pm-node-types.js";
import { findParentNodeClosestToPos } from "./../../tiptap/core/index.js";
import { NodeSelection } from "./../../tiptap/pm/state.js";
import { findNodeById } from "./../../tiptap-editor/helpers/index.js";
import { createRicosNodeExtension } from "./../../tiptap-extensions/index.js";
import { MAX_ITEMS } from "../consts.js";
import { SmartBlockCell } from "./smart-block-cell.js";
const SMART_BLOCK_CELL_SUPPORTED_QUICK_ACTIONS = [
  Node_Type.BLOCKQUOTE,
  Node_Type.CODE_BLOCK,
  Node_Type.HEADING,
  Node_Type.ORDERED_LIST,
  Node_Type.BULLETED_LIST,
  Node_Type.CHECKBOX_LIST,
  Node_Type.PARAGRAPH,
  Node_Type.APP_EMBED,
  Node_Type.AUDIO,
  Node_Type.BUTTON,
  Node_Type.COLLAPSIBLE_LIST,
  Node_Type.DIVIDER,
  Node_Type.EMBED,
  Node_Type.FILE,
  Node_Type.GALLERY,
  Node_Type.GIF,
  Node_Type.HTML,
  Node_Type.IMAGE,
  Node_Type.LINK_PREVIEW,
  Node_Type.POLL,
  Node_Type.TOC,
  Node_Type.VIDEO
];
const smartBlockCellExtension = createRicosNodeExtension({
  name: PmNodeType.SMART_BLOCK_CELL,
  groups: ["custom-styles", "node-container"],
  type: "react-node",
  Component: SmartBlockCell,
  reconfigure: (config, _extensions, _props, settings, services) => {
    return {
      ...config,
      draggable: !services.context.isMobile,
      addOptions: () => {
        return {
          services,
          ...settings
        };
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      isolating: true,
      group: "block",
      selectable: true,
      draggable: true,
      content: `(PARAGRAPH|smartBlockCellContent)*`,
      supportedQuickActions: SMART_BLOCK_CELL_SUPPORTED_QUICK_ACTIONS,
      parseHTML() {
        return [
          {
            tag: "div[data-smart-block-cell]",
            getAttrs: (el) => {
              const shapeRaw = el.getAttribute("data-shape");
              let shape = null;
              try {
                if (shapeRaw) {
                  shape = JSON.parse(shapeRaw);
                }
              } catch {
              }
              return {
                type: el.getAttribute("data-type") || null,
                label: el.getAttribute("data-label") || null,
                accentColor: el.getAttribute("data-accent-color") || null,
                backgroundColor: el.getAttribute("data-background-color") || null,
                borderColor: el.getAttribute("data-border-color") || null,
                borderWidth: el.getAttribute("data-border-width") ? Number(el.getAttribute("data-border-width")) : null,
                borderRadius: el.getAttribute("data-border-radius") ? Number(el.getAttribute("data-border-radius")) : null,
                shape,
                shapeColor: el.getAttribute("data-shape-color") || null
              };
            }
          }
        ];
      },
      renderHTML({ node }) {
        return [
          "div",
          {
            "data-smart-block-cell": node.attrs.id ?? true,
            "data-type": node.attrs.type ?? null,
            "data-label": node.attrs.label ?? null,
            "data-accent-color": node.attrs.accentColor ?? null,
            "data-background-color": node.attrs.backgroundColor ?? null,
            "data-border-color": node.attrs.borderColor ?? null,
            "data-border-width": node.attrs.borderWidth ?? null,
            "data-border-radius": node.attrs.borderRadius ?? null,
            "data-shape": node.attrs.shape ? JSON.stringify(node.attrs.shape) : null,
            "data-shape-color": node.attrs.shapeColor ?? null
          },
          0
        ];
      },
      addAttributes: () => ({
        type: null,
        label: null,
        borderColor: null,
        borderWidth: null,
        borderRadius: null,
        accentColor: null,
        backgroundColor: null,
        shape: null,
        shapeColor: null
      }),
      addCommands() {
        return {
          selectSmartBlockCell: (nodeId) => ({ tr, chain }) => {
            const nodeWithPos = findNodeById(tr, nodeId)[0];
            if (!nodeWithPos) {
              return false;
            }
            return chain().setNodeSelection(nodeWithPos.pos).focus(void 0, { scrollIntoView: false }).run();
          },
          moveSmartBlockCell: (cellId, insertBeforeCellId, targetSmartBlockId) => ({ tr, dispatch }) => {
            const sourceNodeWithPos = findNodeById(tr, cellId)[0];
            if (!sourceNodeWithPos) {
              return false;
            }
            const { pos: sourcePos, node: sourceNode } = sourceNodeWithPos;
            const sourceParentSmartBlock = findParentNodeClosestToPos(
              tr.doc.resolve(sourcePos),
              (node) => node.type?.name === PmNodeType.SMART_BLOCK
            );
            let targetSmartBlockNodeWithPos = sourceParentSmartBlock;
            let targetPos;
            if (insertBeforeCellId) {
              const targetNodeWithPos = findNodeById(tr, insertBeforeCellId)[0];
              if (!targetNodeWithPos) {
                return false;
              }
              targetSmartBlockNodeWithPos = findParentNodeClosestToPos(
                tr.doc.resolve(targetNodeWithPos.pos),
                (node) => node.type?.name === PmNodeType.SMART_BLOCK
              );
              targetPos = targetNodeWithPos.pos;
            } else {
              if (targetSmartBlockId) {
                targetSmartBlockNodeWithPos = findNodeById(tr, targetSmartBlockId)[0];
              }
              if (!targetSmartBlockNodeWithPos) {
                return false;
              }
              targetPos = targetSmartBlockNodeWithPos.pos + targetSmartBlockNodeWithPos.node.nodeSize - 1;
            }
            if (targetSmartBlockNodeWithPos && sourceParentSmartBlock && targetSmartBlockNodeWithPos.node.attrs.id !== sourceParentSmartBlock.node.attrs.id && targetSmartBlockNodeWithPos.node.childCount >= MAX_ITEMS) {
              return false;
            }
            if (targetPos === sourcePos || targetPos === sourcePos + sourceNode.nodeSize) {
              return false;
            }
            tr.delete(sourcePos, sourcePos + sourceNode.nodeSize);
            const mappedTargetPos = tr.mapping.map(targetPos);
            tr.insert(mappedTargetPos, sourceNode);
            tr.setSelection(NodeSelection.create(tr.doc, mappedTargetPos));
            if (dispatch) {
              dispatch(tr);
            }
            return true;
          }
        };
      }
    };
  }
});
export {
  smartBlockCellExtension
};
//# sourceMappingURL=smart-block-cell-extension.js.map
