import { Node_Type } from "./../ricos-schema/legacy/wix/rich_content/v1/index.js";
import {
  createRicosFunctionalExtension,
  isInsideList
} from "./../plugin-commons/index.js";
const INDENT_SIZE = 40;
const indent = createRicosFunctionalExtension({
  type: "extension",
  groups: ["shortcuts-enabled"],
  name: "indent",
  reconfigure(config, _extensions, _, settings) {
    const types = [Node_Type.PARAGRAPH, Node_Type.HEADING, 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 (isInsideList(state)) {
              return commands.indentList();
            }
            if (useTabSymbolInParagraph) {
              return false;
            }
            return commands.applyIndentation(1);
          },
          outdent: () => ({ state, commands }) => 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;
          }
        };
      }
    };
  }
});
export {
  indent
};
//# sourceMappingURL=tiptap.js.map
