<!--
// Copyright © 2023 Hardcore Engineering Inc.
// 
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
  import { onDestroy } from 'svelte'
  import { locationToUrl, navigate, location, getCurrentLocation } from '../location'
  import { Location } from '../types'

  export let app: string | undefined = undefined
  export let space: string | undefined = undefined
  export let special: string | undefined = undefined

  let loc = createLocation(getCurrentLocation(), app, space, special)

  onDestroy(
    location.subscribe(async (res) => {
      loc = createLocation(res, app, space, special)
    })
  )

  $: href = locationToUrl(loc)

  function createLocation (
    loc: Location,
    app: string | undefined,
    space: string | undefined,
    special: string | undefined
  ): Location {
    const location: Location = {
      path: [...loc.path]
    }
    if (app !== undefined) {
      location.path[2] = app
      location.path.length = 3
    }
    if (space !== undefined) {
      location.path[3] = space
      location.path.length = 4
    }
    if (special !== undefined) {
      location.path[4] = special
      location.path.length = 5
    }
    return location
  }

  function clickHandler (e: MouseEvent) {
    if (e.metaKey || e.ctrlKey) return
    e.preventDefault()
    navigate(loc)
  }
</script>

<a {href} on:click={clickHandler}>
  <slot />
</a>
