"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 tiptap_exports = {};
__export(tiptap_exports, {
  indent: () => indent
});
module.exports = __toCommonJS(tiptap_exports);
var import_v1 = require("./../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_plugin_commons = require("./../plugin-commons/index.js");
const INDENT_SIZE = 40;
const indent = (0, import_plugin_commons.createRicosFunctionalExtension)({
  type: "extension",
  groups: ["shortcuts-enabled"],
  name: "indent",
  reconfigure(config, _extensions, _, settings) {
    const types = [import_v1.Node_Type.PARAGRAPH, import_v1.Node_Type.HEADING, import_v1.Node_Type.BLOCKQUOTE];
    const minLevel = 0;
    const maxLevel = 4;
    const { isTextSelection, AllSelection } = settings;
    return {
      ...config,
      addGlobalAttributes() {
        return [
          {
            types,
            attributes: {
              indentation: {
                renderHTML: (attributes) => {
                  if (attributes.indentation === 0 || Number.isNaN(parseInt(attributes.indentation, 10))) {
                    return {};
                  }
                  return {
                    style: `margin-inline-start: ${attributes.indentation * INDENT_SIZE}px`
                  };
                }
              }
            }
          }
        ];
      },
      addCommands() {
        const setNodeIndentMarkup = (tr, pos, delta) => {
          const node = tr?.doc?.nodeAt(pos);
          if (node) {
            const nextLevel = (node.attrs.indentation || 0) + delta;
            const indent2 = nextLevel < minLevel ? minLevel : nextLevel > maxLevel ? maxLevel : nextLevel;
            if (indent2 !== node.attrs.indentation) {
              const { indentation: _2, ...currentAttrs } = node.attrs;
              const nodeAttrs = indent2 > minLevel ? { ...currentAttrs, indentation: indent2 } : currentAttrs;
              return tr.setNodeMarkup(pos, node.type, nodeAttrs, node.marks);
            }
          }
          return tr;
        };
        const updateIndentationLevel = (tr, delta) => {
          const { doc, selection } = tr;
          if (doc && selection && (isTextSelection(selection) || selection instanceof AllSelection)) {
            const { from, to } = selection;
            doc.nodesBetween(from, to, (node, pos) => {
              if (types.includes(node.type.name)) {
                setNodeIndentMarkup(tr, pos, delta);
                return false;
              }
              return true;
            });
          }
          return tr;
        };
        return {
          applyIndentation: (direction) => ({ tr, dispatch }) => {
            updateIndentationLevel(tr, direction);
            if (tr.docChanged) {
              dispatch?.(tr);
              return true;
            }
            return false;
          },
          indent: ({ useTabSymbolInParagraph = false } = {}) => ({ state, commands }) => {
            if ((0, import_plugin_commons.isInsideList)(state)) {
              return commands.indentList();
            }
            if (useTabSymbolInParagraph) {
              return false;
            }
            return commands.applyIndentation(1);
          },
          outdent: () => ({ state, commands }) => (0, import_plugin_commons.isInsideList)(state) ? commands.outdentList() : commands.applyIndentation(-1)
        };
      }
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      addKeyboardShortcuts() {
        return {
          Tab: () => this.editor.commands.indent({ useTabSymbolInParagraph: true }),
          "Shift-Tab": () => {
            this.editor.commands.outdent();
            return true;
          }
        };
      }
    };
  }
});
//# sourceMappingURL=tiptap.js.map
