const axios = require("axios")

function buildScraperUrl(baseUrl, targetUrl) {
  if (!baseUrl) throw new Error("SCRAPER_API_URL missing")
  if (!targetUrl) throw new Error("url parametresi zorunlu")

  const normalizedBase = String(baseUrl)
  const encoded = encodeURIComponent(String(targetUrl))

  if (normalizedBase.includes("{url}")) {
    return normalizedBase.replace("{url}", encoded)
  }

  if (normalizedBase.endsWith("?") || normalizedBase.endsWith("&")) {
    return `${normalizedBase}url=${encoded}`
  }

  return normalizedBase.endsWith("/")
    ? `${normalizedBase}${encoded}`
    : `${normalizedBase}${encoded}`
}

async function getProductPreviewFromUrl(url) {
  const baseUrl = process.env.SCRAPER_API_URL
  const scraperUrl = buildScraperUrl(baseUrl, url)

  const { data } = await axios.get(scraperUrl, { timeout: 20000 })
  if (data && typeof data === "object" && data.product) return data.product
  return data
}

module.exports = { getProductPreviewFromUrl }
