import { joinApiUrl } from '@/config'

/** 是否外链或 data URL */
export function isExternalUrl(url) {
  if (!url) return false
  return /^(https?:|data:|\/\/)/i.test(String(url).trim())
}

/**
 * 将后端返回的图片路径转为可展示的完整 URL
 * @param {string} path bannerImage / categoryPic / mainPic 等
 */
export function resolveFileUrl(path) {
  if (path == null || path === '') {
    return ''
  }
  const raw = String(path).split(',')[0].trim()
  if (!raw) {
    return ''
  }
  if (isExternalUrl(raw)) {
    return raw
  }
  return joinApiUrl(raw.startsWith('/') ? raw : `/${raw}`)
}
