/* Socket Lib - Built with esbuild */
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var progress_exports = {};
__export(progress_exports, {
  ProgressBar: () => ProgressBar,
  createProgressIndicator: () => createProgressIndicator
});
module.exports = __toCommonJS(progress_exports);
var import_yoctocolors_cjs = __toESM(require("../external/yoctocolors-cjs"));
var import_strings = require("../strings");
class ProgressBar {
  current = 0;
  total;
  startTime;
  lastRender = 0;
  stream;
  options;
  terminated = false;
  lastDrawnWidth = 0;
  constructor(total, options) {
    this.total = total;
    this.startTime = Date.now();
    this.stream = options?.stream || process.stderr;
    this.options = {
      width: 40,
      format: ":bar :percent :current/:total",
      complete: "\u2588",
      incomplete: "\u2591",
      head: "",
      clear: false,
      // ~60fps.
      renderThrottle: 16,
      stream: this.stream,
      color: "cyan",
      ...options
    };
  }
  /**
   * Update progress and redraw bar.
   */
  update(current, tokens) {
    if (this.terminated) {
      return;
    }
    this.current = Math.min(current, this.total);
    const now = Date.now();
    if (now - this.lastRender < this.options.renderThrottle && this.current < this.total) {
      return;
    }
    this.lastRender = now;
    this.render(tokens);
    if (this.current >= this.total) {
      this.terminate();
    }
  }
  /**
   * Increment progress by amount.
   */
  tick(amount = 1, tokens) {
    this.update(this.current + amount, tokens);
  }
  /**
   * Render the progress bar.
   */
  render(tokens) {
    const colorFn = import_yoctocolors_cjs.default[this.options.color] || ((s) => s);
    const percent = Math.floor(this.current / this.total * 100);
    const elapsed = Date.now() - this.startTime;
    const eta = this.current === 0 ? 0 : elapsed / this.current * (this.total - this.current);
    const availableWidth = this.options.width;
    const filledWidth = Math.floor(this.current / this.total * availableWidth);
    const emptyWidth = availableWidth - filledWidth;
    const filled = (0, import_strings.repeatString)(this.options.complete, filledWidth);
    const empty = (0, import_strings.repeatString)(this.options.incomplete, emptyWidth);
    const bar = colorFn(filled) + empty;
    let output = this.options.format;
    output = output.replace(":bar", bar);
    output = output.replace(":percent", `${percent}%`);
    output = output.replace(":current", String(this.current));
    output = output.replace(":total", String(this.total));
    output = output.replace(":elapsed", this.formatTime(elapsed));
    output = output.replace(":eta", this.formatTime(eta));
    if (tokens) {
      for (const [key, value] of Object.entries(tokens)) {
        output = output.replace(`:${key}`, String(value));
      }
    }
    this.clearLine();
    this.stream.write(output);
    this.lastDrawnWidth = (0, import_strings.stripAnsi)(output).length;
  }
  /**
   * Clear the current line.
   */
  clearLine() {
    if (this.stream.isTTY) {
      this.stream.cursorTo(0);
      this.stream.clearLine(0);
    } else if (this.lastDrawnWidth > 0) {
      this.stream.write(`\r${(0, import_strings.repeatString)(" ", this.lastDrawnWidth)}\r`);
    }
  }
  /**
   * Format time in seconds to human readable.
   */
  formatTime(ms) {
    const seconds = Math.round(ms / 1e3);
    if (seconds < 60) {
      return `${seconds}s`;
    }
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = seconds % 60;
    return `${minutes}m${remainingSeconds}s`;
  }
  /**
   * Terminate the progress bar.
   */
  terminate() {
    if (this.terminated) {
      return;
    }
    this.terminated = true;
    if (this.options.clear) {
      this.clearLine();
    } else {
      this.stream.write("\n");
    }
  }
}
function createProgressIndicator(current, total, label) {
  const percent = Math.floor(current / total * 100);
  const progress = `${current}/${total}`;
  let output = "";
  if (label) {
    output += `${label}: `;
  }
  output += `${import_yoctocolors_cjs.default.cyan(`[${percent}%]`)} ${progress}`;
  return output;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ProgressBar,
  createProgressIndicator
});
//# sourceMappingURL=progress.js.map
