"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../dom.js");
const async = require("../../../common/async.js");
const color = require("../../../common/color.js");
const lifecycle = require("../../../common/lifecycle.js");
const objects = require("../../../common/objects.js");
require("./progressbar.css.js");
const CSS_DONE = "done";
const CSS_ACTIVE = "active";
const CSS_INFINITE = "infinite";
const CSS_INFINITE_LONG_RUNNING = "infinite-long-running";
const CSS_DISCRETE = "discrete";
const defaultOpts = {
  progressBarBackground: color.Color.fromHex("#0E70C0")
};
class ProgressBar extends lifecycle.Disposable {
  constructor(container, options) {
    super();
    this.options = options || /* @__PURE__ */ Object.create(null);
    objects.mixin(this.options, defaultOpts, false);
    this.workedVal = 0;
    this.progressBarBackground = this.options.progressBarBackground;
    this.showDelayedScheduler = this._register(new async.RunOnceScheduler(() => dom.show(this.element), 0));
    this.longRunningScheduler = this._register(new async.RunOnceScheduler(() => this.infiniteLongRunning(), ProgressBar.LONG_RUNNING_INFINITE_THRESHOLD));
    this.create(container);
  }
  create(container) {
    this.element = document.createElement("div");
    this.element.classList.add("monaco-progress-container");
    this.element.setAttribute("role", "progressbar");
    this.element.setAttribute("aria-valuemin", "0");
    container.appendChild(this.element);
    this.bit = document.createElement("div");
    this.bit.classList.add("progress-bit");
    this.element.appendChild(this.bit);
    this.applyStyles();
  }
  off() {
    this.bit.style.width = "inherit";
    this.bit.style.opacity = "1";
    this.element.classList.remove(CSS_ACTIVE, CSS_INFINITE, CSS_INFINITE_LONG_RUNNING, CSS_DISCRETE);
    this.workedVal = 0;
    this.totalWork = void 0;
    this.longRunningScheduler.cancel();
  }
  /**
   * Stops the progressbar from showing any progress instantly without fading out.
   */
  stop() {
    return this.doDone(false);
  }
  doDone(delayed) {
    this.element.classList.add(CSS_DONE);
    if (!this.element.classList.contains(CSS_INFINITE)) {
      this.bit.style.width = "inherit";
      if (delayed) {
        setTimeout(() => this.off(), 200);
      } else {
        this.off();
      }
    } else {
      this.bit.style.opacity = "0";
      if (delayed) {
        setTimeout(() => this.off(), 200);
      } else {
        this.off();
      }
    }
    return this;
  }
  /**
   * Use this mode to indicate progress that has no total number of work units.
   */
  infinite() {
    this.bit.style.width = "2%";
    this.bit.style.opacity = "1";
    this.element.classList.remove(CSS_DISCRETE, CSS_DONE, CSS_INFINITE_LONG_RUNNING);
    this.element.classList.add(CSS_ACTIVE, CSS_INFINITE);
    this.longRunningScheduler.schedule();
    return this;
  }
  infiniteLongRunning() {
    this.element.classList.add(CSS_INFINITE_LONG_RUNNING);
  }
  getContainer() {
    return this.element;
  }
  style(styles) {
    this.progressBarBackground = styles.progressBarBackground;
    this.applyStyles();
  }
  applyStyles() {
    if (this.bit) {
      const background = this.progressBarBackground ? this.progressBarBackground.toString() : "";
      this.bit.style.backgroundColor = background;
    }
  }
}
ProgressBar.LONG_RUNNING_INFINITE_THRESHOLD = 1e4;
exports.ProgressBar = ProgressBar;
