← back to All Designerwallcoverings

lib/vendor-host.js

35 lines

'use strict';

const { hostSlugify } = require('./vendor-pairs');
const BASE = '.designerwallcoverings.com';

function buildVendorHostMap(vendors, productRows) {
  const map = new Map();
  const put = (name, explicitSlug) => {
    const vendor = String(name || '').trim();
    const slug = hostSlugify(explicitSlug || vendor);
    if (vendor && slug && !map.has(slug)) map.set(slug, vendor);
  };
  for (const row of vendors || []) put(row.name || row.vendor, row.site_slug);
  for (const row of productRows || []) put(row.vendor);
  return map;
}

function vendorHostContext(hostHeader, vendorMap) {
  const host = String(hostHeader || '').split(':')[0].toLowerCase();
  if (!host.endsWith(BASE)) return null;
  let slug = host.slice(0, -BASE.length);
  if (!slug || slug === 'all' || slug.includes('.')) return null;
  const internal = slug.endsWith('-internal');
  if (internal) slug = slug.slice(0, -'-internal'.length);
  const vendor = vendorMap.get(slug);
  return vendor ? { vendor, slug, internal, host } : { vendor: null, slug, internal, host };
}

function vendorRootFile(context, pathname) {
  if (!context || !context.vendor || !context.internal) return null;
  return pathname === '/' || pathname === '/index.html' ? '/vendor-internal.html' : null;
}

module.exports = { buildVendorHostMap, vendorHostContext, vendorRootFile };