import type { Visibility } from './api-types'

// Mastodon Pass §4.3: Mastodon's documented `Status.visibility` enum is the
// same four values used today (`public | unlisted | private | direct`). The
// `'draft'` value is a Pixelfed extension. Mastodon-shaped forks (Glitch-soc,
// Pleroma, Akkoma, Hometown, …) sometimes emit fork-only values such as
// `'local'`; per the plan these collapse to `'public'` rather than crash the
// composer or the visibility picker. The pre-Phase-3 codebase only ever
// stored Pixelfed-emitted values, so the runtime guard was unnecessary.
const KNOWN_VISIBILITIES = new Set<Visibility>([
  'public',
  'unlisted',
  'private',
  'direct',
  'draft',
])

/**
 * Treat any unrecognised `Visibility` candidate as `'public'`. The composer
 * uses this whenever it ingests a value from the server (`source.privacy`,
 * `compose_settings.default_scope`, `Status.visibility` echoed back from a
 * reply target, …) so a fork-emitted value does not propagate into UI state
 * that would not know how to render it.
 */
export function coerceVisibility(v: string | null | undefined): Visibility {
  if (typeof v !== 'string') return 'public'
  return KNOWN_VISIBILITIES.has(v as Visibility) ? (v as Visibility) : 'public'
}
