import axios from 'axios';
import { useAuthStore } from '../store/auth';

const http = axios.create({
  baseURL: '/api',
  timeout: 15000,
});

http.interceptors.request.use((config) => {
  const auth = useAuthStore();
  if (auth.token) {
    config.headers = config.headers || {};
    config.headers.Authorization = `Bearer ${auth.token}`;
  }
  return config;
});

http.interceptors.response.use(
  (resp) => resp.data,
  (error) => Promise.reject(error),
);

export default http;
