"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_colors_convert = require('../colors/convert.js');
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/spinner/with.ts
/**
* @file Lifecycle wrappers around `Spinner` — `withSpinner` (async, push-pop
*   options + auto-stop), `withSpinnerRestore` (conditionally restart a
*   previously-spinning instance), and `withSpinnerSync` (sync sibling of
*   `withSpinner`). Each wrapper guarantees `spinner.stop()` runs via
*   `try/finally` even when the inner operation throws, then re-throws the
*   original error so callers see the same failure surface as a plain `await`.
*/
/**
* Execute an async operation with spinner lifecycle management. Ensures
* `spinner.stop()` is always called via try/finally, even if the operation
* throws. Provides safe cleanup and consistent spinner behavior.
*
* @example
*   ;```ts
*   import { Spinner } from '@socketsecurity/lib/spinner/spinner'
*   import { withSpinner } from '@socketsecurity/lib/spinner/with'
*
*   const spinner = Spinner()
*
*   // With spinner instance
*   const result = await withSpinner({
*     message: 'Processing…',
*     operation: async () => {
*       return await processData()
*     },
*     spinner,
*   })
*
*   // Without spinner instance (no-op, just runs operation)
*   const result = await withSpinner({
*     message: 'Processing…',
*     operation: async () => {
*       return await processData()
*     },
*   })
*   ```
*
* @template T - Return type of the operation.
*
* @param options - Configuration object.
* @param options.message - Message to display while spinner is running.
* @param options.operation - Async function to execute.
* @param options.spinner - Optional spinner instance (if not provided, no
*   spinner is used)
*
* @returns Result of the operation
*
* @throws Re-throws any error from operation after stopping spinner
*/
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 = require_colors_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();
		/* c8 ignore start */
		if (wasSpinning) 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();
	}
}
/**
* Execute an async operation with conditional spinner restart. Useful when you
* need to temporarily stop a spinner for an operation, then restore it to its
* previous state (if it was spinning).
*
* @example
*   ;```ts
*   import { getDefaultSpinner } from '@socketsecurity/lib/spinner/default'
*   import { withSpinnerRestore } from '@socketsecurity/lib/spinner/with'
*
*   const spinner = getDefaultSpinner()
*   const wasSpinning = spinner.isSpinning
*   spinner.stop()
*
*   const result = await withSpinnerRestore({
*     operation: async () => {
*       // Do work without spinner
*       return await someOperation()
*     },
*     spinner,
*     wasSpinning,
*   })
*   // Spinner is automatically restarted if wasSpinning was true
*   ```
*
* @template T - Return type of the operation.
*
* @param options - Configuration object.
* @param options.operation - Async function to execute.
* @param options.spinner - Optional spinner instance to manage.
* @param options.wasSpinning - Whether spinner was spinning before the
*   operation.
*
* @returns Result of the operation
*
* @throws Re-throws any error from operation after restoring spinner state
*/
async function withSpinnerRestore(options) {
	const { operation, spinner, wasSpinning } = {
		__proto__: null,
		...options
	};
	try {
		return await operation();
	} finally {
		if (spinner && wasSpinning) spinner.start();
	}
}
/**
* Execute a synchronous operation with spinner lifecycle management. Ensures
* `spinner.stop()` is always called via try/finally, even if the operation
* throws. Provides safe cleanup and consistent spinner behavior for sync
* operations.
*
* @example
*   ;```ts
*   import { Spinner } from '@socketsecurity/lib/spinner/spinner'
*   import { withSpinnerSync } from '@socketsecurity/lib/spinner/with'
*
*   const spinner = Spinner()
*
*   const result = withSpinnerSync({
*     message: 'Processing…',
*     operation: () => {
*       return processDataSync()
*     },
*     spinner,
*   })
*   ```
*
* @template T - Return type of the operation.
*
* @param options - Configuration object.
* @param options.message - Message to display while spinner is running.
* @param options.operation - Synchronous function to execute.
* @param options.spinner - Optional spinner instance (if not provided, no
*   spinner is used)
*
* @returns Result of the operation
*
* @throws Re-throws any error from operation after stopping spinner
*/
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 = require_colors_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();
		/* c8 ignore start */
		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();
	}
}

//#endregion
exports.withSpinner = withSpinner;
exports.withSpinnerRestore = withSpinnerRestore;
exports.withSpinnerSync = withSpinnerSync;