import { validate } from "uuid"

/**
 * Extract work-session id from the current URL.
 * Supports racletteJS route format: /:workSession/id={uuid}
 */
export const extractSessionIdFromUrl = (): string | null => {
  const path = window.location.pathname + (window.location.hash || "")
  const workSessionMatch = path.match(/\/:workSession\/id=([^/&#]+)/)

  if (workSessionMatch?.[1] && validate(workSessionMatch[1])) {
    return workSessionMatch[1]
  }

  return null
}
