"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 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var plugin_toolbar_button_exports = {};
__export(plugin_toolbar_button_exports, {
  RicosPluginToolbarButton: () => RicosPluginToolbarButton
});
module.exports = __toCommonJS(plugin_toolbar_button_exports);
var import_get_node_in_selection = require("./../_shared/resolvers/get-node-in-selection.js");
var import_is_always_visible = require("./../_shared/resolvers/is-always-visible.js");
class RicosPluginToolbarButton {
  constructor(button, services) {
    this.button = button;
    this.services = services;
  }
  static of(button, services) {
    return new RicosPluginToolbarButton(button, services);
  }
  getButton() {
    return { ...this.button };
  }
  equals(button) {
    return this.button.id === button.getButton().id;
  }
  toToolbarItemConfig() {
    const attributeResolvers = {
      visible: import_is_always_visible.isAlwaysVisible,
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      ...this.button.attributes
    };
    if (this.button.type === "separator") {
      return {
        id: this.button.id,
        type: "separator",
        presentation: {
          icon: this.button.icon
        },
        attributes: attributeResolvers,
        commands: {}
      };
    }
    if (this.button.type === "color") {
      const { command: command2 } = this.button;
      return {
        id: this.button.id,
        type: "color",
        presentation: {
          tooltip: resolveTooltip(this.button.tooltip, this.services.t),
          dataHook: this.button.dataHook
        },
        attributes: attributeResolvers,
        commands: {
          click: ({ publicCommands, tiptapEditor, attributes }) => (args) => {
            return command2({
              referenceElement: args?.referenceElement ?? args?.target,
              tiptapEditor,
              publicCommands,
              attributes,
              modals: this.services.modals,
              getComponentProps: void 0,
              ...args
            });
          }
        }
      };
    }
    const { id, type, tooltip, dataHook, command } = this.button;
    const publishPluginToolbarClick = (args, node) => {
      const { value } = args || {};
      node && //temporary fix for table! (table hasn't node as attrs)
      this.services.pluginsEvents.publishPluginToolbarClick({
        pluginId: node.type.name,
        nodeId: node.attrs.id,
        buttonName: this.button.id,
        value
      });
    };
    return {
      id,
      type,
      presentation: {
        tooltip: resolveTooltip(tooltip, this.services.t),
        label: this.button.label ? this.services.t(this.button.label) : void 0,
        dataHook,
        icon: "getIcon" in this.button ? this.button.getIcon?.({
          services: this.services,
          attributes: getResolvedAttributes(attributeResolvers, this.services)
        }) : this.button.icon
      },
      attributes: attributeResolvers,
      commands: {
        click: ({ publicCommands, tiptapEditor, attributes }) => (args) => {
          const node = attributes.selectedNode;
          const returnValue = command({
            referenceElement: args?.referenceElement ?? args?.target,
            tiptapEditor,
            publicCommands,
            attributes,
            modals: this.services.modals,
            getComponentProps: "getComponentProps" in this.button ? this.button.getComponentProps : void 0,
            ...args
          });
          publishPluginToolbarClick(args, node);
          return returnValue;
        }
      }
    };
  }
  getRenderer() {
    const { id, renderer } = this.button;
    return {
      [id]: renderer
    };
  }
}
const resolveTooltip = (tooltip, t) => typeof tooltip === "function" ? tooltip : t(tooltip || "");
const getResolvedAttributes = (attributeResolvers, services) => Object.keys(attributeResolvers).map((attributeName) => {
  return {
    attributeName,
    attribute: services.content.resolve(attributeResolvers[attributeName])
  };
}).reduce((acc, { attributeName, attribute }) => {
  return {
    ...acc,
    [attributeName]: attribute
  };
}, {});
//# sourceMappingURL=plugin-toolbar-button.js.map
