<script lang="ts">
  import {onMount} from "svelte"
  import {dissoc, maybe} from "@welshman/lib"
  import {preventDefault} from "@lib/html"
  import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
  import AltArrowRight from "@assets/icons/alt-arrow-right.svg?dataurl"
  import Icon from "@lib/components/Icon.svelte"
  import Button from "@lib/components/Button.svelte"
  import Spinner from "@lib/components/Spinner.svelte"
  import Modal from "@lib/components/Modal.svelte"
  import ModalBody from "@lib/components/ModalBody.svelte"
  import ModalFooter from "@lib/components/ModalFooter.svelte"
  import RelaySummary from "@app/components/RelaySummary.svelte"
  import SpaceAccessRequest from "@app/components/SpaceAccessRequest.svelte"
  import SpaceJoinSettings from "@app/components/SpaceJoinSettings.svelte"
  import {attemptRelayAccess} from "@app/relays"
  import {addSpace} from "@app/groups"
  import {broadcastUserData} from "@app/profiles"
  import {setSpaceNotifications} from "@app/settings"
  import {relaysMostlyRestricted} from "@app/policies"
  import {notificationSettings} from "@app/settings"
  import {pushModal} from "@app/modal"
  import {pushToast} from "@app/toast"
  import {goToSpace} from "@app/routes"
  import {Push} from "@app/push"

  type Props = {
    url: string
  }

  const {url}: Props = $props()

  const back = () => history.back()

  const join = async () => {
    if (error) {
      return pushModal(SpaceAccessRequest, {url, callback: back})
    }

    loading = true

    try {
      if (notifications) {
        if (!notificationSettings.get().push) {
          await setSpaceNotifications(url, true)
        } else {
          const permissions = await Push.request()

          if (permissions.startsWith("granted")) {
            await setSpaceNotifications(url, true)
          }
        }
      } else {
        await setSpaceNotifications(url, false)
      }

      await addSpace(url)
      await goToSpace(url)

      broadcastUserData([url])
      relaysMostlyRestricted.update(dissoc(url))
      pushToast({message: "Welcome to the space!"})
    } catch (e) {
      console.error("Failed to join space:", e)
      pushToast({theme: "error", message: "Failed to join space. Please try again."})
    } finally {
      loading = false
    }
  }

  let error = $state(maybe<string>())
  let loading = $state(true)
  let notifications = $state(true)

  onMount(async () => {
    error = await attemptRelayAccess(url)
    loading = false
  })
</script>

<Modal tag="form" onsubmit={preventDefault(join)}>
  <ModalBody>
    <RelaySummary {url} />
    <SpaceJoinSettings {url} bind:error bind:notifications />
  </ModalBody>
  <ModalFooter>
    <Button class="btn btn-link" onclick={back} disabled={loading}>
      <Icon icon={AltArrowLeft} />
      Go back
    </Button>
    <Button type="submit" class="btn btn-primary" disabled={loading}>
      <Spinner {loading}>
        {error ? "Request Access" : "Join Space"}
      </Spinner>
      <Icon icon={AltArrowRight} />
    </Button>
  </ModalFooter>
</Modal>
