import { getConfigKey } from '@/api/infra/config'
import { getAccessToken } from '@/utils/auth'
import { isHttps } from '@/views/infra/bookmark/logic'

/**
 * 获取文件预览地址
 * @param fileId
 * @param wopi_client
 * @param wopi_server
 */
export async function initFilePreviewUrl(fileId: string | number, wopi_client?: string | undefined, wopi_server?: string | undefined): Promise<string> {
  if (!wopi_client || !wopi_server) {
    if (isHttps()) {
      wopi_client = await getConfigKey('wopi_client_addr')
      wopi_server = await getConfigKey('wopi_server_ip_addr')
    }
    else {
      wopi_client = await getConfigKey('wopi_client_addr_http')
      wopi_server = await getConfigKey('wopi_server_ip_addr_http')
    }
  }
  const wopi = `${wopi_server}/admin-api/infra/file/preview/wopi/files/${fileId}?access_token=${getAccessToken()}`
  return `${wopi_client}?lang=zh-cn&WOPISrc=${encodeURIComponent(wopi)}`
}
