## `(<package source> <volatile uri> [--cache:<map>])-><package handle>?`
##
## resolves a package handle using the specified cache
function(package_source_resolve package_source uri)
  set(args ${ARGN})
  list_extract_labelled_value(args --cache)
  ans(cache)
  if(NOT cache)
    map_new()
    ans(cache)
  endif()

  map_has(${cache} "${uri}")
  ans(hit)
  if(hit)
    map_tryget(${cache} ${uri})
    ans(resolved_handle)
    list(LENGTH resolved_handle count)
    if(NOT ${count} EQUAL 1)
      return()
    endif()
  else()
    call(package_source.resolve("${uri}"))
    ans(resolved_handle)
    map_set(${cache} ${uri} ${resolved_handle})
    if(NOT resolved_handle)
      return()
    endif()
    map_tryget(${resolved_handle} uri)
    ans(dependable_uri)
    map_set(${cache} ${dependable_uri} ${resolved_handle})
  endif()

  return_ref(resolved_handle)
endfunction()