"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
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 with_exports = {};
__export(with_exports, {
  withSpinner: () => withSpinner,
  withSpinnerRestore: () => withSpinnerRestore,
  withSpinnerSync: () => withSpinnerSync
});
module.exports = __toCommonJS(with_exports);
var import_node_process = __toESM(require("node:process"));
var import_convert = require("../colors/convert");
async function withSpinner(options) {
  const { message, operation, spinner, withOptions } = {
    __proto__: null,
    ...options
  };
  if (!spinner) {
    return await operation();
  }
  const savedColor = withOptions?.color !== void 0 ? spinner.color : void 0;
  const savedShimmerState = withOptions?.shimmer !== void 0 ? spinner.shimmerState : void 0;
  if (withOptions?.color !== void 0) {
    spinner.color = (0, import_convert.toRgb)(withOptions.color);
  }
  if (withOptions?.shimmer !== void 0) {
    if (typeof withOptions.shimmer === "string") {
      spinner.updateShimmer({ dir: withOptions.shimmer });
    } else {
      spinner.setShimmer(withOptions.shimmer);
    }
  }
  spinner.start(message);
  try {
    return await operation();
  } finally {
    const wasSpinning = spinner.isSpinning;
    spinner.stop();
    if (wasSpinning) {
      import_node_process.default.stderr.write("\r\x1B[2K");
    }
    if (savedColor !== void 0) {
      spinner.color = savedColor;
    }
    if (withOptions?.shimmer !== void 0) {
      if (savedShimmerState) {
        spinner.setShimmer({
          color: savedShimmerState.color,
          dir: savedShimmerState.direction,
          speed: savedShimmerState.speed
        });
      } else {
        spinner.disableShimmer();
      }
    }
  }
}
async function withSpinnerRestore(options) {
  const { operation, spinner, wasSpinning } = {
    __proto__: null,
    ...options
  };
  try {
    return await operation();
  } finally {
    if (spinner && wasSpinning) {
      spinner.start();
    }
  }
}
function withSpinnerSync(options) {
  const { message, operation, spinner, withOptions } = {
    __proto__: null,
    ...options
  };
  if (!spinner) {
    return operation();
  }
  const savedColor = withOptions?.color !== void 0 ? spinner.color : void 0;
  const savedShimmerState = withOptions?.shimmer !== void 0 ? spinner.shimmerState : void 0;
  if (withOptions?.color !== void 0) {
    spinner.color = (0, import_convert.toRgb)(withOptions.color);
  }
  if (withOptions?.shimmer !== void 0) {
    if (typeof withOptions.shimmer === "string") {
      spinner.updateShimmer({ dir: withOptions.shimmer });
    } else {
      spinner.setShimmer(withOptions.shimmer);
    }
  }
  spinner.start(message);
  try {
    return operation();
  } finally {
    spinner.stop();
    if (savedColor !== void 0) {
      spinner.color = savedColor;
    }
    if (withOptions?.shimmer !== void 0) {
      if (savedShimmerState) {
        spinner.setShimmer({
          color: savedShimmerState.color,
          dir: savedShimmerState.direction,
          speed: savedShimmerState.speed
        });
      } else {
        spinner.disableShimmer();
      }
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  withSpinner,
  withSpinnerRestore,
  withSpinnerSync
});
