import { Decoration_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { mergeAttributes } from "./../../tiptap/index.js";
import { Plugin, PluginKey } from "./../../tiptap/pm/state.js";
import { createRicosMarkExtension } from "./../../plugin-commons/index.js";
import { LINK_PARSE_HTML_TAG } from "./../../tiptap-extensions/link/link-parse-html-tag.js";
const underline = createRicosMarkExtension({
  type: "mark",
  groups: [],
  name: Decoration_Type.UNDERLINE,
  reconfigure: (config, _extensions, _props, _settings, services) => {
    const autoUnderline = services.editorProps.generalLinkSettings?.autoUnderline ?? true;
    return {
      ...config,
      addOptions: () => ({
        HTMLAttributes: {},
        autoUnderline
      })
    };
  },
  createExtensionConfig() {
    return {
      name: this.name,
      addProseMirrorPlugins() {
        const plugins = [];
        if (this.options.autoUnderline) {
          plugins.push(
            new Plugin({
              key: new PluginKey("link-underline"),
              priority: 2,
              appendTransaction: (transactions, _oldState, newState) => {
                let from, to;
                transactions.forEach((t) => {
                  if (t.getMeta("autoLink")) {
                    const steps = t.steps;
                    const autoLinkStep = steps?.[steps.length - 1]?.toJSON() || {};
                    from = autoLinkStep.from;
                    to = autoLinkStep.to;
                    return;
                  }
                });
                if (!!from && !!to) {
                  return newState.tr.addMark(from, to, this.type.create()).removeStoredMark(this.type);
                }
              }
            })
          );
        }
        return plugins;
      },
      parseHTML() {
        return [
          {
            tag: "u"
          },
          {
            style: "text-decoration",
            consuming: false,
            getAttrs: (style) => style.includes("underline") ? {} : false
          },
          ...this.options.autoUnderline ? [{ tag: LINK_PARSE_HTML_TAG }] : []
        ];
      },
      renderHTML({ HTMLAttributes }) {
        return [
          "u",
          mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
          0
        ];
      },
      addCommands() {
        return {
          setUnderline: () => ({ commands }) => {
            return commands.setMark(this.name);
          },
          toggleUnderline: () => ({ commands }) => {
            return commands.toggleMark(this.name);
          },
          unsetUnderline: () => ({ commands }) => {
            return commands.unsetMark(this.name);
          }
        };
      },
      addKeyboardShortcuts() {
        return {
          "Mod-u": () => this.editor.commands.toggleUnderline()
        };
      }
    };
  }
});
export {
  underline
};
//# sourceMappingURL=extension.js.map
