<script>
import { mapActions, mapState } from 'vuex';
import { GlTooltipDirective, GlButton } from '@gitlab/ui';

export default {
  name: 'SecurityDashboardActionButtons',
  components: {
    GlButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    vulnerability: {
      type: Object,
      required: true,
    },
    canCreateIssue: {
      type: Boolean,
      required: false,
      default: false,
    },
    canDismissVulnerability: {
      type: Boolean,
      required: false,
      default: false,
    },
    isDismissed: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  computed: {
    ...mapState('vulnerabilities', ['isCreatingIssue', 'isDismissingVulnerability']),
  },
  methods: {
    ...mapActions('vulnerabilities', [
      'openModal',
      'createIssue',
      'dismissVulnerability',
      'undoDismiss',
    ]),
    handleCreateIssue() {
      const { vulnerability } = this;
      this.createIssue({ vulnerability, flashError: true });
    },
    handleDismissVulnerability() {
      const { vulnerability } = this;
      this.dismissVulnerability({ vulnerability, flashError: true });
    },
    handleUndoDismiss() {
      const { vulnerability } = this;
      this.undoDismiss({ vulnerability, flashError: true });
    },
  },
};
</script>

<template>
  <div class="btn-group">
    <gl-button
      key="more-info"
      v-gl-tooltip
      :aria-label="s__('SecurityReports|More info')"
      :title="s__('SecurityReports|More info')"
      class="js-more-info"
      variant="info"
      category="secondary"
      icon="information-o"
      @click="openModal({ vulnerability })"
    />
    <gl-button
      v-if="canCreateIssue"
      key="create-issue"
      v-gl-tooltip
      :aria-label="s__('SecurityReports|Create issue')"
      :loading="isCreatingIssue"
      :title="s__('SecurityReports|Create issue')"
      class="js-create-issue"
      variant="success"
      category="secondary"
      icon="issue-new"
      @click="handleCreateIssue"
    />
    <template v-if="canDismissVulnerability">
      <gl-button
        v-if="isDismissed"
        key="undo-dismiss"
        v-gl-tooltip
        :aria-label="s__('SecurityReports|Undo dismiss')"
        :loading="isDismissingVulnerability"
        :title="s__('SecurityReports|Undo dismiss')"
        class="js-undo-dismiss"
        variant="warning"
        category="secondary"
        icon="redo"
        @click="handleUndoDismiss"
      />
      <gl-button
        v-else
        key="dismiss-vulnerability"
        v-gl-tooltip
        :aria-label="s__('SecurityReports|Dismiss vulnerability')"
        :loading="isDismissingVulnerability"
        :title="s__('SecurityReports|Dismiss vulnerability')"
        class="js-dismiss-vulnerability"
        variant="warning"
        category="secondary"
        icon="cancel"
        @click="handleDismissVulnerability"
      />
    </template>
  </div>
</template>
