"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const event = require("./event.js");
const lifecycle = require("./lifecycle.js");
const nls = require("../../nls.js");
var __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function(resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function(resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
class Action extends lifecycle.Disposable {
  constructor(id, label = "", cssClass = "", enabled = true, actionCallback) {
    super();
    this._onDidChange = this._register(new event.Emitter());
    this.onDidChange = this._onDidChange.event;
    this._enabled = true;
    this._id = id;
    this._label = label;
    this._cssClass = cssClass;
    this._enabled = enabled;
    this._actionCallback = actionCallback;
  }
  get id() {
    return this._id;
  }
  get label() {
    return this._label;
  }
  set label(value) {
    this._setLabel(value);
  }
  _setLabel(value) {
    if (this._label !== value) {
      this._label = value;
      this._onDidChange.fire({ label: value });
    }
  }
  get tooltip() {
    return this._tooltip || "";
  }
  set tooltip(value) {
    this._setTooltip(value);
  }
  _setTooltip(value) {
    if (this._tooltip !== value) {
      this._tooltip = value;
      this._onDidChange.fire({ tooltip: value });
    }
  }
  get class() {
    return this._cssClass;
  }
  set class(value) {
    this._setClass(value);
  }
  _setClass(value) {
    if (this._cssClass !== value) {
      this._cssClass = value;
      this._onDidChange.fire({ class: value });
    }
  }
  get enabled() {
    return this._enabled;
  }
  set enabled(value) {
    this._setEnabled(value);
  }
  _setEnabled(value) {
    if (this._enabled !== value) {
      this._enabled = value;
      this._onDidChange.fire({ enabled: value });
    }
  }
  get checked() {
    return this._checked;
  }
  set checked(value) {
    this._setChecked(value);
  }
  _setChecked(value) {
    if (this._checked !== value) {
      this._checked = value;
      this._onDidChange.fire({ checked: value });
    }
  }
  run(event2, data) {
    return __awaiter(this, void 0, void 0, function* () {
      if (this._actionCallback) {
        yield this._actionCallback(event2);
      }
    });
  }
}
class ActionRunner extends lifecycle.Disposable {
  constructor() {
    super(...arguments);
    this._onBeforeRun = this._register(new event.Emitter());
    this.onBeforeRun = this._onBeforeRun.event;
    this._onDidRun = this._register(new event.Emitter());
    this.onDidRun = this._onDidRun.event;
  }
  run(action, context) {
    return __awaiter(this, void 0, void 0, function* () {
      if (!action.enabled) {
        return;
      }
      this._onBeforeRun.fire({ action });
      let error = void 0;
      try {
        yield this.runAction(action, context);
      } catch (e) {
        error = e;
      }
      this._onDidRun.fire({ action, error });
    });
  }
  runAction(action, context) {
    return __awaiter(this, void 0, void 0, function* () {
      yield action.run(context);
    });
  }
}
class Separator extends Action {
  constructor(label) {
    super(Separator.ID, label, label ? "separator text" : "separator");
    this.checked = false;
    this.enabled = false;
  }
}
Separator.ID = "vs.actions.separator";
class SubmenuAction {
  constructor(id, label, actions, cssClass) {
    this.tooltip = "";
    this.enabled = true;
    this.checked = void 0;
    this.id = id;
    this.label = label;
    this.class = cssClass;
    this._actions = actions;
  }
  get actions() {
    return this._actions;
  }
  dispose() {
  }
  run() {
    return __awaiter(this, void 0, void 0, function* () {
    });
  }
}
class EmptySubmenuAction extends Action {
  constructor() {
    super(EmptySubmenuAction.ID, nls.localize("submenu.empty", "(empty)"), void 0, false);
  }
}
EmptySubmenuAction.ID = "vs.actions.empty";
exports.Action = Action;
exports.ActionRunner = ActionRunner;
exports.EmptySubmenuAction = EmptySubmenuAction;
exports.Separator = Separator;
exports.SubmenuAction = SubmenuAction;
