<script>
import { GlAlert } from '@gitlab/ui';
import { n__ } from '~/locale';
import PackagesSettings from '~/packages_and_registries/settings/group/components/packages_settings.vue';
import DependencyProxySettings from '~/packages_and_registries/settings/group/components/dependency_proxy_settings.vue';

import getGroupPackagesSettingsQuery from '~/packages_and_registries/settings/group/graphql/queries/get_group_packages_settings.query.graphql';

export default {
  name: 'GroupSettingsApp',
  components: {
    GlAlert,
    PackagesSettings,
    DependencyProxySettings,
  },
  inject: ['groupPath', 'dependencyProxyAvailable'],
  apollo: {
    group: {
      query: getGroupPackagesSettingsQuery,
      variables() {
        return {
          fullPath: this.groupPath,
        };
      },
    },
  },
  data() {
    return {
      group: {},
      alertMessage: null,
    };
  },
  computed: {
    packageSettings() {
      return this.group?.packageSettings || {};
    },
    dependencyProxySettings() {
      return this.group?.dependencyProxySetting || {};
    },
    isLoading() {
      return this.$apollo.queries.group.loading;
    },
  },
  methods: {
    dismissAlert() {
      this.alertMessage = null;
    },
    handleSuccess(amount = 1) {
      const successMessage = n__(
        'Setting saved successfully',
        'Settings saved successfully',
        amount,
      );
      this.$toast.show(successMessage);
      this.dismissAlert();
    },
    handleError(amount = 1) {
      const errorMessage = n__(
        'An error occurred while saving the setting',
        'An error occurred while saving the settings',
        amount,
      );
      this.alertMessage = errorMessage;
    },
  },
};
</script>

<template>
  <div>
    <gl-alert v-if="alertMessage" variant="warning" class="gl-mt-4" @dismiss="dismissAlert">
      {{ alertMessage }}
    </gl-alert>

    <packages-settings
      :package-settings="packageSettings"
      :is-loading="isLoading"
      @success="handleSuccess(2)"
      @error="handleError(2)"
    />

    <dependency-proxy-settings
      v-if="dependencyProxyAvailable"
      :dependency-proxy-settings="dependencyProxySettings"
      :is-loading="isLoading"
      @success="handleSuccess"
      @error="handleError"
    />
  </div>
</template>
