import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { useQuery } from "@tanstack/react-query";
import { fetchLatestCameraSnapshot, fetchSnapshotObjectUrl, type DeviceStatus } from "../api.js";
import { formatClock } from "../lib/format.js";
import { useLiveStore } from "../lib/live-store.js";
import { wsUrl } from "../lib/origin.js";
import { createPlatformSocket } from "../lib/platform-ws.js";
// Booth camera tiles (wiki/decisions/camera-live-view.md). Opened from the camera
// chips in the device footer; docked bottom-right, the first opened rightmost and each
// next one to its left. A tile rests on the camera's newest entry/exit snapshot and
// switches to live video while that camera is seeing a vehicle (the server's per-camera
// activity, pushed over /api/ws). Live frames come over their own socket as JPEG
// binaries; only the newest frame is shown. Advisory viewing — nothing here gates
// anything.
type LiveState =
| { state: "connecting" }
| { state: "live" }
| { state: "retrying"; reason: string }
| { state: "unavailable"; reason: string };
/** Delay before re-opening a live socket that dropped while the camera is still active. */
const RECONNECT_MS = 2000;
export function CameraDock({
openIds,
cameras,
label,
canSeeSnapshots,
onClose,
}: {
openIds: string[];
/** Configured cameras (for labels, and to skip a tile whose camera was removed). */
cameras: DeviceStatus[];
label: (d: DeviceStatus) => string;
canSeeSnapshots: boolean;
onClose: (deviceId: string) => void;
}) {
const byId = new Map(cameras.map((c) => [c.deviceId, c]));
const shown = openIds.filter((id) => byId.has(id));
if (shown.length === 0) return null;
return (
// Above the footer; first opened rightmost (row-reverse), the next to its left.