import { getNodeInSelection } from "./../_shared/resolvers/get-node-in-selection.js";
import { isAlwaysVisible } from "./../_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: isAlwaysVisible,
      selectedNode: 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
  };
}, {});
export {
  RicosPluginToolbarButton
};
//# sourceMappingURL=plugin-toolbar-button.js.map
