"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tiptap_foreground_exports = {};
__export(tiptap_foreground_exports, {
  tiptapColorForegroundExtension: () => tiptapColorForegroundExtension
});
module.exports = __toCommonJS(tiptap_foreground_exports);
var import_prosemirror_state = require("prosemirror-state");
var import_color_defaults_json = __toESM(require("./../ricos-schema/legacy/statics/color.defaults.json.js"));
var import_ricos_types = require("./../ricos-types/index.js");
var import_theme_color_token = require("./../_shared/theme-color-token.js");
var import_state = require("./../tiptap/pm/state.js");
var import_view = require("./../tiptap/pm/view.js");
var import_plugin_commons = require("./../plugin-commons/index.js");
var import_data_hooks = require("./data-hooks.js");
var import_ordered_set = require("./ordered-set.js");
var import_tiptap_theme_color_clipboard = require("./tiptap-theme-color-clipboard.js");
const COLOR_PREVIEW_PLUGIN_KEY = new import_state.PluginKey("colorPreview");
const tiptapColorForegroundExtension = (0, import_plugin_commons.createRicosMarkExtension)({
  type: "mark",
  name: import_ricos_types.Mark_Type.COLOR_FOREGROUND,
  groups: [],
  reconfigure: (config, _extensions, _props, settings) => ({
    ...config,
    addOptions() {
      return {
        HTMLAttributes: {},
        ...settings
      };
    }
  }),
  createExtensionConfig() {
    return {
      name: this.name,
      addAttributes() {
        return {
          color: { default: import_color_defaults_json.default.foreground }
        };
      },
      parseHTML() {
        return [
          {
            tag: `span[${import_data_hooks.FOREGROUND_COLOR_VALUE_ATTR}]`,
            getAttrs: (node) => {
              const color = node.getAttribute(import_data_hooks.FOREGROUND_COLOR_VALUE_ATTR);
              return color ? { color } : false;
            }
          },
          {
            style: "color",
            getAttrs: (color) => ({ color })
          }
        ];
      },
      renderHTML({ HTMLAttributes: { color } }) {
        const styleColor = this.options.styleSelectionPredicate?.(color) ? this.options.customStyleFn?.(import_ordered_set.OrderedSet.of(color)).color : color;
        const resolved = (0, import_theme_color_token.resolveDocumentColorToCss)(styleColor) ?? styleColor;
        return [
          "span",
          {
            "data-hook": import_data_hooks.FOREGROUND_COLOR,
            ...color ? { [import_data_hooks.FOREGROUND_COLOR_VALUE_ATTR]: color } : {},
            style: `color: ${resolved}; text-decoration: inherit;`
          }
        ];
      },
      addCommands() {
        return {
          previewColor: (color) => ({ tr, dispatch }) => {
            tr.setMeta(COLOR_PREVIEW_PLUGIN_KEY, color);
            dispatch?.(tr);
            return true;
          },
          setColor: (color) => ({ tr, dispatch, commands }) => {
            commands.setMark(this.name, { color });
            tr.setMeta(COLOR_PREVIEW_PLUGIN_KEY, null);
            dispatch?.(tr);
            return true;
          },
          unsetColor: () => ({ commands }) => commands.setMark(this.name, { color: null })
        };
      },
      addProseMirrorPlugins() {
        return [
          (0, import_tiptap_theme_color_clipboard.createThemeColorClipboardPlugin)(),
          new import_prosemirror_state.Plugin({
            key: COLOR_PREVIEW_PLUGIN_KEY,
            state: {
              init() {
                return { color: null };
              },
              apply(tr) {
                const newColor = tr.getMeta(COLOR_PREVIEW_PLUGIN_KEY);
                return { color: newColor };
              }
            },
            props: {
              decorations(state) {
                const { color } = COLOR_PREVIEW_PLUGIN_KEY.getState(state);
                const { from, to } = state.selection;
                if (!color || from === to) {
                  return null;
                }
                const resolved = (0, import_theme_color_token.resolveDocumentColorToCss)(color) ?? color;
                return import_view.DecorationSet.create(state.doc, [
                  import_view.Decoration.inline(from, to, {
                    style: `color: ${resolved}; text-decoration: inherit;`
                  })
                ]);
              }
            }
          })
        ];
      }
    };
  }
});
//# sourceMappingURL=tiptap-foreground.js.map
