<script>
import { mapActions } from 'vuex';
import { GlButton } from '@gitlab/ui';
import GroupItemName from '../group_item_name.vue';
import { I18N_ADD_SUBSCRIPTIONS_ERROR_MESSAGE } from '../../constants';

export default {
  components: {
    GlButton,
    GroupItemName,
  },
  inject: {
    subscriptionsPath: {
      default: '',
    },
  },
  props: {
    group: {
      type: Object,
      required: true,
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      isLoading: false,
    };
  },
  methods: {
    ...mapActions(['addSubscription']),
    async onClick() {
      this.isLoading = true;
      try {
        await this.addSubscription({
          namespacePath: this.group.full_path,
          subscriptionsPath: this.subscriptionsPath,
        });
      } catch (error) {
        this.$emit('error', error?.response?.data?.error || I18N_ADD_SUBSCRIPTIONS_ERROR_MESSAGE);
      }
      this.isLoading = false;
    },
  },
};
</script>

<template>
  <li class="gl-border-b-1 gl-border-b-solid gl-border-b-gray-100">
    <div class="gl-display-flex gl-align-items-center gl-py-3">
      <div class="gl-min-w-0 gl-display-flex gl-flex-grow-1 gl-flex-shrink-1 gl-align-items-center">
        <div class="gl-min-w-0 gl-flex-grow-1 flex-shrink-1">
          <group-item-name :group="group" />
        </div>

        <gl-button
          category="secondary"
          variant="confirm"
          :loading="isLoading"
          :disabled="disabled"
          @click.prevent="onClick"
        >
          {{ __('Link') }}
        </gl-button>
      </div>
    </div>
  </li>
</template>
