import { Decoration_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { isMarkActive, mergeAttributes } 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 FONT_WEIGHT_BOLD = 700;
const FONT_WEIGHT_NORMAL = 400;
const starInputRegex = /(?:^|\s)((?:\*\*)((?:[^*]+))(?:\*\*))$/;
const starPasteRegex = /(?:^|\s)((?:\*\*)((?:[^*]+))(?:\*\*))/g;
const underscoreInputRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))$/;
const underscorePasteRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))/g;
const name = Decoration_Type.BOLD;
const bold = createRicosMarkExtension({
  type: "mark",
  groups: [],
  name: Decoration_Type.BOLD,
  reconfigure(config, _extensions, _ricosProps, _settings, services) {
    return {
      ...config,
      addCommands() {
        return {
          setBold: () => ({ commands }) => {
            return commands.setMark(this.name);
          },
          toggleBold: () => ({ commands, state }) => {
            const decoration = getStylesDecorationBySelectedNode(name, {
              state,
              styles: services.styles,
              pmConverters: services.converters
            });
            const hasBoldWeightInStyle = decoration && decoration.fontWeightValue === FONT_WEIGHT_BOLD;
            const hasBoldWeightInMark = isMarkActive(state, this.name, {
              fontWeightValue: FONT_WEIGHT_BOLD
            });
            return commands.toggleMark(this.name, {
              fontWeightValue: hasBoldWeightInMark || !hasBoldWeightInStyle ? FONT_WEIGHT_BOLD : FONT_WEIGHT_NORMAL
            });
          },
          unsetBold: () => ({ commands }) => {
            return commands.unsetMark(this.name);
          }
        };
      }
    };
  },
  createExtensionConfig() {
    return {
      name,
      addOptions() {
        return {
          HTMLAttributes: {}
        };
      },
      addAttributes() {
        return {
          fontWeightValue: { default: FONT_WEIGHT_BOLD }
        };
      },
      parseHTML() {
        return [
          {
            tag: "strong"
          },
          {
            tag: "b",
            getAttrs: (node) => node.style.fontWeight !== "normal" && null
          },
          {
            style: "font-weight",
            getAttrs: (value) => /^(bold(er)?|[5-9]\d{2,})$/.test(value) && null
          }
        ];
      },
      renderHTML({ HTMLAttributes }) {
        const { fontWeightValue, ..._ } = mergeAttributes(
          this.options.HTMLAttributes,
          HTMLAttributes
        );
        return ["strong", { style: `font-weight: ${fontWeightValue}` }, 0];
      },
      addKeyboardShortcuts() {
        return {
          "Mod-b": () => this.editor.commands.toggleBold()
        };
      },
      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 {
  bold
};
//# sourceMappingURL=extension.js.map
