<script>
import vulnerabilitySeveritiesCountQuery from '../graphql/vulnerability_severities_count.graphql';
import VulnerabilityCountListLayout from './vulnerability_count_list_layout.vue';

import { vulnerabilitiesSeverityCountScopes } from '../constants';

export default {
  components: {
    VulnerabilityCountListLayout,
  },
  props: {
    scope: {
      type: String,
      required: true,
      validator: (value) => Object.values(vulnerabilitiesSeverityCountScopes).includes(value),
    },
    fullPath: {
      type: String,
      required: false,
      default: '',
    },
    filters: {
      type: Object,
      required: false,
      default: () => ({}),
    },
  },
  data: () => ({
    queryError: false,
    vulnerabilitiesCount: {},
  }),
  computed: {
    isLoading() {
      return this.$apollo.queries.vulnerabilitiesCount.loading;
    },
  },
  apollo: {
    vulnerabilitiesCount: {
      query: vulnerabilitySeveritiesCountQuery,
      variables() {
        const { scope, fullPath } = this;
        const { instance, group, project } = vulnerabilitiesSeverityCountScopes;
        return {
          fullPath,
          isInstance: scope === instance,
          isGroup: scope === group,
          isProject: scope === project,
          ...this.filters,
        };
      },
      update(data) {
        return data[this.scope]?.vulnerabilitySeveritiesCount || {};
      },
      result() {
        this.queryError = false;
      },
      error() {
        this.queryError = true;
      },
    },
  },
};
</script>

<template>
  <vulnerability-count-list-layout
    :show-error="queryError"
    :is-loading="isLoading"
    :vulnerabilities-count="vulnerabilitiesCount"
  />
</template>
