export function headerProject(session, selectedWorkspace, workspaces, preferSelected = false) {
  if (preferSelected && selectedWorkspace) return selectedWorkspace;
  if (!session) return undefined;
  const workspace = workspaces.find((item) => item.id === session.workspaceId);
  if (workspace) return workspace;
  if (session.projectLabel) return { id: session.workspaceId, label: session.projectLabel };
  return undefined;
}

export function accountSwitchLocked(sessions = [], currentSession = null) {
  return false;
}

export function accountSwitchLockAccountId(sessions = [], currentSession = null) {
  return "";
}

export function accountSwitchAllowed(accountId, selectedAccountId, lockedAccountId = "") {
  return !lockedAccountId || accountId === selectedAccountId || accountId === lockedAccountId;
}

export function chatProgressLabel(session, t) {
  if (!session) return t("context.chat.noSelection");
  return t({
    opening: "transition.opening",
    starting: "context.chat.starting",
    running: "activity.running",
    waiting_approval: "context.chat.waitingApproval",
    waiting_response: "context.chat.waitingResponse",
    completed: "context.chat.complete",
    failed: "context.chat.failed",
    interrupted: "context.chat.interrupted"
  }[session.status] ?? "context.chat.ready");
}

export function chatProgressTone(session) {
  if (["opening", "starting", "running", "waiting_approval", "waiting_response"].includes(session?.status)) return "active";
  if (["failed", "interrupted"].includes(session?.status)) return "error";
  if (session?.status === "completed") return "complete";
  return "idle";
}

export function lastChatProjectLabel(session, project, t) {
  if (project?.label) return project.label;
  return session ? t("context.project.unknown") : t("context.chat.none");
}
