"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_paths_normalize = require('../paths/normalize.js');
const require_node_fs = require('../node/fs.js');
const require_node_path = require('../node/path.js');

//#region src/fs/unique.ts
/**
* @file Generate a unique filepath by appending `-1`, `-2`, … before the
*   extension until the path is free. Useful for "save next to existing" flows
*   where overwriting silently would be wrong (PDF reports, exports, etc.).
*/
/**
* Generate a unique filepath by adding number suffix if the path exists.
* Appends `-1`, `-2`, etc. before the file extension until a non-existent path
* is found. Useful for creating files without overwriting existing ones.
*
* @example
*   ;```ts
*   // If 'report.pdf' exists, returns 'report-1.pdf'
*   const uniquePath = uniqueSync('./report.pdf')
*
*   // If 'data.json' and 'data-1.json' exist, returns 'data-2.json'
*   const path = uniqueSync('./data.json')
*
*   // If 'backup' doesn't exist, returns 'backup' unchanged
*   const backupPath = uniqueSync('./backup')
*   ```
*
* @param filepath - Desired file path.
*
* @returns Normalized unique filepath (original if it doesn't exist, or with
*   number suffix)
*/
/*@__NO_SIDE_EFFECTS__*/
function uniqueSync(filepath) {
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	const filepathStr = String(filepath);
	if (!fs.existsSync(filepathStr)) return /* @__PURE__ */ require_paths_normalize.normalizePath(filepathStr);
	const dirname = path.dirname(filepathStr);
	const ext = path.extname(filepathStr);
	const basename = path.basename(filepathStr, ext);
	let counter = 1;
	let uniquePath;
	do {
		uniquePath = path.join(dirname, `${basename}-${counter}${ext}`);
		counter++;
	} while (fs.existsSync(uniquePath));
	return /* @__PURE__ */ require_paths_normalize.normalizePath(uniquePath);
}

//#endregion
exports.uniqueSync = uniqueSync;