import { errorResponse } from "@/core/utils/errorResponse";
import { successRequest } from "@/core/utils/successRequest";
import axios from "axios";
import { useAuth } from "../contexts/auth";

const defaultOptions = {
    data: {},
    requestOptions: {
        headers: {},
    },
    notification: {
        show: true,
        success: false,
        failed: true,
    },
};

const useRequest = (initOptions) => {
    const { logout } = useAuth()

    const _options = Object.assign({}, defaultOptions, initOptions);

    return async (url = "", method = "get", options) => {
        const mergedOptions = Object.assign({}, _options, options);

        try {
            const response = await axios({
                url,
                method,
                data: method === "get" ? null : mergedOptions.data,
                withCredentials: true,
                ...mergedOptions.requestOptions,
            });
            successRequest(mergedOptions.notification.show && mergedOptions.notification.success, "request_data");
            return response;
        } catch (error) {
            if (error.response) {
                errorResponse(
                    error.response,
                    mergedOptions.notification.show && mergedOptions.notification.failed,
                    "request_data", logout
                );
            }
            throw error;
        }
    };
};

export default useRequest;
