import type { ApiAlertRule } from "../alerts-service.js"

const COOLDOWN_MS: Record<ApiAlertRule["cooldown"], number> = {
  "1м": 60_000,
  "5м": 5 * 60_000,
  "15м": 15 * 60_000,
  "1ч": 60 * 60_000,
  "4ч": 4 * 60 * 60_000,
  "24ч": 24 * 60 * 60_000,
}

export function cooldownToMs(cd: ApiAlertRule["cooldown"]): number {
  return COOLDOWN_MS[cd] ?? COOLDOWN_MS["5м"]
}

/** Можно ли отправить снова после lastIso (ISO) */
export function isCooldownElapsed(lastFiredIso: string | null | undefined, cooldown: ApiAlertRule["cooldown"]): boolean {
  if (!lastFiredIso?.trim()) return true
  const t = Date.parse(lastFiredIso)
  if (!Number.isFinite(t)) return true
  return Date.now() - t >= cooldownToMs(cooldown)
}
