<template><div /></template>

<script setup lang="ts">
import { useQuery } from '@vue/apollo-composable';
import gql from 'graphql-tag';
import { useRouter } from 'vue-router';
const router = useRouter();
const props = defineProps<{
  id: string;
}>();

const decoded = window.atob(props.id);

let query;

if (decoded.startsWith('ChapterNode')) {
  query = gql`
    query ChapterQuery($id: ID!) {
      chapter(id: $id) {
        path
      }
    }
  `;
} else {
  query = gql`
    query ContentBlockQuery($id: ID!) {
      contentBlock(id: $id) {
        path
      }
    }
  `;
}

const { onResult } = useQuery(query, () => ({ id: props.id }));

onResult(({ data }) => {
  let path;
  if (data.chapter) {
    path = data.chapter.path;
  } else if (data.contentBlock) {
    path = data.contentBlock.path;
  } else {
    router.push({ name: 'not-found' });
    return;
  }
  router.replace(`/${path}`);
});
</script>
