/**
 * Axios 适配器
 * 
 * 基于 axios 库的适配器实现（支持浏览器和 Node.js 环境）
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "axiosAdapter", {
    enumerable: true,
    get: function() {
        return axiosAdapter;
    }
});
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
async function axiosAdapter() {
    // 动态导入 axios（按需加载）
    // 使用多种方式尝试导入，以兼容不同的构建工具
    let axios;
    let importError = null;
    // 方法1：使用 Function 构造函数（避免构建时解析）
    try {
        // @ts-ignore - axios 是可选依赖
        axios = await new Function('return import("axios")')();
    } catch (error) {
        importError = error;
        // 方法2：直接使用动态导入（某些构建工具可能需要这种方式）
        try {
            // @ts-ignore - axios 是可选依赖
            axios = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("axios")));
        } catch (error2) {
            importError = error2;
        }
    }
    // 如果两种方法都失败，检查是否是模块未找到错误
    if (!axios || !axios.default) {
        if (importError instanceof Error) {
            const errorMessage = importError.message.toLowerCase();
            // 检查是否是模块未找到相关的错误
            const isModuleNotFound = errorMessage.includes('cannot find module') || errorMessage.includes('cannot resolve') || errorMessage.includes('module not found') || errorMessage.includes('failed to resolve') || errorMessage.includes('does not exist') || errorMessage.includes('failed to fetch dynamically imported module') || errorMessage.includes('loading chunk') || errorMessage.includes('networkerror') || errorMessage.includes('axios') && (errorMessage.includes('resolve') || errorMessage.includes('find') || errorMessage.includes('not found'));
            if (isModuleNotFound) {
                // 模块未找到，抛出友好的依赖缺失错误
                const { createDependencyError } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("../utils/dependencyCheck")));
                // 检测当前环境
                const isBrowser = typeof window !== 'undefined';
                const environment = isBrowser ? 'browser' : 'node';
                const errorMessage = createDependencyError('axios', {
                    adapterName: 'axios',
                    environment
                });
                throw new Error(errorMessage);
            }
        }
        throw new Error('axios 库导入失败，请检查安装是否正确');
    }
    const axiosInstance = axios.default;
    return {
        name: 'axios',
        capabilities: {
            supportsAbort: true,
            supportsTimeout: true,
            supportsProgress: true,
            supportsInterceptors: true
        },
        async request (options) {
            const { url, method = 'GET', headers = {}, query, body, timeout, signal, credentials } = options;
            // 构建 axios 配置
            const axiosConfig = {
                url,
                method: method.toLowerCase(),
                headers: headers,
                params: query,
                data: body,
                timeout: timeout,
                signal: signal,
                withCredentials: credentials === 'include',
                validateStatus: ()=>true
            };
            try {
                // 执行请求
                // @ts-ignore - axios 类型定义
                const axiosResponse = await axiosInstance.request(axiosConfig);
                // 返回响应对象
                return {
                    data: axiosResponse.data,
                    status: axiosResponse.status,
                    statusText: axiosResponse.statusText,
                    headers: axiosResponse.headers,
                    raw: axiosResponse
                };
            } catch (error) {
                // axios 的错误处理
                if (error.response) {
                    // HTTP 错误
                    const status = error.response.status;
                    const statusText = error.response.statusText || '';
                    throw {
                        message: `HTTP ${status} ${statusText}`,
                        status,
                        statusText,
                        response: {
                            data: error.response.data,
                            status,
                            statusText,
                            headers: error.response.headers
                        },
                        request: options
                    };
                } else if (error.request) {
                    // 网络错误
                    throw {
                        message: error.message || '网络请求失败',
                        request: options
                    };
                } else {
                    // 其他错误
                    throw error;
                }
            }
        }
    };
}

//# sourceMappingURL=axiosAdapter.js.map