export const isUsableSourceUrl = (value?: string | null) =>
  typeof value === 'string' && (/^https?:\/\//i.test(value) || value.startsWith('/'))

export const resolveSourceUrl = (source?: string | null, explicitUrl?: string | null) => {
  if (isUsableSourceUrl(explicitUrl)) return explicitUrl || undefined
  if (isUsableSourceUrl(source)) return source || undefined
  const sourceText = typeof source === 'string' ? source.trim() : ''
  const isBareDomain = /^[a-z0-9.-]+\.[a-z]{2,}(\/.*)?$/i.test(sourceText) && !/\s/.test(sourceText)
  if (isBareDomain) return `https://${sourceText}`
  return undefined
}
