import { d as CleoError, E as ExitCode } from './paths-2M_UxD_H.js';
import { l as lockfile } from './index-IVoIz4D5.js';

//#region ../core/dist/store/lock.js
/**
* File locking using proper-lockfile.
* Prevents concurrent modifications to CLEO data files.
* @epic T4454
* @task T4457
*/
/** Default lock options. */
var DEFAULT_LOCK_OPTIONS = {
	retries: {
		retries: 3,
		minTimeout: 100,
		maxTimeout: 1e3,
		factor: 2
	},
	stale: 1e4,
	realpath: false
};
/**
* Acquire an exclusive lock on a file.
* Returns a release function that must be called when done.
*/
async function acquireLock(filePath, options) {
	try {
		return await lockfile.lock(filePath, {
			...DEFAULT_LOCK_OPTIONS,
			...options?.stale !== void 0 && { stale: options.stale },
			...options?.retries !== void 0 && { retries: {
				...DEFAULT_LOCK_OPTIONS.retries,
				retries: options.retries
			} }
		});
	} catch (err) {
		throw new CleoError(ExitCode.LOCK_TIMEOUT, `Failed to acquire lock: ${filePath}`, {
			fix: `Another process may be writing to this file. Wait and retry.`,
			cause: err
		});
	}
}
/**
* Execute a function while holding an exclusive lock on a file.
* The lock is automatically released when the function completes (or throws).
*/
async function withLock(filePath, fn, options) {
	const release = await acquireLock(filePath, options);
	try {
		return await fn();
	} finally {
		await release();
	}
}

export { withLock as w };
//# sourceMappingURL=lock-BoHF25mx.js.map
