import classNames from "classnames";
import { generateId } from "./../../ricos-content/index.js";
import { Node_Type, TextStyle_TextAlignment } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { Plugin, PluginKey } from "./../../tiptap/pm/state.js";
import { Decoration, DecorationSet } from "./../../tiptap/pm/view.js";
import {
  createRicosNodeExtension,
  withBreakoutContainer
} from "./../../plugin-commons/index.js";
import { createSpanWrapperDecorationPlugin } from "../decoration-plugins/span-wrapper.js";
import styles from "../statics/styles-scss.js";
import { isInlineTextOnly } from "../transaction-predicates/is-inline-text-only.js";
const inputRegex = /^\s*>\s$/;
const alignmentToFlex = {
  [TextStyle_TextAlignment.LEFT]: "left",
  [TextStyle_TextAlignment.CENTER]: "center",
  [TextStyle_TextAlignment.RIGHT]: "right",
  [TextStyle_TextAlignment.JUSTIFY]: "flex-start"
};
const blockquote = createRicosNodeExtension({
  groups: ["shortcuts-enabled"],
  type: "html-node",
  name: Node_Type.BLOCKQUOTE,
  reconfigure: (config, _extensions, _props, settings) => ({
    ...config,
    addOptions() {
      return {
        HTMLAttributes: {
          class: styles.quote
        },
        ...settings
      };
    }
  }),
  createExtensionConfig({ mergeAttributes, textblockTypeInputRule }) {
    return {
      name: this.name,
      // ITS NOT BlockquoteNodeChildrenPattern !!!. ProseMirror and RicosDocument have different structure for blockquote.
      content: "inline*",
      group: "block smartBlockCellContent layoutCellContent collapsibleListItemTitleContent collapsibleListItemBodyContent tableCellContent",
      defining: true,
      addAttributes() {
        return {
          indentation: null,
          textStyle: null,
          paragraphId: null
        };
      },
      parseHTML() {
        return [{ tag: "blockquote" }];
      },
      renderHTML({ HTMLAttributes }) {
        const allAttributes = mergeAttributes(this.options.HTMLAttributes, HTMLAttributes);
        const textAlignment = allAttributes?.textStyle?.textAlignment;
        const attrs = {
          ...allAttributes,
          class: classNames(allAttributes.class)
        };
        const breakoutContainerAttr = {
          class: styles.quoteContainer,
          style: textAlignment === TextStyle_TextAlignment.AUTO || textAlignment === TextStyle_TextAlignment.UNRECOGNIZED ? void 0 : `justify-content: ${alignmentToFlex[textAlignment]};`
        };
        return withBreakoutContainer(["blockquote", attrs, 0], breakoutContainerAttr);
      },
      addCommands() {
        return {
          setBlockquote: () => ({ commands }) => {
            return commands.updateTextNode(this.name, { paragraphId: generateId() });
          },
          toggleBlockquote: () => ({ commands }) => {
            return commands.toggleTextNode(this.name, Node_Type.PARAGRAPH);
          },
          unsetBlockquote: () => ({ commands }) => {
            return commands.updateTextNode(Node_Type.PARAGRAPH, {});
          }
        };
      },
      addInputRules() {
        return [
          textblockTypeInputRule({
            find: inputRegex,
            type: this.type,
            getAttributes: ({ groups }) => groups
          })
        ];
      },
      addProseMirrorPlugins() {
        return [
          createSpanWrapperDecorationPlugin([Node_Type.BLOCKQUOTE]),
          createBlockquoteDirectionDecorationPlugin()
        ];
      }
    };
  }
});
const createBlockquoteDirectionDecorationPlugin = () => {
  const key = new PluginKey("blockquote-direction");
  return new Plugin({
    key,
    state: {
      init: (_config, state) => buildBlockquoteDirectionDecorations(state.doc),
      apply: (tr, prev) => {
        if (!tr.docChanged) {
          return prev;
        }
        if (isInlineTextOnly(tr)) {
          return prev.map(tr.mapping, tr.doc);
        }
        return buildBlockquoteDirectionDecorations(tr.doc);
      }
    },
    props: {
      decorations(state) {
        return key.getState(state);
      }
    }
  });
};
const buildBlockquoteDirectionDecorations = (doc) => {
  const decorations = [];
  doc.descendants((node, pos) => {
    if (node.type.name !== Node_Type.BLOCKQUOTE) {
      return;
    }
    decorations.push(Decoration.node(pos, pos + node.nodeSize, { dir: "auto" }));
    return false;
  });
  return DecorationSet.create(doc, decorations);
};
export {
  blockquote
};
//# sourceMappingURL=extension.js.map
