export const DEFAULT_CATALOG_URL = 'https://file.websoft.top/nnlc.pdf'

const safeDecodeUrl = (value: string) => {
  let nextValue = value

  for (let index = 0; index < 2; index += 1) {
    try {
      const decodedValue = decodeURIComponent(nextValue)
      if (decodedValue === nextValue) {
        break
      }
      nextValue = decodedValue
    } catch {
      break
    }
  }

  return nextValue
}

export const normalizePdfUrl = (value?: string) => {
  const trimmedValue = (value || '').trim()
  if (!trimmedValue) {
    return DEFAULT_CATALOG_URL
  }

  return safeDecodeUrl(trimmedValue)
}

export const isPdfUrl = (value?: string) => /\.pdf(\?|#|$)/i.test((value || '').trim())
