variable :: R.Region -> Env.Environment -> String -> Result.ResultErr Var.Canonical
variable region env var =
  case toVarName var of
    Right (name, varName)
        | ModuleName.isNative name ->
            error "TODO" "Result.var (Var.Canonical (Var.Module name) varName)"

    _ ->
        case Set.toList `fmap` Map.lookup var (Env._values env) of
          Just [v] ->
              Result.var v

          Just vs ->
              preferLocals region env "variable" vs var

          Nothing ->
              notFound region "variable" (Map.keys (Env._values env)) var
variable :: R.Region -> Env.Environment -> String -> Result.ResultErr Var.Canonical
variable region env var =
  case toVarName var of
    Right (name, varName)
        | ModuleName.isNative name ->
            error "TODO" "Result.var (Var.Canonical (Var.Module name) varName)"

    _ ->
        case Set.toList `fmap` Map.lookup var (Env._values env) of
          Just [v] ->
              Result.var v

          Just vs ->
              preferLocals region env "variable" vs var

          Nothing ->
              notFound region "variable" (Map.keys (Env._values env)) var
variable region env var =
  case toVarName var of
    Right (name, varName)
        | ModuleName.isNative name ->
            error "TODO" "Result.var (Var.Canonical (Var.Module name) varName)"

    _ ->
        case Set.toList `fmap` Map.lookup var (Env._values env) of
          Just [v] ->
              Result.var v

          Just vs ->
              preferLocals region env "variable" vs var

          Nothing ->
              notFound region "variable" (Map.keys (Env._values env)) var