// Pure helpers shared by `SecretForm` (compound form for new-secret flows)
// and the reuse tests. UI state lives in `secret-form.tsx`.

export function slugifyForSecretId(input: string): string {
  return input
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/^-+|-+$/g, "");
}

const normalizeSecretId = (secretId: string): string => secretId.trim();

export function isSecretIdTaken(secretId: string, existingSecretIds: Iterable<string>): boolean {
  const normalizedId = normalizeSecretId(secretId);
  if (!normalizedId) return false;

  for (const existingSecretId of existingSecretIds) {
    if (normalizeSecretId(existingSecretId) === normalizedId) {
      return true;
    }
  }

  return false;
}

export function getUniqueSecretId(
  baseName: string,
  existingSecretIds: Iterable<string>,
  fallbackId = "secret",
): string {
  const baseId = slugifyForSecretId(baseName) || fallbackId;
  if (!baseId) return "";
  if (!isSecretIdTaken(baseId, existingSecretIds)) return baseId;

  let suffix = 2;
  while (isSecretIdTaken(`${baseId}-${suffix}`, existingSecretIds)) {
    suffix += 1;
  }
  return `${baseId}-${suffix}`;
}
