<script lang="ts" context="module">
  const contextKey = {ctx: "modalContext"};

  export type ModalData =
    | { name: "closed" }
    | { name: "scope.create" }
    | { name: "scope.edit", scope: Scope }
    | { name: "scope.delete", scope: Scope }
    | { name: "item.create", requirement?: RequirementWithoutItems }
    | { name: "item.edit", item: Item }
    | { name: "item.acquire", item: Item }
    | { name: "item.delete", item: Item }
    | { name: "requirement.create", project: ProjectEntry }
    | { name: "requirement.edit", requirement: RequirementWithoutItems }
    | { name: "requirement.delete", requirement: RequirementWithoutItems }
    | { name: "project.create" }
    | { name: "project.edit", project: Project }
    | { name: "project.delete", project: Project }
    | { name: "stat.create" }
    | { name: "stat.edit", stat: Stat }
    | { name: "stat.delete", stat: Stat }
    | { name: "sprint.create" }
    | { name: "sprint.edit", sprint: Sprint }
    | { name: "sprint.delete", sprint: Sprint }
    | { name: "requirement.jump" }
    | { name: "" }

  interface ModalContextData {
    currentModal: Readable<ModalData>
    openModal(data: ModalData): void
    closeModal(): void
  };

  export function getModalContext() {
    return getContext(contextKey) as ModalContextData;
  }
</script>

<script lang="ts">
  import { writable, type Readable } from "svelte/store";
  import { getContext, onMount, setContext } from "svelte";
  import type { ProjectEntry, Requirement, RequirementWithoutItems } from "$lib/models/project";
  import type Item from "$lib/models/item";
  import type Project from "$lib/models/project";
  import type Stat from "$lib/models/stat";
  import type Sprint from "$lib/models/sprint";
  import type Scope from "$lib/models/scope";

  let store = writable<ModalData>({name: "closed"});

  setContext<ModalContextData>(contextKey, {
    currentModal: {subscribe: store.subscribe},
    openModal: (data: ModalData) => store.set(data),
    closeModal: () => store.set({name: "closed"}),
  });
</script>

<slot></slot>