<script lang="ts">
  import {onMount, mount, unmount} from "svelte"
  import Drawer from "@lib/components/Drawer.svelte"
  import Dialog from "@lib/components/Dialog.svelte"
  import {modal, modalStack, popModal} from "@app/util/modal"

  const closeModal = () => {
    if ($modal && !$modal.options.noEscape) {
      popModal()
    }
  }

  const onKeyDown = (e: any) => {
    if (e.code === "Escape" && e.target === document.body) {
      closeModal()
    }
  }

  let element: HTMLElement
  const instances: Record<string, any> = {}

  onMount(() => {
    return modalStack.subscribe($modalStack => {
      const ids = $modalStack.map(({id}) => id)

      for (const [id, instance] of Object.entries(instances)) {
        if (!ids.includes(id)) {
          unmount(instance, {outro: true})
          delete instances[id]
        }
      }

      for (const item of $modalStack) {
        if (instances[item.id]) {
          continue
        }

        const {options, component, props} = item
        const wrapper = options.drawer ? Drawer : Dialog

        instances[item.id] = mount(wrapper as any, {
          target: element,
          props: {
            onClose: closeModal,
            noEscape: options.noEscape,
            fullscreen: options.fullscreen,
            children: {component, props},
          },
        })
      }
    })
  })
</script>

<svelte:window onkeydown={onKeyDown} />

<div bind:this={element}></div>
