<script lang="ts">
  import { getContext } from "svelte";

  import { Link, useLocation, useParams } from "svelte-navigator";
  import type { Writable } from "svelte/store";
  import type { Address, VALUE_TYPE } from "upend/types";
  const params = useParams();
  const location = useLocation();

  export let to: {
    entity?: Address;
    attribute?: string;
    value?: { t: VALUE_TYPE; c: string };
  };

  const { index } = getContext("browse") as { index: Writable<number> };

  let routerTo = "#";
  $: {
    if ($location.pathname.startsWith("/browse") && to.entity) {
      let addresses = $params.addresses.split(",");

      routerTo =
        "/browse/" +
        addresses
          .slice(0, $index + 1)
          .concat([to.entity])
          .join(",");
    }
  }
</script>

<Link class="uplink" to={routerTo}>
  <slot />
</Link>

<style lang="scss">
  :global(.uplink) {
    text-decoration: none;
  }
</style>
