import { ref } from 'vue';
import { request } from 'graphql-request';
import {GET_COMPANY_INFO} from "@/graphql/queries/company.js";
import { setCompanyInfo } from '@/stores/company.js';
import {APP_API_DOMAIN} from "@/config/index.js";

export function useCompanyInfo() {
  const company = ref(null);
  const loading = ref(false);
  const error = ref(null);

  const getCompanyInfo = async () => {
    loading.value = true;
    error.value = null;

    try {
      const data = await request(APP_API_DOMAIN, GET_COMPANY_INFO);
      company.value = data.parameters;
      setCompanyInfo(data.parameters);
    } catch (err) {
      error.value = err;
      console.error('useCompanyInfo error:', err);
    } finally {
      loading.value = false;
    }
  };

  return {
    company,
    loading,
    error,
    getCompanyInfo
  };
}