import Taro from '@tarojs/taro'
import { SHARE_CHANNEL } from '../constant'
// Determine if the environment is `weapp` and get the `envVersion`
const isWeApp = process.env.TARO_ENV === 'weapp'
const isPreRelease = process.env.TARO_APP_PRE_RELEASE === 'true'
const accountInfo = isWeApp ? Taro.getAccountInfoSync() : null
const envVersion = isWeApp
  ? accountInfo?.miniProgram.envVersion || 'release'
  : null

// Helper function to determine base URL based on environment
const getBaseUrl = (releaseUrl: string, preUrl: string) => {
  if(isPreRelease){
    return releaseUrl
  }
  if (isWeApp) {
    return envVersion === 'release' ? releaseUrl : preUrl
  }
  return process.env.XLB_ENV !== 'pre_env' ? releaseUrl : preUrl
}

// Define base URLs
export const bluebookAiAgent = getBaseUrl(
  'https://api.xiaolanben.com/blue-aiagent/',
  'https://api.xiaolanben.com/blue-aiagent-pre/',
)

export const bluebookBaseConst = getBaseUrl(
  'https://api.xiaolanben.com/bluebook/',
  'https://api.xiaolanben.com/bluebook-pre/',
)

export const blueServiceBaseConst = getBaseUrl(
  'https://api.xiaolanben.com/blue-service/',
  'https://api.xiaolanben.com/blue-service-pre/',
)

export const blueUserBaseConst = getBaseUrl(
  'https://api.xiaolanben.com/blue-user/',
  'https://api.xiaolanben.com/blue-user-pre/',
)

export const blueConnBaseConst = getBaseUrl(
  'https://api.xiaolanben.com/blue-conn/',
  'https://api.xiaolanben.com/blue-conn-pre/',
)

export const xlbGatewayBaseConst = getBaseUrl(
  'https://api.xiaolanben.com/',
  'https://blue-book-pre.u51.com/',
)

export const bluebookGatewayBaseConst = getBaseUrl(
  'https://api.xiaolanben.com/xlb-gateway/blue-book/',
  'https://api.xiaolanben.com/blue-book-idc/blue-book/',
)

export const blueServicePrefix = isWeApp
  ? 'https://api.xiaolanben.com/'
  : '/api.xiaolanben.com/'

export const usercenterbase = 'https://api.u51.com/'

export function getUserCenterParams() {
  return {
    big_app_id: 35,
    app_id: 2035,
    plat: 4,
    channel: SHARE_CHANNEL,
    invitationCode: undefined,
  }
}
