import { ref } from 'vue';
import api from '../api';

const allowed = ref<boolean | null>(null);
let loadPromise: Promise<void> | null = null;

export function useSmokeTestsAllowed() {
  async function ensureLoaded() {
    if (allowed.value !== null) return;
    if (!loadPromise) {
      loadPromise = (async () => {
        try {
          const { data } = await api.get('/admin/system/smoke-tests');
          allowed.value = !!data.data?.allowed;
        } catch {
          allowed.value = false;
        }
      })();
    }
    await loadPromise;
  }

  return { allowed, ensureLoaded };
}
