"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 smart_block_cell_extension_exports = {};
__export(smart_block_cell_extension_exports, {
  smartBlockCellExtension: () => smartBlockCellExtension
});
module.exports = __toCommonJS(smart_block_cell_extension_exports);
var import_next_schema = require("./../../ricos-schema/next-schema/index.js");
var import_pm_node_types = require("./../../_shared/pm-node-types.js");
var import_core = require("./../../tiptap/core/index.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_helpers = require("./../../tiptap-editor/helpers/index.js");
var import_tiptap_extensions = require("./../../tiptap-extensions/index.js");
var import_consts = require("../consts.js");
var import_smart_block_cell = require("./smart-block-cell.js");
const SMART_BLOCK_CELL_SUPPORTED_QUICK_ACTIONS = [
  import_next_schema.Node_Type.BLOCKQUOTE,
  import_next_schema.Node_Type.CODE_BLOCK,
  import_next_schema.Node_Type.HEADING,
  import_next_schema.Node_Type.ORDERED_LIST,
  import_next_schema.Node_Type.BULLETED_LIST,
  import_next_schema.Node_Type.CHECKBOX_LIST,
  import_next_schema.Node_Type.PARAGRAPH,
  import_next_schema.Node_Type.APP_EMBED,
  import_next_schema.Node_Type.AUDIO,
  import_next_schema.Node_Type.BUTTON,
  import_next_schema.Node_Type.COLLAPSIBLE_LIST,
  import_next_schema.Node_Type.DIVIDER,
  import_next_schema.Node_Type.EMBED,
  import_next_schema.Node_Type.FILE,
  import_next_schema.Node_Type.GALLERY,
  import_next_schema.Node_Type.GIF,
  import_next_schema.Node_Type.HTML,
  import_next_schema.Node_Type.IMAGE,
  import_next_schema.Node_Type.LINK_PREVIEW,
  import_next_schema.Node_Type.POLL,
  import_next_schema.Node_Type.TOC,
  import_next_schema.Node_Type.VIDEO
];
const smartBlockCellExtension = (0, import_tiptap_extensions.createRicosNodeExtension)({
  name: import_pm_node_types.PmNodeType.SMART_BLOCK_CELL,
  groups: ["custom-styles", "node-container"],
  type: "react-node",
  Component: import_smart_block_cell.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 = (0, import_helpers.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 = (0, import_helpers.findNodeById)(tr, cellId)[0];
            if (!sourceNodeWithPos) {
              return false;
            }
            const { pos: sourcePos, node: sourceNode } = sourceNodeWithPos;
            const sourceParentSmartBlock = (0, import_core.findParentNodeClosestToPos)(
              tr.doc.resolve(sourcePos),
              (node) => node.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK
            );
            let targetSmartBlockNodeWithPos = sourceParentSmartBlock;
            let targetPos;
            if (insertBeforeCellId) {
              const targetNodeWithPos = (0, import_helpers.findNodeById)(tr, insertBeforeCellId)[0];
              if (!targetNodeWithPos) {
                return false;
              }
              targetSmartBlockNodeWithPos = (0, import_core.findParentNodeClosestToPos)(
                tr.doc.resolve(targetNodeWithPos.pos),
                (node) => node.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK
              );
              targetPos = targetNodeWithPos.pos;
            } else {
              if (targetSmartBlockId) {
                targetSmartBlockNodeWithPos = (0, import_helpers.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 >= import_consts.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(import_state.NodeSelection.create(tr.doc, mappedTargetPos));
            if (dispatch) {
              dispatch(tr);
            }
            return true;
          }
        };
      }
    };
  }
});
//# sourceMappingURL=smart-block-cell-extension.js.map
