<template>
  <Group />
</template>

<script>
import { withFetch } from 'inside-collection/hocs'
import { http } from '@inside-library/helpers/Http'

import Container from './GroupContainer'

const fetchData = async ({ $route }) => {
  try {
    const { data } = await http.get('content/spaces', {
      fields: [
        'uuid',
        'title',
        'introduction',
        'admin',
        'image',
        'content_type',
        'content',
        'has_documents',
        'slug',
        { spaces_referents: ['uuid', 'firstname', 'lastname', 'image'] },
        {
          users: {
            fields: ['uuid', 'firstname', 'lastname', 'image'],
            filters: { reverse: true, 'status:eq': 1 }
          }
        },
        {
          news: {
            fields: [
              'uuid',
              'title',
              'image',
              'slug',
              'published_at',
              'content_type',
              'admin'
            ],
            filters: {
              reverse: true,
              limit: 3,
              sort: 'published_at:desc',
              'status:eq': 1
            }
          }
        },
        {
          tools: {
            fields: [
              'uuid',
              'title',
              'tools_link',
              'image',
              'class',
              'content_type',
              'admin',
              'tools_link'
            ],
            filters: { 'status:eq': 1, sort: 'weight:asc' }
          }
        },
        {
          events: {
            fields: [
              'uuid',
              'title',
              'slug',
              'content_type',
              'admin',
              'date',
              'end_date',
              'image',
              'place',
              {
                events_categories: {
                  fields: ['uuid', 'color', 'content_type'],
                  filters: { 'status:eq': 1 }
                }
              }
            ],
            filters: {
              'status:eq': 1,
              'date:gte': 'now()',
              limit: 3,
              sort: 'date:desc',
              reverse: true
            }
          }
        },
        {
          folders: {
            fields: [
              'uuid',
              {
                folders: {
                  fields: ['uuid', 'title', 'content_type', 'admin'],
                  filters: { 'status:eq': 1, reverse: true }
                }
              }
            ],
            filters: { 'status:eq': 1 }
          }
        }
      ],
      filters: { 'status:eq': 1, 'slug:eq': $route.params.slug }
    })

    return data[0]
  } catch (err) {
    throw new Error(err)
  }
}

export default {
  name: 'GroupPage',
  components: { Group: withFetch(Container, { size: 'xxl' })(fetchData) }
}
</script>
