import type { HostSharedState } from "../sdk/protocol";
import { toEvent, toService, type RuntimeEvent, type Service } from "./model";

export function servicesFrom(state: HostSharedState | null): Service[] {
  if (!state?.snapshot) return [];
  const deployments = new Map(
    state.snapshot.deployments.map((deployment) => [
      deployment.service_id,
      deployment.active_revision,
    ]),
  );
  return state.snapshot.services.map((service) => toService(service, deployments.get(service.id)));
}

export function eventsFrom(state: HostSharedState | null): RuntimeEvent[] {
  return state?.snapshot?.events.map(toEvent) ?? [];
}
