import { stringify } from 'qs';

import { RequestConfig } from '@/utils/request/types';

import { authHeaderInterceptor } from './authHeaderInterceptor';
import {
    requestEncryptionInterceptor,
    responseDecryptionInterceptor,
} from './encryptionInterceptors';
import { errorConfig } from './requestErrorConfig';

const config: RequestConfig = {
    baseURL: import.meta.env.VITE_API_BASE_URL!,
    // 请求超时时间
    timeout: 15000,
    headers: {
        Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
        'X-Requested-With': 'XMLHttpRequest',
        'X-NL-Product-Code': import.meta.env.VITE_API_PRODUCT_CODE!, // 每次请求必须包含产品代码
        'X-NL-Request-Type': 'web', // 请求类型，用于区分请求来源
    },
    paramsSerializer: (params) => stringify(params),
    ...errorConfig,
    requestInterceptors: [authHeaderInterceptor, requestEncryptionInterceptor],
    responseInterceptors: [responseDecryptionInterceptor],
};

export default config;
