import type { ImportResult } from './types.ts'

export function parseGpx(text: string): ImportResult {
  const doc = new DOMParser().parseFromString(text, 'text/xml')
  const bookmarks: ImportResult['bookmarks'] = []

  for (const wpt of doc.querySelectorAll('wpt')) {
    const lat = parseFloat(wpt.getAttribute('lat') ?? '')
    const lng = parseFloat(wpt.getAttribute('lon') ?? '')
    if (isNaN(lat) || isNaN(lng)) continue
    const name = wpt.querySelector('name')?.textContent?.trim() || undefined
    const description = wpt.querySelector('desc')?.textContent?.trim() ||
      undefined
    bookmarks.push({
      lat,
      lng,
      folderTempId: null,
      properties: { name, description },
    })
  }

  return { folders: [], bookmarks }
}
