"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 actions = require("../../../common/actions.js");
const event = require("../../../common/event.js");
require("./dropdown.css.js");
class BaseDropdown extends actions.ActionRunner {
  constructor(container, options) {
    super();
    this._onDidChangeVisibility = this._register(new event.Emitter());
    this.onDidChangeVisibility = this._onDidChangeVisibility.event;
    this._element = dom.append(container, dom.$(".monaco-dropdown"));
    this._label = dom.append(this._element, dom.$(".dropdown-label"));
    let labelRenderer = options.labelRenderer;
    if (!labelRenderer) {
      labelRenderer = (container2) => {
        container2.textContent = options.label || "";
        return null;
      };
    }
    for (const event2 of [dom.EventType.CLICK, dom.EventType.MOUSE_DOWN, touch.EventType.Tap]) {
      this._register(dom.addDisposableListener(this.element, event2, (e) => dom.EventHelper.stop(e, true)));
    }
    for (const event2 of [dom.EventType.MOUSE_DOWN, touch.EventType.Tap]) {
      this._register(dom.addDisposableListener(this._label, event2, (e) => {
        if (e instanceof MouseEvent && e.detail > 1) {
          return;
        }
        if (this.visible) {
          this.hide();
        } else {
          this.show();
        }
      }));
    }
    this._register(dom.addDisposableListener(this._label, dom.EventType.KEY_UP, (e) => {
      const event2 = new keyboardEvent.StandardKeyboardEvent(e);
      if (event2.equals(
        3
        /* Enter */
      ) || event2.equals(
        10
        /* Space */
      )) {
        dom.EventHelper.stop(e, true);
        if (this.visible) {
          this.hide();
        } else {
          this.show();
        }
      }
    }));
    const cleanupFn = labelRenderer(this._label);
    if (cleanupFn) {
      this._register(cleanupFn);
    }
    this._register(touch.Gesture.addTarget(this._label));
  }
  get element() {
    return this._element;
  }
  show() {
    if (!this.visible) {
      this.visible = true;
      this._onDidChangeVisibility.fire(true);
    }
  }
  hide() {
    if (this.visible) {
      this.visible = false;
      this._onDidChangeVisibility.fire(false);
    }
  }
  dispose() {
    super.dispose();
    this.hide();
    if (this.boxContainer) {
      this.boxContainer.remove();
      this.boxContainer = void 0;
    }
    if (this.contents) {
      this.contents.remove();
      this.contents = void 0;
    }
    if (this._label) {
      this._label.remove();
      this._label = void 0;
    }
  }
}
class DropdownMenu extends BaseDropdown {
  constructor(container, options) {
    super(container, options);
    this._actions = [];
    this._contextMenuProvider = options.contextMenuProvider;
    this.actions = options.actions || [];
    this.actionProvider = options.actionProvider;
    this.menuClassName = options.menuClassName || "";
    this.menuAsChild = !!options.menuAsChild;
  }
  set menuOptions(options) {
    this._menuOptions = options;
  }
  get menuOptions() {
    return this._menuOptions;
  }
  get actions() {
    if (this.actionProvider) {
      return this.actionProvider.getActions();
    }
    return this._actions;
  }
  set actions(actions2) {
    this._actions = actions2;
  }
  show() {
    super.show();
    this.element.classList.add("active");
    this._contextMenuProvider.showContextMenu({
      getAnchor: () => this.element,
      getActions: () => this.actions,
      getActionsContext: () => this.menuOptions ? this.menuOptions.context : null,
      getActionViewItem: (action) => this.menuOptions && this.menuOptions.actionViewItemProvider ? this.menuOptions.actionViewItemProvider(action) : void 0,
      getKeyBinding: (action) => this.menuOptions && this.menuOptions.getKeyBinding ? this.menuOptions.getKeyBinding(action) : void 0,
      getMenuClassName: () => this.menuClassName,
      onHide: () => this.onHide(),
      actionRunner: this.menuOptions ? this.menuOptions.actionRunner : void 0,
      anchorAlignment: this.menuOptions ? this.menuOptions.anchorAlignment : 0,
      domForShadowRoot: this.menuAsChild ? this.element : void 0
    });
  }
  hide() {
    super.hide();
  }
  onHide() {
    this.hide();
    this.element.classList.remove("active");
  }
}
exports.BaseDropdown = BaseDropdown;
exports.DropdownMenu = DropdownMenu;
