import { __esmMin } from "../../_virtual/_rolldown/runtime.js";
//#region src/routing/account-id.ts
/**
* Normalize an account id string.
*
* Rules:
* - Only `a-z`, `0-9`, `_`, `-`
* - Must not start with `-` or `_`
* - Max length 64
* - Empty input -> `default`
*/
function normalizeAccountId(value) {
	const trimmed = (value ?? "").trim();
	if (!trimmed) return DEFAULT_ACCOUNT_ID;
	const cached = normalizeAccountIdCache.get(trimmed);
	if (cached) return cached;
	const normalized = canonicalizeAccountId(trimmed) || "default";
	setNormalizeCache(normalizeAccountIdCache, trimmed, normalized);
	return normalized;
}
/**
* Like `normalizeAccountId`, but empty input yields `undefined` (not `default`).
*/
function normalizeOptionalAccountId(value) {
	const trimmed = (value ?? "").trim();
	if (!trimmed) return;
	if (normalizeOptionalAccountIdCache.has(trimmed)) return normalizeOptionalAccountIdCache.get(trimmed);
	const normalized = canonicalizeAccountId(trimmed) || void 0;
	setNormalizeCache(normalizeOptionalAccountIdCache, trimmed, normalized);
	return normalized;
}
/**
* Whether the value already matches the strict account id pattern.
*/
function isValidAccountId(value) {
	const trimmed = (value ?? "").trim();
	return Boolean(trimmed) && VALID_ID_RE.test(trimmed);
}
/**
* Alias for `normalizeAccountId` (same canonicalization as session segments).
*/
function sanitizeAccountId(value) {
	return normalizeAccountId(value);
}
function canonicalizeAccountId(value) {
	if (VALID_ID_RE.test(value)) return value.toLowerCase();
	return value.toLowerCase().replace(INVALID_CHARS_RE, "-").replace(LEADING_DASH_RE, "").replace(TRAILING_DASH_RE, "").slice(0, 64) || void 0;
}
function setNormalizeCache(cache, key, value) {
	cache.set(key, value);
	if (cache.size <= ACCOUNT_ID_CACHE_MAX) return;
	const oldest = cache.keys().next();
	if (!oldest.done) cache.delete(oldest.value);
}
var DEFAULT_ACCOUNT_ID, VALID_ID_RE, INVALID_CHARS_RE, LEADING_DASH_RE, TRAILING_DASH_RE, ACCOUNT_ID_CACHE_MAX, normalizeAccountIdCache, normalizeOptionalAccountIdCache;
var init_account_id = __esmMin((() => {
	DEFAULT_ACCOUNT_ID = "default";
	VALID_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/i;
	INVALID_CHARS_RE = /[^a-z0-9_-]+/g;
	LEADING_DASH_RE = /^-+/;
	TRAILING_DASH_RE = /-+$/;
	ACCOUNT_ID_CACHE_MAX = 512;
	normalizeAccountIdCache = /* @__PURE__ */ new Map();
	normalizeOptionalAccountIdCache = /* @__PURE__ */ new Map();
}));
//#endregion
init_account_id();
export { DEFAULT_ACCOUNT_ID, init_account_id, isValidAccountId, normalizeAccountId, normalizeOptionalAccountId, sanitizeAccountId };

//# sourceMappingURL=account-id.js.map