import { Decoration_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { isMarkActive } from "./../../tiptap/index.js";
import { markInputRule, markPasteRule } from "./../../tiptap/core/index.js";
import { createRicosMarkExtension } from "./../../plugin-commons/index.js";
import { getStylesDecorationBySelectedNode } from "./../../tiptap-extensions/index.js";
const starInputRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))$/;
const starPasteRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))/g;
const underscoreInputRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))$/;
const underscorePasteRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))/g;
const name = Decoration_Type.ITALIC;
const italic = createRicosMarkExtension({
  type: "mark",
  groups: [],
  name,
  reconfigure(config, _extensions, _ricosProps, _settings, services) {
    return {
      ...config,
      addCommands() {
        return {
          setItalic: () => ({ commands }) => {
            return commands.setMark(this.name);
          },
          toggleItalic: () => ({ commands, state }) => {
            const decoration = getStylesDecorationBySelectedNode(name, {
              state,
              styles: services.styles,
              pmConverters: services.converters
            });
            const hasItalicInStyle = decoration && decoration.italicData === true;
            const hasItalicInMark = isMarkActive(state, this.name, { italicData: true });
            return commands.toggleMark(this.name, {
              italicData: !!(hasItalicInMark || !hasItalicInStyle)
            });
          },
          unsetItalic: () => ({ commands }) => {
            return commands.unsetMark(this.name);
          }
        };
      }
    };
  },
  createExtensionConfig() {
    return {
      name,
      addOptions() {
        return {
          HTMLAttributes: {}
        };
      },
      addAttributes() {
        return {
          italicData: {
            default: true
          }
        };
      },
      parseHTML() {
        return [
          {
            tag: "em"
          },
          {
            tag: "i",
            getAttrs: (node) => node.style.fontStyle !== "normal" && null
          },
          {
            style: "font-style=italic"
          }
        ];
      },
      renderHTML({ HTMLAttributes }) {
        const { italicData } = HTMLAttributes;
        return [
          "em",
          { style: `font-style: ${italicData ? "italic" : "normal"}` },
          0
        ];
      },
      addKeyboardShortcuts() {
        return {
          "Mod-i": () => this.editor.commands.toggleItalic()
        };
      },
      addInputRules() {
        return [
          markInputRule({
            find: starInputRegex,
            type: this.type
          }),
          markInputRule({
            find: underscoreInputRegex,
            type: this.type
          })
        ];
      },
      addPasteRules() {
        return [
          markPasteRule({
            find: starPasteRegex,
            type: this.type
          }),
          markPasteRule({
            find: underscorePasteRegex,
            type: this.type
          })
        ];
      }
    };
  }
});
export {
  italic
};
//# sourceMappingURL=extension.js.map
