export function debounce(fn: (...args: any[]) => void, wait = 100): (...args: any[]) => void {
  let timeoutId: ReturnType<typeof setTimeout> | undefined
  let timestamp: number
  let storedArguments: any[]

  function later() {
    const last = Date.now() - timestamp
    if (last < wait && last >= 0) {
      timeoutId = setTimeout(later, wait - last)
    } else {
      timeoutId = undefined
      fn(...storedArguments)
    }
  }
  return function (...args: any[]) {
    timestamp = Date.now()
    storedArguments = args
    if (!timeoutId) {
      timeoutId = setTimeout(later, wait)
    }
  }
}

export function nostrLink(code: string): string {
  let res = (window as any).nwc?.nostrLink?.(code)
  if (res) return res
  return 'https://njump.me/' + code
}

export function relayLink(url: string): string {
  let res = (window as any).nwc?.relayLink?.(url)
  if (res) return res
  return 'https://njump.me/r/' + encodeURIComponent(url)
}

export const transparentPixel = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='

export function handleImageError(ev: any) {
  const el = ev.target as HTMLImageElement
  console.warn('error loading image', ev)
  if (el.src !== transparentPixel) {
    el.src = transparentPixel
  }
}

export function splitComma(value: string | null): string[] {
  return value
    ? value
        .split(' ')
        .map(v => v.trim())
        .filter(v => v.length)
    : []
}

export function shorten(nip19code: string): string {
  let idx = nip19code.indexOf('1')
  return nip19code.substring(0, idx + 3) + '…' + nip19code.substring(nip19code.length - 5)
}

export function shortenURL(url: string): string {
  if (url.length <= 30) return url
  try {
    let u = new URL(url)
    if (url.length <= 40) {
      return `${u.host}${u.pathname}${u.search.length ? '?' : ''}${u.search}`
    }
    if (url.length <= 50 && u.pathname.length < 20) {
      return `${u.host}${u.pathname}`
    }
    let pathname = u.pathname
    let parts = u.pathname.split('/')

    let lastPart = parts[parts.length - 1]
    if (lastPart.length > 10) {
      lastPart = `…${lastPart.substring(lastPart.length - 6)}`
    }

    if (parts.length > 2) {
      pathname = `/…/${lastPart}`
    } else {
      pathname = `/${lastPart}`
    }

    return `${u.host}${pathname}`
  } catch (_err) {
    return url
  }
}
