const TOKEN_RE = /^[a-zA-Z0-9_-]{16,64}$/;

export function isValidShareToken(token: string): boolean {
  return TOKEN_RE.test(token);
}

export function generateShareToken(): string {
  const bytes = new Uint8Array(24);
  crypto.getRandomValues(bytes);
  return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
}

export function shareUrlFromRequest(reqUrl: string, token: string): string {
  const origin = new URL(reqUrl).origin;
  return `${origin}/?token=${encodeURIComponent(token)}`;
}
