import axios from 'axios'
import { Output } from './interface-v2'

export class Base {
    protected userAgent = '127.0.0.1' // User-Agent
    constructor() {}

    /**
     * get 请求参数处理
     * @param object query 请求参数
     * @param exclude 需要排除的字段
     * @returns
     */
    protected objectToQueryString(object: Record<string, any>, exclude: string[] = []): string {
        let str = Object.keys(object)
            .filter((key) => !exclude.includes(key))
            .map((key) => {
                return encodeURIComponent(key) + '=' + encodeURIComponent(object[key])
            })
            .join('&')
        if (str) str = '?' + str
        return str || ''
    }
    /**
     * post 请求
     * @param url  请求接口
     * @param params 请求参数
     */
    protected async postRequest(
        url: string,
        params: Record<string, any>,
        authorization: string
    ): Promise<Record<string, any>> {
        try {
            const result = await axios.post(url, params, {
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
                    'User-Agent': this.userAgent,
                    Authorization: authorization,
                    'Accept-Encoding': 'gzip'
                }
            })
            return {
                status: result.status,
                ...result.data
            }
        } catch (error) {
            const err = JSON.parse(JSON.stringify(error))
            return {
                status: err.status,
                ...(err.response.text && JSON.parse(err.response.text))
            }
        }
    }
    /**
     * post 请求 V2
     * @param url  请求接口
     * @param params 请求参数
     */
    protected async postRequestV2(
        url: string,
        params: Record<string, any>,
        authorization: string,
        headers = {}
    ): Promise<Output> {
        try {
            const result = await axios.post(url, params, {
                headers: {
                    ...headers,
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
                    'User-Agent': this.userAgent,
                    Authorization: authorization,
                    'Accept-Encoding': 'gzip'
                }
            })

            return {
                status: result.status,
                data: result.data
            }
        } catch (error) {
            const err = JSON.parse(JSON.stringify(error))
            return {
                status: err.status as number,
                error: err.response.text && JSON.parse(err.response.text)
            }
        }
    }
    /**
     * get 请求
     * @param url  请求接口
     * @param query 请求参数
     */
    protected async getRequest(
        url: string,
        authorization: string,
        query: Record<string, any> = {}
    ): Promise<Record<string, any>> {
        try {
            const result = await axios.get(url, {
                params: query,
                headers: {
                    Accept: 'application/json',
                    'User-Agent': this.userAgent,
                    Authorization: authorization,
                    'Accept-Encoding': 'gzip'
                }
            })

            let data = {}
            switch (result.headers['Content-Type']) {
                case 'application/json':
                    data = {
                        status: result.status,
                        ...result.data
                    }
                    break
                case 'text/plain':
                    data = {
                        status: result.status,
                        data: result.data
                    }
                    break
                case 'application/x-gzip':
                    data = {
                        status: result.status,
                        data: result.data
                    }
                    break
                default:
                    data = {
                        status: result.status,
                        ...result.data
                    }
            }
            return data
        } catch (error) {
            const err = JSON.parse(JSON.stringify(error))
            return {
                status: err.status,
                ...(err.response.text && JSON.parse(err.response.text))
            }
        }
    }
    /**
     * get 请求 v2
     * @param url  请求接口
     * @param query 请求参数
     */
    protected async getRequestV2(url: string, authorization: string, query: Record<string, any> = {}): Promise<Output> {
        try {
            const result = await axios.get(url, {
                params: query,
                headers: {
                    Accept: 'application/json',
                    'User-Agent': this.userAgent,
                    Authorization: authorization,
                    'Accept-Encoding': 'gzip'
                }
            })

            let data: any = {}
            switch (result.headers['Content-Type']) {
                case 'application/json':
                    data = {
                        status: result.status,
                        data: result.data
                    }
                    break
                case 'text/plain':
                    data = {
                        status: result.status,
                        data: result.data
                    }
                    break
                case 'application/x-gzip':
                    data = {
                        status: result.status,
                        data: result.data
                    }
                    break
                default:
                    data = {
                        status: result.status,
                        data: result.data
                    }
            }
            return data
        } catch (error) {
            const err = JSON.parse(JSON.stringify(error))
            return {
                status: err.status,
                error: err.response.text && JSON.parse(err.response.text)
            }
        }
    }
}
