export type ClipAnchor = {
  exact: string
  prefix?: string
  suffix?: string
  xpathStart?: string
  xpathEnd?: string
  startOffset?: number
  endOffset?: number
  startTextOffset?: number
  endTextOffset?: number
}

export type ClipItem = {
  id: string
  tabId?: number
  pageUrl: string
  pageTitle: string
  quote: string
  quoteHtml?: string
  createdAt: string
  color: 'yellow'
  anchor: ClipAnchor
  resolveStatus?: 'ok' | 'broken'
  resolveUpdatedAt?: string
}

export function normalizePageUrl(raw: string): string {
  try {
    const u = new URL(raw)
    u.hash = ''
    return u.toString()
  } catch {
    return String(raw || '').trim()
  }
}

export function normalizeQuote(raw: string): string {
  return String(raw || '')
    .replace(/\s+/g, ' ')
    .trim()
}
