import type { InitHttpCallback } from './init-http'
import type { CramiHttpOptions } from './typing'

export interface HttpObjType extends Partial<InitHttpCallback>{
  __isMerging?: boolean
  [key: string]: any
}
// 去除vue中的白名单
const ALLOW_WHITE_LIST = [
  '__isVue',
  '__v_isRef',
  '__v_isReadonly',
  '__v_isReactive',
]

const ERROR_LIST = [
  'request',
  'get',
  'post',
  'put',
  'delete',
  'option',
]

const httpObj: HttpObjType = {
  __isMerging: false,
}
export const Http: InitHttpCallback = new Proxy(
  httpObj, {
    get: (target, key) => {
      const isExist = Reflect.has(target, key)
      // 如果我们想要的属性不存在,就给他报错
      if (!isExist && !target.__isMerging && !ALLOW_WHITE_LIST.includes(String(key))) {
        if (ERROR_LIST.includes(String(key)))
          console.error(`Http.${String(key)} is not exist,maybe you forgot to import {createHttp} from "@crami/http" to init it`)

        return undefined
      }
      return Reflect.get(target, key)
    },
    set(target, key, value) {
      return Reflect.set(target, key, value)
    },
  },
) as InitHttpCallback

interface AppOptions{
  hooks: Omit<CramiHttpOptions, 'headers'>
}

export const app: AppOptions = {
  hooks: {},
}

export enum ContentTypeEnum {
  // json
  JSON = 'application/json;charset=UTF-8',
  // form-data qs
  FORM = 'application/x-www-form-urlencoded;charset=UTF-8',
  // form-data  upload
  UPLOAD = 'multipart/form-data;charset=UTF-8',
}