(function (root, factory) {
  "use strict";
  const api = factory();
  if (typeof module === "object" && module.exports) module.exports = api;
  root.SDMCaptureConfig = api;
})(typeof globalThis !== "undefined" ? globalThis : this, function () {
  "use strict";

  const cloudConfig = globalThis.YILIAN_CLOUD_CONFIG || {};
  const configuredApiBase = String(cloudConfig.apiBaseUrl || "").trim().replace(/\/$/, "");
  const configuredWorkbench = String(cloudConfig.workbenchUrl || "").trim().replace(/\/$/, "");

  const SETTINGS = Object.freeze({
    ERP_BASE_URL: configuredApiBase || "http://127.0.0.1:8001/api",
    WORKBENCH_URL: configuredWorkbench || "http://127.0.0.1:8001/",
    TASK_STORAGE_KEY: "sdmCaptureTasksV2",
    CACHE_STORAGE_KEY: "sdmCaptureCacheV3",
    OPTIONS_STORAGE_KEY: "sdmCaptureOptionsV1",
    ALARM_NAME: "sdm-capture-queue",
    TASK_CONCURRENCY: 2,
    HTTP_CONCURRENCY: 4,
    CAPTURE_TAB_TIMEOUT_MS: 45000,
    CAPTURE_TAB_POLL_MS: 750,
    REQUEST_RETRIES: 3,
    RETRY_BASE_DELAY_MS: 1600,
    MIN_REQUEST_GAP_MS: 450,
    CACHE_TTL_MS: 6 * 60 * 60 * 1000,
    MAX_CACHE_ENTRIES: 180,
    MAX_TASKS: 500,
    // Large variation families are completed in restart-safe chunks.
    MAX_VARIANTS_PER_TASK: 50000,
    VARIANT_FETCH_BATCH_SIZE: 24,
    DEFAULT_INCLUDE_VARIANTS: true,
    COLLECTOR_V2_ENABLED: true,
    ALLOW_LEGACY_TAB_FALLBACK: false,
    V2_FETCH_CONCURRENCY: 3,
    V2_FETCH_TIMEOUT_MS: 25000,
    V2_FETCH_RETRIES: 3
  });

  const SITES = Object.freeze({
    "amazon.com": { site: "US", country: "US", currency: "USD" },
    "amazon.ca": { site: "CA", country: "CA", currency: "CAD" },
    "amazon.com.mx": { site: "MX", country: "MX", currency: "MXN" },
    "amazon.com.br": { site: "BR", country: "BR", currency: "BRL" },
    "amazon.co.uk": { site: "UK", country: "GB", currency: "GBP" },
    "amazon.de": { site: "DE", country: "DE", currency: "EUR" },
    "amazon.fr": { site: "FR", country: "FR", currency: "EUR" },
    "amazon.it": { site: "IT", country: "IT", currency: "EUR" },
    "amazon.es": { site: "ES", country: "ES", currency: "EUR" },
    "amazon.nl": { site: "NL", country: "NL", currency: "EUR" },
    "amazon.se": { site: "SE", country: "SE", currency: "SEK" },
    "amazon.pl": { site: "PL", country: "PL", currency: "PLN" },
    "amazon.com.be": { site: "BE", country: "BE", currency: "EUR" },
    "amazon.co.jp": { site: "JP", country: "JP", currency: "JPY" },
    "amazon.in": { site: "IN", country: "IN", currency: "INR" },
    "amazon.com.au": { site: "AU", country: "AU", currency: "AUD" },
    "amazon.sg": { site: "SG", country: "SG", currency: "SGD" },
    "amazon.ae": { site: "AE", country: "AE", currency: "AED" },
    "amazon.sa": { site: "SA", country: "SA", currency: "SAR" },
    "amazon.com.tr": { site: "TR", country: "TR", currency: "TRY" },
    "amazon.ie": { site: "IE", country: "IE", currency: "EUR" }
  });

  function clean(value) {
    return String(value == null ? "" : value).trim();
  }

  function normalizeHost(value) {
    let host = clean(value).toLowerCase();
    try {
      if (host.includes("://")) host = new URL(host).hostname.toLowerCase();
    } catch {}
    return host.replace(/^www\./, "").replace(/:\d+$/, "");
  }

  function siteForHost(value) {
    const host = normalizeHost(value);
    return SITES[host] || null;
  }

  function isAmazonHost(value) {
    return Boolean(siteForHost(value));
  }

  function normalizeAsin(value) {
    const asin = clean(value).toUpperCase();
    return /^[A-Z0-9]{10}$/.test(asin) ? asin : "";
  }

  function asinFromUrl(value) {
    try {
      const url = new URL(value);
      const match = url.pathname.match(/(?:\/dp\/|\/gp\/product\/|\/product\/)([A-Z0-9]{10})(?:\/|$)/i);
      return match ? normalizeAsin(match[1]) : "";
    } catch {
      const match = clean(value).match(/(?:\/dp\/|\/gp\/product\/|\/product\/)([A-Z0-9]{10})(?:[/?#]|$)/i);
      return match ? normalizeAsin(match[1]) : "";
    }
  }

  function canonicalProductUrl(hostValue, asinValue) {
    const host = normalizeHost(hostValue);
    const asin = normalizeAsin(asinValue);
    if (!asin || !isAmazonHost(host)) return "";
    return `https://www.${host}/dp/${asin}?th=1&psc=1`;
  }

  function targetFromUrl(value) {
    try {
      const url = new URL(value);
      const domain = normalizeHost(url.hostname);
      const asin = asinFromUrl(url.href);
      if (!asin || !isAmazonHost(domain)) return null;
      const site = siteForHost(domain);
      return { asin, domain, site: site.site, country: site.country, url: canonicalProductUrl(domain, asin) };
    } catch {
      return null;
    }
  }

  return { SETTINGS, SITES, clean, normalizeHost, siteForHost, isAmazonHost, normalizeAsin, asinFromUrl, canonicalProductUrl, targetFromUrl };
});
