import Vue from 'vue';
import LicenseManagement from 'ee/vue_shared/license_management/license_management.vue';
import createStore from 'ee/vue_shared/license_management/store/index';
import ProtectedEnvironmentCreate from 'ee/protected_environments/protected_environment_create';
import ProtectedEnvironmentEditList from 'ee/protected_environments/protected_environment_edit_list';
import showToast from '~/vue_shared/plugins/global_toast';
import '~/pages/projects/settings/ci_cd/show/index';

document.addEventListener('DOMContentLoaded', () => {
  const el = document.getElementById('js-managed-licenses');
  const toasts = document.querySelectorAll('.js-toast-message');

  if (el && el.dataset && el.dataset.apiUrl) {
    const store = createStore();
    store.dispatch('licenseManagement/setIsAdmin', Boolean(el.dataset.apiUrl));
    // eslint-disable-next-line no-new
    new Vue({
      el,
      store,
      render(createElement) {
        return createElement(LicenseManagement, {
          props: {
            ...el.dataset,
          },
        });
      },
    });
  }

  toasts.forEach(toast => showToast(toast.dataset.message));

  // eslint-disable-next-line no-new
  new ProtectedEnvironmentCreate();

  // eslint-disable-next-line no-new
  new ProtectedEnvironmentEditList();
});
