<script>
import { s__ } from '~/locale';
import { GlButton, GlNewDropdown, GlDeprecatedDropdownItem } from '@gitlab/ui';

export default {
  i18n: {
    FUZZING_ARTIFACTS: s__('SecurityReports|Fuzzing artifacts'),
  },
  components: {
    GlButton,
    GlNewDropdown,
    GlDeprecatedDropdownItem,
  },
  props: {
    jobs: {
      type: Array,
      required: true,
    },
    projectId: {
      type: Number,
      required: true,
    },
  },
  computed: {
    hasDropdown() {
      return this.jobs.length > 1;
    },
  },
  methods: {
    artifactDownloadUrl(job) {
      return `/api/v4/projects/${this.projectId}/jobs/artifacts/${
        job.ref
      }/download?job=${encodeURIComponent(job.name)}`;
    },
  },
};
</script>

<template>
  <div>
    <strong>{{ s__('SecurityReports|Download Report') }}</strong>
    <gl-new-dropdown
      v-if="hasDropdown"
      class="d-block mt-1"
      :text="$options.i18n.FUZZING_ARTIFACTS"
      category="secondary"
      variant="info"
      size="small"
    >
      <gl-deprecated-dropdown-item
        v-for="job in jobs"
        :key="job.id"
        :href="artifactDownloadUrl(job)"
        >{{ job.name }}</gl-deprecated-dropdown-item
      >
    </gl-new-dropdown>
    <gl-button
      v-else
      class="d-block mt-1"
      category="secondary"
      variant="info"
      size="small"
      :href="artifactDownloadUrl(jobs[0])"
    >
      {{ $options.i18n.FUZZING_ARTIFACTS }}
    </gl-button>
  </div>
</template>
