import type { GamePluginResponse, JobResponse, RunEndpointResponse, ServerInstanceResponse, ServerInstanceState } from "../api/types"; export type ServerWorkflowViewState = "api" | "local" | "saving"; export type ServerLifecycleActionLabel = "create" | "start" | "stop" | "refresh"; export interface ServerCreateFormState { id: string; name: string; pluginId: string; runEndpointId: string; } export interface ServerWorkflowActionState { label: ServerLifecycleActionLabel; serverInstanceId?: string; success: boolean; message: string; } export interface ServerManagementSummary { total: number; running: number; pendingJobs: number; failed: number; } export const emptyServerCreateForm: ServerCreateFormState = { id: "", name: "", pluginId: "", runEndpointId: "" }; export function summarizeServerManagement(instances: ServerInstanceResponse[], jobs: JobResponse[]): ServerManagementSummary { return { total: instances.length, running: instances.filter((instance) => instance.state === "running").length, pendingJobs: jobs.filter((job) => isPendingJobState(job.state)).length, failed: instances.filter((instance) => instance.state === "failed").length }; } export function pendingJobsForServer(jobs: JobResponse[], serverInstanceId: string): JobResponse[] { return jobs.filter((job) => job.serverInstanceId === serverInstanceId && isPendingJobState(job.state)); } export function pluginLabel(plugin: GamePluginResponse | undefined, pluginId: string): string { if (!plugin) { return pluginId; } return plugin.serverDisplayName || plugin.name || plugin.id; } export function endpointLabel(endpoint: RunEndpointResponse | undefined, runEndpointId: string): string { if (!endpoint) { return runEndpointId; } return endpoint.displayName || endpoint.id; } export function canStartServer(state: ServerInstanceState): boolean { return state === "ready" || state === "stopped"; } export function canStopServer(state: ServerInstanceState): boolean { return state === "running"; } export function isPendingJobState(state: JobResponse["state"]): boolean { return state === "queued" || state === "accepted" || state === "running"; } export function defaultServerCreateForm(plugins: GamePluginResponse[], endpoints: RunEndpointResponse[]): ServerCreateFormState { return { ...emptyServerCreateForm, pluginId: plugins[0]?.id ?? "", runEndpointId: endpoints[0]?.id ?? "" }; }