/** Loading phases shown while opening a UltiDocs editor. */
export type DocsLoadingPhase = "opening" | "session" | "import" | "collaboration" | "connecting"

const PHASE_LABELS: Record<DocsLoadingPhase, string> = {
  opening: "Ouverture du document…",
  session: "Préparation de l'éditeur…",
  import: "Import du document…",
  collaboration: "Connexion à la collaboration…",
  connecting: "Chargement…",
}

export function docsLoadingPhaseLabel(phase: DocsLoadingPhase): string {
  return PHASE_LABELS[phase]
}

export function resolveDocsShellSplashPhase(opts: {
  resolvingFile: boolean
  awaitingSession: boolean
  documentPhase: DocsLoadingPhase
}): DocsLoadingPhase {
  if (opts.resolvingFile) return "opening"
  if (opts.awaitingSession) return "session"
  return opts.documentPhase
}

export function resolveRichTextDocumentLoadingPhase(opts: {
  contentImportPending: boolean
  importDone: boolean
  importRequired: boolean
  collaboration: boolean
  collabSynced: boolean
}): DocsLoadingPhase {
  if (opts.contentImportPending && !opts.importDone) return "import"
  if (opts.importRequired && !opts.importDone) return "import"
  if (opts.collaboration && !opts.collabSynced) return "collaboration"
  return "connecting"
}

export function resolveUltidrawDocumentLoadingPhase(opts: {
  collaboration: boolean
  collabSynced: boolean
}): DocsLoadingPhase {
  if (opts.collaboration && !opts.collabSynced) return "collaboration"
  return "opening"
}
