/**
 * Parse a short duration string (`15m`, `30d`, `90s`, `12h`) to milliseconds.
 * Used for the refresh-cookie `maxAge` and `admin_refresh_tokens.expires_at`;
 * `jsonwebtoken` accepts the same string form directly for `expiresIn`.
 */
const UNIT_MS: Record<string, number> = {
  s: 1000,
  m: 60_000,
  h: 3_600_000,
  d: 86_400_000,
};

export function durationToMs(input: string): number {
  const match = /^(\d+)\s*(s|m|h|d)$/.exec(input.trim());
  if (!match) {
    throw new Error(`Unsupported duration: "${input}" (expected e.g. "15m", "30d")`);
  }
  return Number(match[1]) * UNIT_MS[match[2]];
}
