<script setup lang="ts">
import { hasPermissions } from '~/shared/auth.util';

const { path } = defineProps<{
    path: string
    filter?: string,
    popover?: boolean
}>();
const { user } = useUserSession();
const { content, get } = useContent();
const overview = computed(() => content.value.find(e => e.path === path));
const isOwner = computed(() => user.value?.id === overview.value?.owner);

const loading = ref(false);
if(overview.value && !overview.value.content)
{
    loading.value = true;
    await get(path);
    loading.value = false;
}
</script>

<template>
    <div class="flex flex-1 justify-start items-start flex-col lg:px-16 xl:px-32 2xl:px-64 py-6">
        <Loading v-if="loading" />
        <template v-else-if="overview">
            <div v-if="!popover" class="flex flex-1 flex-row justify-between items-center">
                <ProseH1>{{ overview.title }}</ProseH1>
                <div class="flex gap-4">
                    <NuxtLink :href="{ name: 'explore-edit', hash: '#' + overview.path }" v-if="isOwner || hasPermissions(user?.permissions ?? [], ['admin', 'editor'])"><Button>Modifier</Button></NuxtLink>
                </div>
            </div>
            <MarkdownRenderer v-if="overview.content" :content="overview.content" :filter="filter" />
        </template>
        <template v-else>
            <div><ProseH2>Impossible d'afficher le contenu demandé</ProseH2></div>
        </template>
    </div>
</template>