import { __esmMin } from "../../_virtual/_rolldown/runtime.js";
import { isAbsolute, relative, resolve, sep } from "node:path";
import { homedir } from "node:os";
//#region src/browser/cache-dir-policy.ts
function expandHome(input) {
	if (input === "~") return homedir();
	if (input.startsWith("~/") || input.startsWith(`~${sep}`)) return resolve(homedir(), input.slice(2));
	return input;
}
/**
* Returns `{ ok: true, resolved }` when `input` is an absolute (or `~`-rooted)
* path that resolves inside the user's home directory. Empty / missing input
* is allowed (callers fall back to their own default).
*/
function checkCacheDir(input) {
	if (input === void 0 || input === null) return {
		ok: true,
		resolved: ""
	};
	const trimmed = input.trim();
	if (!trimmed) return {
		ok: true,
		resolved: ""
	};
	const expanded = expandHome(trimmed);
	if (!isAbsolute(expanded)) return {
		ok: false,
		resolved: expanded,
		message: "cacheDir must be an absolute path (or start with ~/)"
	};
	const resolved = resolve(expanded);
	const home = resolve(homedir());
	const rel = relative(home, resolved);
	if (rel === "" || !rel.startsWith("..") && !isAbsolute(rel)) return {
		ok: true,
		resolved
	};
	return {
		ok: false,
		resolved,
		message: `cacheDir must live under your home directory (${home}); got ${resolved}`
	};
}
/** Throws when the path violates the policy. */
function assertCacheDir(input) {
	const r = checkCacheDir(input);
	if (r.ok === false) throw new Error(r.message);
	return r.resolved;
}
var init_cache_dir_policy = __esmMin((() => {}));
//#endregion
init_cache_dir_policy();
export { assertCacheDir, checkCacheDir, expandHome, init_cache_dir_policy };

//# sourceMappingURL=cache-dir-policy.js.map