// SPEC: PJ2026-010403 API契约 draft-2026-06-18-r1; PJ2026-010401 Web工作台 draft-2026-06-18-r1.
// Responsibility: Workbench UI projection helpers that keep route/explicit selection separate from server snapshots.

import { normalizeWorkbenchSessionId } from "@/utils";

export function initialWorkbenchSessionIdFromLocation(location: Pick<Location, "pathname"> | null = typeof window === "undefined" ? null : window.location): string | null {
  const pathname = location?.pathname ?? "";
  const match = /^\/workbench\/sessions\/([^/?#]+)/u.exec(pathname);
  if (!match?.[1]) return null;
  try {
    return normalizeWorkbenchSessionId(decodeURIComponent(match[1]));
  } catch {
    return normalizeWorkbenchSessionId(match[1]);
  }
}
