import { constants } from '@cloudbase/utilities'

// @ts-ignore
const { setSdkName: setUtilitiesSdkName, setProtocol: setUtilitiesProtocol } = constants
/**
 * SDK
 */
let sdkVersion = ''
let sdkName = '@cloudbase/js-sdk'

export function setSdkVersion(version: string) {
  sdkVersion = version
}
export function getSdkVersion() {
  return sdkVersion
}
export function setSdkName(name: string) {
  sdkName = name
  setUtilitiesSdkName(name)
}
export function getSdkName() {
  return sdkName
}
export const DATA_VERSION = '2020-01-10'

export type EndPointKey = 'CLOUD_API' | 'GATEWAY'

interface EndPointInfo {
  env: string
  endPointKey: EndPointKey
  region?: string
  baseUrl?: string
  protocol?: string
}
/**
 *  所有 endPoint 信息
 *  避免直接操作该数组
 *  使用 setEndPointInfo、 getEndPointInfo
 */
const END_POINT_INFO_ARR: Array<EndPointInfo> = []

/** 用来查找 endPoint 的字段 */
const END_POINT_INFO_SEARCH_KEYS = ['env', 'endPointKey', 'region']

const DEFAULT_PROTOCOL = 'https:'

function findMatchedInfo(info: EndPointInfo) {
  return END_POINT_INFO_ARR.find(targetInfo => END_POINT_INFO_SEARCH_KEYS.filter(searchKey => info[searchKey] != null).every(searchKey => targetInfo[searchKey] === info[searchKey],),)
}

export function setEndPointInfo(newInfo: EndPointInfo) {
  const endPointInfo = findMatchedInfo(newInfo)
  if (endPointInfo) {
    if (newInfo.baseUrl != null) {
      endPointInfo.baseUrl = newInfo.baseUrl
    }
    if (newInfo.protocol != null) {
      endPointInfo.protocol = newInfo.protocol
    }
  } else {
    END_POINT_INFO_ARR.push({ ...newInfo, protocol: newInfo.protocol ?? DEFAULT_PROTOCOL })
  }

  // 保持旧代码逻辑
  if (newInfo.endPointKey === 'CLOUD_API') {
    setUtilitiesProtocol((newInfo.protocol ?? DEFAULT_PROTOCOL) as 'http' | 'https')
  }
}

export function getEndPointInfo(env: string, endPointKey: EndPointKey, region?: string) {
  return findMatchedInfo({ env, endPointKey, region })
}

export interface ISetEndPointWithKey {
  key: EndPointKey
  url?: string
  protocol?: 'http' | 'https'
}

export function setGatewayEndPointWithEnv(env: string, protocol?: 'http' | 'https') {
  setEndPointInfo({ endPointKey: 'GATEWAY', env, baseUrl: `//${env}.api.tcloudbasegateway.com/v1`, protocol })
}
export function setRegionLevelEndpoint(env: string, region: string, protocol?: 'http' | 'https') {
  const baseUrl = region
    ? `//${env}.${region}.tcb-api.tencentcloudapi.com/web`
    : `//${env}.ap-shanghai.tcb-api.tencentcloudapi.com/web`
  setEndPointInfo({ env, region, baseUrl, protocol, endPointKey: 'CLOUD_API' })
}

export function getBaseEndPoint(env: string) {
  const info = getEndPointInfo(env, 'CLOUD_API')

  const { protocol: PROTOCOL, baseUrl: BASE_URL } = info
  const webEndpoint = `${PROTOCOL}${BASE_URL}`
  // @todo 临时兼容小程序
  return webEndpoint.match(/(http(s)?:)?\/\/([^/?#]*)/)[0]
  // return `${new URL(webEndpoint).origin}`
}

export enum LOGINTYPE {
  NULL = 'NULL',
  ANONYMOUS = 'ANONYMOUS',
  WECHAT = 'WECHAT',
  WECHAT_PUBLIC = 'WECHAT-PUBLIC',
  WECHAT_OPEN = 'WECHAT-OPEN',
  CUSTOM = 'CUSTOM',
  EMAIL = 'EMAIL',
  USERNAME = 'USERNAME',
  PHONE = 'PHONE',
}

export const OAUTH2_LOGINTYPE_PREFIX = 'OAUTH2'
