/* global */
/* ------------------------------------------------------------------ */
/* Downloads registry                                                  */
/*                                                                     */
/* Single source of truth for every downloadable specifier asset.      */
/* Heavy files (DWG, DXF, Revit, 3D) are meant to live on external      */
/* object storage (Cloudflare R2 / CDN); the site is just the catalog. */
/*                                                                     */
/* To go live with R2 later: set DOWNLOADS_BASE to the CDN root, then   */
/* give each file a relative `file` path (e.g. "gateway/gateway.dwg").  */
/* Local files (assets/…) and absolute http(s) URLs are used as-is.     */
/* ------------------------------------------------------------------ */

// ↓ Set this to your Cloudflare R2 / CDN base once it's live,
//   e.g. "https://files.derlot.com". Leave "" while files are local.
const DOWNLOADS_BASE = "";

// Resolve a file reference to a final URL.
function downloadURL(file) {
  if (!file) return null;
  if (/^https?:\/\//i.test(file)) return file;          // absolute (e.g. BIMobject)
  if (file.indexOf("assets/") === 0) return file;        // shipped with the site
  if (DOWNLOADS_BASE) return DOWNLOADS_BASE.replace(/\/+$/, "") + "/" + file.replace(/^\/+/, "");
  return null;                                           // relative but no CDN yet → not yet available
}

/* Each entry:
 *   scope   — "range" (whole catalogue) or a collection slug (matches COLLECTIONS)
 *   group   — file-type bucket, e.g. "Libraries", "2D drawings", "3D / BIM",
 *             "Specification", "Care & maintenance", "Installation"
 *   name    — display label
 *   format  — PDF | DWG | DXF | RVT | 3DM | SKP | ZIP | Link
 *   size    — human string ("7.5 MB"); "—" if unknown
 *   file    — local path ("assets/…"), absolute URL, or CDN-relative ("gateway/x.dwg")
 *   gated   — true → released on request (lead capture), no direct link
 *   status  — "soon" → listed but not yet uploaded
 *   note    — optional one-liner
 */
const DOWNLOADS = [
  /* ---------- Whole-range libraries ---------- */
  { scope: "range", group: "Libraries", name: "Finishes & materials",            format: "PDF", size: "7.9 MB", file: "assets/downloads/derlot-finishes-and-materials.pdf", note: "Solid timber, powder coat and 34 graded-in upholstery textiles across grades A–E, plus leather." },
  { scope: "range", group: "Libraries", name: "Certifications pack",             format: "ZIP", size: "—", status: "soon", note: "GECA, FSC. BIFMA on select collections." },
  { scope: "range", group: "Libraries", name: "Environmental product declaration", format: "PDF", size: "—", status: "soon" },

  /* ---------- Per-collection (example: Gateway) — the file-type structure ----------
     Replace `gated`/`status` with a real `file` (CDN-relative once R2 is live). */
  { scope: "gateway", group: "Specification", name: "Product specification",      format: "PDF", size: "—", status: "soon" },
  { scope: "gateway", group: "2D drawings",   name: "Plan, elevation, section",   format: "DWG", size: "—", gated: true },
  { scope: "gateway", group: "2D drawings",   name: "Plan, elevation, section",   format: "DXF", size: "—", gated: true },
  { scope: "gateway", group: "3D / BIM",      name: "Revit family",               format: "RVT", size: "—", gated: true },
  { scope: "gateway", group: "3D / BIM",      name: "BIMobject library",          format: "Link", file: "https://www.bimobject.com/" },
];

// GTM download tracking — push a file_download event to the dataLayer.
function trackDownload(item) {
  try {
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      event: "file_download",
      file_name: item.name,
      file_format: item.format,
      file_scope: item.scope,
    });
  } catch (e) { /* no-op */ }
}

window.DOWNLOADS = DOWNLOADS;
window.DOWNLOADS_BASE = DOWNLOADS_BASE;
window.downloadURL = downloadURL;
window.trackDownload = trackDownload;
