"use strict";
/**
 * useApi Hook
 * Provides a typed API client with error handling and request/response interceptors
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useApi = void 0;
const react_1 = require("react");
const axios_1 = __importDefault(require("axios"));
const useAuth_1 = require("./useAuth");
const useApi = () => {
    const { getToken, logout } = (0, useAuth_1.useAuth)();
    // Create axios instance with default config
    const api = axios_1.default.create({
        baseURL: process.env.REACT_APP_API_BASE_URL || '/api',
        timeout: 30000,
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/json',
        },
    });
    // Request interceptor
    api.interceptors.request.use(async (config) => {
        // Add auth token if not skipped
        if (!config.skipAuth) {
            const token = await getToken();
            if (token) {
                config.headers = {
                    ...config.headers,
                    Authorization: `Bearer ${token}`,
                };
            }
        }
        return config;
    }, (error) => {
        return Promise.reject(error);
    });
    // Response interceptor
    api.interceptors.response.use((response) => {
        return response;
    }, async (error) => {
        // Handle auth errors
        if (error.response?.status === 401) {
            await logout();
            window.location.href = '/login';
            return Promise.reject(new Error('Authentication required'));
        }
        // Handle other errors
        if (!error.config?.skipErrorHandling) {
            const errorMessage = error.response?.data?.message || error.message;
            const apiError = new Error(errorMessage);
            apiError.code = error.response?.data?.errors?.[0]?.code;
            apiError.status = error.response?.status;
            apiError.response = error.response?.data;
            return Promise.reject(apiError);
        }
        return Promise.reject(error);
    });
    // Typed request methods
    const get = (0, react_1.useCallback)(async (url, config) => {
        return api.get(url, config);
    }, [api]);
    const post = (0, react_1.useCallback)(async (url, data, config) => {
        return api.post(url, data, config);
    }, [api]);
    const put = (0, react_1.useCallback)(async (url, data, config) => {
        return api.put(url, data, config);
    }, [api]);
    const patch = (0, react_1.useCallback)(async (url, data, config) => {
        return api.patch(url, data, config);
    }, [api]);
    const del = (0, react_1.useCallback)(async (url, config) => {
        return api.delete(url, config);
    }, [api]);
    return {
        get,
        post,
        put,
        patch,
        delete: del,
    };
};
exports.useApi = useApi;
exports.default = exports.useApi;
//# sourceMappingURL=useApi.js.map