"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 name2 in all)
    __defProp(target, name2, { get: all[name2], 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 extension_exports = {};
__export(extension_exports, {
  bold: () => bold
});
module.exports = __toCommonJS(extension_exports);
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_tiptap = require("./../../tiptap/index.js");
var import_core = require("./../../tiptap/core/index.js");
var import_plugin_commons = require("./../../plugin-commons/index.js");
var import_tiptap_extensions = require("./../../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 = import_v1.Decoration_Type.BOLD;
const bold = (0, import_plugin_commons.createRicosMarkExtension)({
  type: "mark",
  groups: [],
  name: import_v1.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 = (0, import_tiptap_extensions.getStylesDecorationBySelectedNode)(name, {
              state,
              styles: services.styles,
              pmConverters: services.converters
            });
            const hasBoldWeightInStyle = decoration && decoration.fontWeightValue === FONT_WEIGHT_BOLD;
            const hasBoldWeightInMark = (0, import_tiptap.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, ..._ } = (0, import_tiptap.mergeAttributes)(
          this.options.HTMLAttributes,
          HTMLAttributes
        );
        return ["strong", { style: `font-weight: ${fontWeightValue}` }, 0];
      },
      addKeyboardShortcuts() {
        return {
          "Mod-b": () => this.editor.commands.toggleBold()
        };
      },
      addInputRules() {
        return [
          (0, import_core.markInputRule)({
            find: starInputRegex,
            type: this.type
          }),
          (0, import_core.markInputRule)({
            find: underscoreInputRegex,
            type: this.type
          })
        ];
      },
      addPasteRules() {
        return [
          (0, import_core.markPasteRule)({
            find: starPasteRegex,
            type: this.type
          }),
          (0, import_core.markPasteRule)({
            find: underscorePasteRegex,
            type: this.type
          })
        ];
      }
    };
  }
});
//# sourceMappingURL=extension.js.map
