import { computed } from 'vue'
import { useRoute, type RouteLocationNormalizedLoaded } from 'vue-router'

function normalizeParam(param: unknown): string {
  if (Array.isArray(param)) {
    return typeof param[0] === 'string' ? param[0] : ''
  }
  return typeof param === 'string' ? param : ''
}

export function useRouteVersionId(routeArg?: RouteLocationNormalizedLoaded) {
  const route = routeArg ?? useRoute()
  return computed(() => normalizeParam(route.params.version ?? route.params.versionId))
}
