"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.apiCall = void 0;
const logger_1 = require("../../logger");
const apiCall = async (options = {}, apiUrl) => {
    const { method = 'GET', params, headers = {} } = options;
    const fetchOptions = {
        method,
        headers: {
            'Content-Type': 'application/json',
            ...headers,
        },
    };
    // Handle params based on method
    if (method === 'GET' && params) {
        const queryString = new URLSearchParams(params).toString();
        apiUrl += `?${queryString}`;
    }
    else if (params) {
        fetchOptions.body = JSON.stringify(params);
    }
    // logger.info(`URL: ${apiUrl}`);
    try {
        const response = await fetch(apiUrl, fetchOptions);
        const data = await response.json();
        // logger.info(`URL: ${JSON.stringify(data)}`);
        if (!response.ok) {
            return { error: data?.message || 'An error occurred', status: response.status };
        }
        return data;
    }
    catch (error) {
        logger_1.logger.info(`Error: ${JSON.stringify(error)}`);
        return { error: error.message || 'Network error' };
    }
};
exports.apiCall = apiCall;
//# sourceMappingURL=index.js.map