import ME_QUERY from '@/graphql/gql/queries/meQuery.gql';

export default {
  data() {
    return {
      me: {
        selectedClass: {
          id: '',
        },
        permissions: [],
        schoolClasses: [],
        isTeacher: false,
        team: null
      },
      showPopover: false,
    };
  },

  computed: {
    topicRoute() {
      if (this.me.lastTopic && this.me.lastTopic.slug) {
        return {
          name: 'topic',
          params: {
            topicSlug: this.me.lastTopic.slug,
          },
        };
      }
      return '/book/topic/berufliche-grundbildung';
    },
    schoolClass() {
      return this.me.selectedClass;
    },
    canManageContent() {
      return this.me.isTeacher;
    },
    isReadOnly() {
      return this.me.readOnly || this.me.selectedClass.readOnly;
    },
    currentClassName() {
      let currentClass = this.me.schoolClasses.find(schoolClass => {
        return schoolClass.id === this.me.selectedClass.id;
      });
      return currentClass
        ? currentClass.name
        : (this.me.schoolClasses.length ? this.me.schoolClasses[0].name : '');
    },
  },

  apollo: {
    me: {
      query: ME_QUERY,
      update(data) {
        return this.$getRidOfEdges(data).me;
      },
      fetchPolicy: 'cache-first'
    },
  },
};
