import { API_URL } from '../constants/defaults';

let _token = null;
let _company = null;

export const setApiAuth = (token, company) => {
  _token = token;
  _company = company;
};

export const clearApiAuth = () => {
  _token = null;
  _company = null;
};

export const getApiAuth = () => ({ token: _token, company: _company });

export const apiFetch = (url, options = {}) => {
  const headers = {
    'Content-Type': 'application/json',
    'X-Requested-With': 'AfrihairApp',
    ...(options.headers || {}),
  };
  if (_token) headers['X-Auth-Token'] = _token;
  if (_company) headers['X-Company'] = _company;
  return fetch(url, { ...options, headers });
};

export { API_URL };
