'use strict';

const index = require('./index-58ea899e.js');
const internalIconManager = require('./internal-icon-manager-c989189a.js');
const globalSlimmerRegistry = require('./global-slimmer-registry-b3bce7e0.js');
const codeBlock = require('./code-block-f26ddfed.js');
const domNodeSubjectObserverFactory = require('./dom-node-subject-observer-factory-9110cb94.js');
const vegaEventId = require('./vega-event-id-d9994eb3.js');
const darkModeStyleController = require('./dark-mode-style-controller-134f945a.js');
const contentState = require('./content-state-530c366a.js');

const text = {
  icon: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Pro 6.3.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path fill="currentColor" d="M64 96v32c0 17.7-14.3 32-32 32s-32-14.3-32-32V80C0 53.5 21.5 32 48 32H224 400c26.5 0 48 21.5 48 48v48c0 17.7-14.3 32-32 32s-32-14.3-32-32V96H256l0 320h48c17.7 0 32 14.3 32 32s-14.3 32-32 32H144c-17.7 0-32-14.3-32-32s14.3-32 32-32h48l0-320H64z"/></svg>`,
};

var __decorate$2 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
class ToolbarButtonSlimmer extends globalSlimmerRegistry.VegaSlimmer {
  constructor({ icon, tooltip, autoFocusOnClick = true }) {
    super();
    this.renderBtn = ({ showArrowIcon, iconColor } = {}) => {
      var _a;
      return (index.h("vega-tooltip", { text: this.translationSlimmer.t(this.tooltip.text), placement: ['bottom', 'top'] },
        index.h("vega-rich-text-editor-toolbar-button", Object.assign({ showArrowIcon: showArrowIcon, icon: this.icon, iconColor: iconColor, ref: (ref) => {
            domNodeSubjectObserverFactory.DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, vegaEventId.VegaClick, () => {
              this.handleClick();
            });
          }, selected: this.isSelected(), disabled: this.isDisabled(), hidden: this.isHidden() }, this.preventLostFocus()), (_a = this.renderSlotContent) === null || _a === void 0 ? void 0 : _a.call(this))));
    };
    /**
     * Handle the click event on the toolbar button.
     * If there are no selected nodes, the focus will be set on the content and the click will be triggered after the selection change.
     */
    this.handleClick = () => {
      if (typeof this.onClick === 'function') {
        const selectedNodes = this.selectionController.getSelectedNodes();
        if (!selectedNodes.length && this.autoFocusOnClick) {
          void this.focusController.focusOnLastTextNode().then(() => {
            this.onClick();
          });
        }
        else {
          this.onClick();
        }
      }
    };
    this.icon = icon;
    this.tooltip = tooltip;
    this.autoFocusOnClick = autoFocusOnClick;
  }
  setIcon(icon) {
    this.icon = icon;
  }
  /**
   * Whether the button is hidden or not.
   */
  isHidden() {
    return false;
  }
  render() {
    return typeof this.renderBtnWrapper === 'function'
      ? this.renderBtnWrapper(this.renderBtn)
      : this.renderBtn();
  }
  /**
   * Whether the button is disabled or not.
   */
  isDisabled() {
    return this.disabled;
  }
  preventLostFocus() {
    return {
      onMouseDown: (e) => e.preventDefault(),
    };
  }
}
__decorate$2([
  globalSlimmerRegistry.MapToComponentField()
], ToolbarButtonSlimmer.prototype, "translationSlimmer", void 0);
__decorate$2([
  globalSlimmerRegistry.MapToComponentField()
], ToolbarButtonSlimmer.prototype, "selectionController", void 0);
__decorate$2([
  globalSlimmerRegistry.MapToComponentField()
], ToolbarButtonSlimmer.prototype, "focusController", void 0);
__decorate$2([
  globalSlimmerRegistry.MapToComponentField()
], ToolbarButtonSlimmer.prototype, "disabled", void 0);

var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/**
 * Abstract class for visual mode toolbar buttons, except for the source edit button.
 */
class VisualModeToolbarButtonSlimmer extends ToolbarButtonSlimmer {
  constructor(config) {
    super(config);
  }
  /**
   * Determines if the button should be hidden.
   * Hide the button when the editor is in source view mode, except for the source edit button.
   *
   * @returns {boolean} - True if the button is hidden, false otherwise.
   */
  isHidden() {
    return this.sourceView;
  }
  /**
   * Determines if the button should be disabled.
   * Determines whether should disable the button or not when the editor is not in source view mode, except for the source edit button.
   *
   * @returns {boolean} - True if the button is disabled, false otherwise.
   */
  isDisabled() {
    return super.isDisabled() && !this.sourceView;
  }
  /**
   * Check if the node support text annotation
   *
   * @param {Nullable< RTENode| RTEBlock>} node - The RTE node to check
   * @param {string} annotationActionName - The annotation name to check
   * @returns {boolean} - True if the node support text annotation, false otherwise
   */
  shouldDisabledToolbar(node, annotationActionName) {
    return node && !codeBlock.ActionHandleStrategyRegistry.canHandle(annotationActionName, node);
  }
}
__decorate$1([
  globalSlimmerRegistry.MapToComponentField()
], VisualModeToolbarButtonSlimmer.prototype, "sourceView", void 0);

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
class TextColorToolbarButtonSlimmer extends VisualModeToolbarButtonSlimmer {
  constructor() {
    super({
      icon: 'text',
      tooltip: { text: 'Text Color' },
    });
    this.popoverShow = false;
    darkModeStyleController.VegaDarkModeStyleController.registerCustomDarkModeColor(TextColorToolbarButtonSlimmer.formatDarkModeColor());
  }
  static formatDarkModeColor() {
    return codeBlock.RTE_TEXT_COLORS.reduce((prev, current) => {
      current.dark && (prev[`rich-text-editor-${current.key}`] = current.dark);
      return prev;
    }, {});
  }
  renderSlotContent() {
    return (index.h("div", { class: {
        'toolbar-btn-color-preview-box': true,
        'disabled': this.isDisabled(),
      }, style: {
        backgroundColor: this.getCurrentColor(),
      } }));
  }
  handlePopoverShowChange() {
    this.popoverShow = true;
  }
  handlePopoverHideChange() {
    this.popoverShow = false;
  }
  renderBtnWrapper(renderBtn) {
    return (index.h("vega-popover", { placement: "bottom", alignment: "center", ref: (ref) => {
        this.popoverRef = ref;
        domNodeSubjectObserverFactory.DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, vegaEventId.VegaPopoverShow, this.handlePopoverShowChange.bind(this));
        domNodeSubjectObserverFactory.DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, vegaEventId.VegaPopoverHide, this.handlePopoverHideChange.bind(this));
      } },
      index.h("div", { slot: "popover-content" }, renderBtn({
        showArrowIcon: false,
      })),
      index.h("div", Object.assign({ slot: "popover", style: { userSelect: 'none' } }, super.preventLostFocus()), this.renderPopoverContent())));
  }
  renderPopoverContent() {
    return (index.h("vega-color-picker", { isInline: true, colors: codeBlock.RTE_TEXT_COLORS.map((item) => item[!this.isInDarkMode ? 'light' : 'dark']), ref: (ref) => {
        domNodeSubjectObserverFactory.DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, vegaEventId.VegaChange, this.handleChange.bind(this));
      }, value: this.getCurrentColor() }));
  }
  async handleChange(e) {
    if (this.popoverShow) {
      if (!this.selectionController.getSelectedNodes().length) {
        await this.focusController.focusOnLastTextNode();
      }
      this.selectionController
        .getSelectedNodes()
        .filter((node) => node instanceof codeBlock.RTETextNode)
        .forEach((node) => node.apply(new codeBlock.TextColorAnnotationAction(this.matchColor(e.detail).key)));
    }
    void this.popoverRef.hide();
  }
  matchColor(value) {
    return codeBlock.RTE_TEXT_COLORS.find((item) => item[this.isInDarkMode ? 'dark' : 'light'] === value);
  }
  getCurrentColor() {
    let selectedItem = codeBlock.RTE_TEXT_COLORS[0];
    const selectedNodes = this.selectionController.getSelectedNodes();
    if (selectedNodes.length > 1) {
      if (this.isSelectedSameColor(selectedNodes)) {
        selectedItem = this.getTextColor(selectedNodes[0]);
      }
      else {
        selectedItem = null;
      }
    }
    if (selectedNodes.length === 1) {
      selectedItem = this.getTextColor(selectedNodes[0]);
    }
    return selectedItem ? selectedItem[!this.isInDarkMode ? 'light' : 'dark'] : null;
  }
  isSelected() {
    return false;
  }
  isSelectedSameColor(selectedNodes) {
    return selectedNodes.every((node) => this.getTextColor(node).key === this.getTextColor(selectedNodes[0]).key);
  }
  getTextColor(selectedNodes) {
    var _a;
    return (((_a = selectedNodes.getAnnotationByType('TEXT_COLOR')) === null || _a === void 0 ? void 0 : _a.textColor) ||
      codeBlock.RTE_TEXT_COLORS[0]);
  }
  isDisabled() {
    const selectedNodes = this.selectionController.getSelectedNodes();
    return (super.isDisabled() ||
      selectedNodes.some((node) => {
        var _a;
        return ((_a = node.getAnnotationByType(codeBlock.NodeAnnotationTypeEnum.CODE)) === null || _a === void 0 ? void 0 : _a.code) ||
          node.getAnnotationByType(codeBlock.NodeAnnotationTypeEnum.LINK) ||
          this.shouldDisabledToolbar(node, codeBlock.TextColorAnnotationAction.name);
      }));
  }
}
(() => {
  internalIconManager.VegaInternalIconManager.register({
    text,
  });
})();
__decorate([
  globalSlimmerRegistry.MapToComponentField()
], TextColorToolbarButtonSlimmer.prototype, "host", void 0);
__decorate([
  globalSlimmerRegistry.MapToComponentField()
], TextColorToolbarButtonSlimmer.prototype, "isInDarkMode", void 0);
__decorate([
  globalSlimmerRegistry.MapToComponentField()
], TextColorToolbarButtonSlimmer.prototype, "selectionController", void 0);
__decorate([
  globalSlimmerRegistry.MapToComponentField()
], TextColorToolbarButtonSlimmer.prototype, "focusController", void 0);

/**
 * Action to add an image annotation to the editor
 */
class ImageAnnotationAction extends codeBlock.AnnotationAction {
  constructor(size, alt) {
    super();
    this.isFlushable = true;
    this.size = size;
    this.alt = alt;
  }
  /**
   * Converts the action to an annotation
   *
   * @returns {ImageAnnotation} The annotation
   */
  toAnnotation() {
    return new contentState.ImageAnnotation(this.size, this.alt);
  }
}

exports.ImageAnnotationAction = ImageAnnotationAction;
exports.TextColorToolbarButtonSlimmer = TextColorToolbarButtonSlimmer;
exports.ToolbarButtonSlimmer = ToolbarButtonSlimmer;
exports.VisualModeToolbarButtonSlimmer = VisualModeToolbarButtonSlimmer;
