"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../dom.js");
const keyboardEvent = require("../../keyboardEvent.js");
const touch = require("../../touch.js");
const iconLabels = require("../iconLabel/iconLabels.js");
const color = require("../../../common/color.js");
const event = require("../../../common/event.js");
const lifecycle = require("../../../common/lifecycle.js");
const objects = require("../../../common/objects.js");
require("./button.css.js");
const defaultOptions = {
  buttonBackground: color.Color.fromHex("#0E639C"),
  buttonHoverBackground: color.Color.fromHex("#006BB3"),
  buttonForeground: color.Color.white
};
class Button extends lifecycle.Disposable {
  constructor(container, options) {
    super();
    this._onDidClick = this._register(new event.Emitter());
    this.options = options || /* @__PURE__ */ Object.create(null);
    objects.mixin(this.options, defaultOptions, false);
    this.buttonForeground = this.options.buttonForeground;
    this.buttonBackground = this.options.buttonBackground;
    this.buttonHoverBackground = this.options.buttonHoverBackground;
    this.buttonSecondaryForeground = this.options.buttonSecondaryForeground;
    this.buttonSecondaryBackground = this.options.buttonSecondaryBackground;
    this.buttonSecondaryHoverBackground = this.options.buttonSecondaryHoverBackground;
    this.buttonBorder = this.options.buttonBorder;
    this._element = document.createElement("a");
    this._element.classList.add("monaco-button");
    this._element.tabIndex = 0;
    this._element.setAttribute("role", "button");
    container.appendChild(this._element);
    this._register(touch.Gesture.addTarget(this._element));
    [dom.EventType.CLICK, touch.EventType.Tap].forEach((eventType) => {
      this._register(dom.addDisposableListener(this._element, eventType, (e) => {
        if (!this.enabled) {
          dom.EventHelper.stop(e);
          return;
        }
        this._onDidClick.fire(e);
      }));
    });
    this._register(dom.addDisposableListener(this._element, dom.EventType.KEY_DOWN, (e) => {
      const event2 = new keyboardEvent.StandardKeyboardEvent(e);
      let eventHandled = false;
      if (this.enabled && (event2.equals(
        3
        /* Enter */
      ) || event2.equals(
        10
        /* Space */
      ))) {
        this._onDidClick.fire(e);
        eventHandled = true;
      } else if (event2.equals(
        9
        /* Escape */
      )) {
        this._element.blur();
        eventHandled = true;
      }
      if (eventHandled) {
        dom.EventHelper.stop(event2, true);
      }
    }));
    this._register(dom.addDisposableListener(this._element, dom.EventType.MOUSE_OVER, (e) => {
      if (!this._element.classList.contains("disabled")) {
        this.setHoverBackground();
      }
    }));
    this._register(dom.addDisposableListener(this._element, dom.EventType.MOUSE_OUT, (e) => {
      this.applyStyles();
    }));
    this.focusTracker = this._register(dom.trackFocus(this._element));
    this._register(this.focusTracker.onDidFocus(() => this.setHoverBackground()));
    this._register(this.focusTracker.onDidBlur(() => this.applyStyles()));
    this.applyStyles();
  }
  get onDidClick() {
    return this._onDidClick.event;
  }
  setHoverBackground() {
    let hoverBackground;
    if (this.options.secondary) {
      hoverBackground = this.buttonSecondaryHoverBackground ? this.buttonSecondaryHoverBackground.toString() : null;
    } else {
      hoverBackground = this.buttonHoverBackground ? this.buttonHoverBackground.toString() : null;
    }
    if (hoverBackground) {
      this._element.style.backgroundColor = hoverBackground;
    }
  }
  style(styles) {
    this.buttonForeground = styles.buttonForeground;
    this.buttonBackground = styles.buttonBackground;
    this.buttonHoverBackground = styles.buttonHoverBackground;
    this.buttonSecondaryForeground = styles.buttonSecondaryForeground;
    this.buttonSecondaryBackground = styles.buttonSecondaryBackground;
    this.buttonSecondaryHoverBackground = styles.buttonSecondaryHoverBackground;
    this.buttonBorder = styles.buttonBorder;
    this.applyStyles();
  }
  applyStyles() {
    if (this._element) {
      let background, foreground;
      if (this.options.secondary) {
        foreground = this.buttonSecondaryForeground ? this.buttonSecondaryForeground.toString() : "";
        background = this.buttonSecondaryBackground ? this.buttonSecondaryBackground.toString() : "";
      } else {
        foreground = this.buttonForeground ? this.buttonForeground.toString() : "";
        background = this.buttonBackground ? this.buttonBackground.toString() : "";
      }
      const border = this.buttonBorder ? this.buttonBorder.toString() : "";
      this._element.style.color = foreground;
      this._element.style.backgroundColor = background;
      this._element.style.borderWidth = border ? "1px" : "";
      this._element.style.borderStyle = border ? "solid" : "";
      this._element.style.borderColor = border;
    }
  }
  get element() {
    return this._element;
  }
  set label(value) {
    this._element.classList.add("monaco-text-button");
    if (this.options.supportIcons) {
      dom.reset(this._element, ...iconLabels.renderLabelWithIcons(value));
    } else {
      this._element.textContent = value;
    }
    if (typeof this.options.title === "string") {
      this._element.title = this.options.title;
    } else if (this.options.title) {
      this._element.title = value;
    }
  }
  set enabled(value) {
    if (value) {
      this._element.classList.remove("disabled");
      this._element.setAttribute("aria-disabled", String(false));
      this._element.tabIndex = 0;
    } else {
      this._element.classList.add("disabled");
      this._element.setAttribute("aria-disabled", String(true));
    }
  }
  get enabled() {
    return !this._element.classList.contains("disabled");
  }
}
exports.Button = Button;
