const API_URL = '/api/dashboard/system-config'

const DEFAULT_MAP_HUB = {
  name: '红原县',
  coordinates: [102.568685, 32.826358],
  description: '四川省阿坝州红原县，中国重要的牦牛养殖基地',
  localOriginKeyword: '红原'
}

const DEFAULT_MAP_FLOW_LEVELS = {
  high: { threshold: 80, color: '#E6A23C', description: '主要流向' },
  medium: { threshold: 40, color: '#409EFF', description: '重要流向' },
  low: { threshold: 0, color: '#67C23A', description: '一般流向' }
}

const DEFAULT_SYSTEM_CONFIG = {
  title: '/标题.png',
  titleBackground: '/images/标题背景.png',
  mapHub: DEFAULT_MAP_HUB,
  mapFlowLevels: DEFAULT_MAP_FLOW_LEVELS,
  mapGeoCoordMap: {
    '红原县': [102.568685, 32.826358]
  }
}

export { DEFAULT_MAP_HUB, DEFAULT_MAP_FLOW_LEVELS }

export async function loadSystemConfig() {
  try {
    const response = await fetch(API_URL)
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`)
    }

    const result = await response.json()
    if (result.code !== 1 || !result.data) {
      throw new Error(result.message || '系统配置接口返回异常')
    }

    return {
      ...DEFAULT_SYSTEM_CONFIG,
      ...result.data
    }
  } catch (error) {
    console.warn('加载系统配置失败，使用默认配置:', error)
    return { ...DEFAULT_SYSTEM_CONFIG }
  }
}

export function isImageTitle(title) {
  return typeof title === 'string' && title.startsWith('/')
}
