// 环境配置
export const ENV_CONFIG = {
  dev: {
    name: '开发环境',
    serverUrl: 'http://127.0.0.1:9500',
  },
  prod: {
    name: '生产环境',
    serverUrl: 'https://websopy-api.websoft.top',
  },
} as const

export type EnvKey = keyof typeof ENV_CONFIG

const STORAGE_KEY = 'websopy_api_env'

// 客户端检测
function isClient(): boolean {
  return typeof window !== 'undefined'
}

// 从 localStorage 读取保存的环境，默认生产环境
export function getCurrentEnv(): EnvKey {
  if (!isClient()) return 'prod'
  const saved = localStorage.getItem(STORAGE_KEY)
  if (saved && saved in ENV_CONFIG) {
    return saved as EnvKey
  }
  return 'prod'
}

export function setCurrentEnv(env: EnvKey) {
  if (!isClient()) return
  localStorage.setItem(STORAGE_KEY, env)
}

export function getApiBaseUrl(): string {
  return ENV_CONFIG[getCurrentEnv()].serverUrl
}

// 动态 API URL（根据当前环境）
const BASE_URL = getApiBaseUrl()

export const SERVER_API_URL =  '/api/_server'
export const MODULES_API_URL = '/api/_modules'
// App 模块：相对路径，走 /api/_app proxy → websopy-api.websoft.top/api/app/*
export const APP_API_URL = '/api/app'
export const FILE_SERVER = '/api/_file'

// Some endpoints use this as a special TenantId override (defaults to current tenant)
export const TEMPLATE_ID = '5'
