// Inline SVG icons — stroke = currentColor
const Icon = ({ name, size = 16, stroke = 1.6, ...rest }) => {
  const common = {
    width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
    stroke: 'currentColor', strokeWidth: stroke, strokeLinecap: 'round', strokeLinejoin: 'round',
    'aria-hidden': true, ...rest,
  };
  switch (name) {
    case 'search': return (<svg {...common}><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/></svg>);
    case 'arrow-down': return (<svg {...common}><path d="M12 5v14"/><path d="m6 13 6 6 6-6"/></svg>);
    case 'arrow-right': return (<svg {...common}><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>);
    case 'arrow-up-right': return (<svg {...common}><path d="M7 17 17 7"/><path d="M7 7h10v10"/></svg>);
    case 'check': return (<svg {...common}><path d="M5 13l4 4L19 7"/></svg>);
    case 'check-circle': return (<svg {...common}><circle cx="12" cy="12" r="9"/><path d="m8 12 3 3 5-6"/></svg>);
    case 'clock': return (<svg {...common}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>);
    case 'truck': return (<svg {...common}><path d="M3 7h11v10H3z"/><path d="M14 10h4l3 3v4h-7z"/><circle cx="7" cy="18" r="2"/><circle cx="17" cy="18" r="2"/></svg>);
    case 'box': return (<svg {...common}><path d="M3 7l9-4 9 4-9 4-9-4z"/><path d="M3 7v10l9 4 9-4V7"/><path d="M12 11v10"/></svg>);
    case 'shield': return (<svg {...common}><path d="M12 3l8 3v6c0 5-4 8-8 9-4-1-8-4-8-9V6l8-3z"/><path d="m9 12 2 2 4-4"/></svg>);
    case 'sparkle': return (<svg {...common}><path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8"/></svg>);
    case 'flame': return (<svg {...common}><path d="M12 22a7 7 0 0 0 7-7c0-4-3-5-3-9 0 0-2 2-4 2-1 0-2-1-2-3 0 0-5 4-5 10a7 7 0 0 0 7 7z"/></svg>);
    case 'compare': return (<svg {...common}><path d="M4 6h16"/><path d="M4 12h16"/><path d="M4 18h10"/><circle cx="18" cy="18" r="3"/></svg>);
    case 'plus': return (<svg {...common}><path d="M12 5v14"/><path d="M5 12h14"/></svg>);
    case 'minus': return (<svg {...common}><path d="M5 12h14"/></svg>);
    case 'close': return (<svg {...common}><path d="m6 6 12 12"/><path d="M6 18 18 6"/></svg>);
    case 'filter': return (<svg {...common}><path d="M3 5h18"/><path d="M6 12h12"/><path d="M10 19h4"/></svg>);
    case 'sort': return (<svg {...common}><path d="M8 6v12"/><path d="m4 10 4-4 4 4"/><path d="M16 18V6"/><path d="m20 14-4 4-4-4"/></svg>);
    case 'menu': return (<svg {...common}><path d="M3 6h18"/><path d="M3 12h18"/><path d="M3 18h18"/></svg>);
    case 'star': return (<svg {...common}><path d="m12 2 3 7 7 1-5 5 1 7-6-3-6 3 1-7-5-5 7-1z"/></svg>);
    case 'warn': return (<svg {...common}><path d="M12 9v4"/><path d="M12 17h.01"/><path d="m12 3 10 18H2z"/></svg>);
    case 'fish': return (<svg {...common}><path d="M2 12c4-6 12-6 16 0-4 6-12 6-16 0z"/><path d="M18 12c1.5-2 3-2 4 0-1 2-2.5 2-4 0z"/><circle cx="6" cy="11" r="0.6" fill="currentColor"/></svg>);
    case 'anchor': return (<svg {...common}><circle cx="12" cy="5" r="2.2"/><path d="M12 7v14"/><path d="M5 15a7 7 0 0 0 14 0"/><path d="M9 11h6"/></svg>);
    case 'pump': return (<svg {...common}><rect x="5" y="9" width="10" height="11" rx="1.5"/><path d="M15 13h3v7"/><path d="M9 5h3v4H8z"/></svg>);
    case 'engine': return (<svg {...common}><rect x="4" y="9" width="14" height="8" rx="1.5"/><path d="M8 9V6h6v3"/><path d="M18 11h2v4h-2"/></svg>);
    case 'prop': return (<svg {...common}><circle cx="12" cy="12" r="2"/><path d="M12 10c-3-3-6-2-6 1s3 4 6 1"/><path d="M12 14c3 3 6 2 6-1s-3-4-6-1"/><path d="M10 12c-3 3-2 6 1 6s4-3 1-6"/><path d="M14 12c3-3 2-6-1-6s-4 3-1 6"/></svg>);
    case 'paint': return (<svg {...common}><rect x="6" y="3" width="12" height="6" rx="1"/><path d="M6 9v3c0 1 1 2 2 2h4v3"/><rect x="10" y="17" width="4" height="4" rx="0.5"/></svg>);
    case 'safety': return (<svg {...common}><path d="M7 7h10l-1 12-4 2-4-2z"/><path d="M7 7c1-3 3-4 5-4s4 1 5 4"/></svg>);
    case 'rope': return (<svg {...common}><path d="M5 5c4 2 4 6 0 8s-4 6 0 8"/><path d="M19 5c-4 2-4 6 0 8s4 6 0 8"/></svg>);
    case 'partner': return (<svg {...common}><path d="M12 3l8 3v6c0 5-4 8-8 9-4-1-8-4-8-9V6l8-3z"/><path d="m9 12 2 2 4-4"/></svg>);
    case 'camera': return (<svg {...common}><path d="M4 8a2 2 0 0 1 2-2h2l2-2h4l2 2h2a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2z"/><circle cx="12" cy="13" r="4"/></svg>);
    case 'upload': return (<svg {...common}><path d="M12 3v13"/><path d="m6 9 6-6 6 6"/><path d="M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-2"/></svg>);
    case 'image': return (<svg {...common}><rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="9" cy="9" r="1.5"/><path d="m21 16-5-5-9 9"/></svg>);
    case 'dashboard': return (<svg {...common}><rect x="3" y="3" width="7" height="9" rx="1"/><rect x="14" y="3" width="7" height="5" rx="1"/><rect x="14" y="12" width="7" height="9" rx="1"/><rect x="3" y="16" width="7" height="5" rx="1"/></svg>);
    case 'merchants': return (<svg {...common}><path d="M3 7h18l-1.5 4H4.5z"/><path d="M5 11v9h14v-9"/><path d="M9 20v-5h6v5"/></svg>);
    case 'products': return (<svg {...common}><path d="M3 7l9-4 9 4-9 4-9-4z"/><path d="M3 7v10l9 4 9-4V7"/></svg>);
    case 'analytics': return (<svg {...common}><path d="M3 3v18h18"/><path d="m7 14 4-4 3 3 5-6"/></svg>);
    case 'settings': return (<svg {...common}><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .4 1.9l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.9-.4 1.7 1.7 0 0 0-1 1.5V21a2 2 0 0 1-4 0v-.1a1.7 1.7 0 0 0-1-1.5 1.7 1.7 0 0 0-1.9.4l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .4-1.9 1.7 1.7 0 0 0-1.5-1H3a2 2 0 0 1 0-4h.1a1.7 1.7 0 0 0 1.5-1 1.7 1.7 0 0 0-.4-1.9l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.9.4h0a1.7 1.7 0 0 0 1-1.5V3a2 2 0 0 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.9-.4l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.4 1.9v0a1.7 1.7 0 0 0 1.5 1H21a2 2 0 0 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></svg>);
    case 'alert': return (<svg {...common}><circle cx="12" cy="12" r="9"/><path d="M12 8v4"/><path d="M12 16h.01"/></svg>);
    case 'rss': return (<svg {...common}><path d="M4 11a9 9 0 0 1 9 9"/><path d="M4 4a16 16 0 0 1 16 16"/><circle cx="5" cy="19" r="1.5"/></svg>);
    case 'link': return (<svg {...common}><path d="M10 13a5 5 0 0 0 7.5 0l3-3a5 5 0 1 0-7-7l-1 1"/><path d="M14 11a5 5 0 0 0-7.5 0l-3 3a5 5 0 1 0 7 7l1-1"/></svg>);
    default: return null;
  }
};

// stock status -> chip mapping per DESIGN.md Ek C
const STOCK_MAP = {
  IN_STOCK:   { icon: 'check-circle', cls: 'good', text: 'Stokta var' },
  LOW_STOCK:  { icon: 'flame',        cls: 'warn', text: 'Az kaldı' },
  OUT_OF_STOCK:{ icon: 'warn',        cls: 'bad',  text: 'Stokta yok' },
  PREORDER:   { icon: 'clock',        cls: 'deep', text: 'Ön sipariş' },
  UNKNOWN:    { icon: null,           cls: '',     text: 'Stok bilgisi yok' },
};

// tr-TR currency
const TRY_FMT = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY', maximumFractionDigits: 2, minimumFractionDigits: 0 });
const formatPrice = (n) => TRY_FMT.format(n);
const formatNum = (n) => new Intl.NumberFormat('tr-TR').format(n);

// "X saat önce" in tr-TR
function timeAgoTr(iso) {
  const diffSec = Math.floor((Date.now() - new Date(iso).getTime()) / 1000);
  if (diffSec < 60) return 'az önce';
  const diffMin = Math.floor(diffSec / 60);
  if (diffMin < 60) return `${diffMin} dk önce`;
  const diffH = Math.floor(diffMin / 60);
  if (diffH < 24) return `${diffH} saat önce`;
  const diffD = Math.floor(diffH / 24);
  if (diffD < 30) return `${diffD} gün önce`;
  return `${Math.floor(diffD / 30)} ay önce`;
}

// hash router — utilities
function parseRoute() {
  const h = window.location.hash || '#/';
  const path = h.replace(/^#/, '');
  if (path.startsWith('/urun/')) {
    const after = path.slice(6);
    const m = after.match(/^(.+)-p-([a-z0-9]+)$/);
    if (m) return { name: 'pdp', shortSlug: m[2], slug: m[1] };
  }
  if (path.startsWith('/arama')) {
    const q = new URLSearchParams(path.split('?')[1] || '').get('q') || '';
    return { name: 'search', q };
  }
  if (path.startsWith('/kategori/')) {
    return { name: 'category', slug: path.slice(10) };
  }
  if (path.startsWith('/markalar')) return { name: 'brands' };
  if (path.startsWith('/marka/')) {
    return { name: 'brand', slug: path.slice(7) };
  }
  if (path.startsWith('/kategoriler')) return { name: 'categories' };
  if (path.startsWith('/en-ucuz-')) {
    return { name: 'seo-cheapest', slug: path.slice(9) };
  }
  if (path.startsWith('/nasil-calisir')) return { name: 'how' };
  if (path.startsWith('/satici-basvuru')) return { name: 'signup' };
  if (path.startsWith('/admin')) return { name: 'admin' };
  if (path.startsWith('/hakkimizda')) return { name: 'about' };
  if (path.startsWith('/iletisim')) return { name: 'contact' };
  if (path.startsWith('/sss')) return { name: 'faq' };
  if (path.startsWith('/gizlilik')) return { name: 'privacy' };
  if (path.startsWith('/kullanim-sartlari')) return { name: 'terms' };
  if (path.startsWith('/cerez')) return { name: 'cookies' };
  if (path.startsWith('/yardim')) return { name: 'help' };
  if (path.startsWith('/favoriler')) return { name: 'favorites' };
  if (path.startsWith('/alarm')) return { name: 'alerts' };
  if (path.startsWith('/teklif-bildir')) {
    const slug = path.slice('/teklif-bildir'.length).replace(/^\//, '') || null;
    return { name: 'priceReport', shortSlug: slug };
  }
  if (path.startsWith('/magaza/')) return { name: 'merchant', slug: path.slice(8) };
  if (path.startsWith('/go/')) return { name: 'go', offerId: path.slice(4) };
  if (path === '/' || path === '') return { name: 'home' };
  return { name: '404' };
}

function navigate(href) {
  window.location.hash = href;
  window.scrollTo({ top: 0, behavior: 'instant' });
}

function urlForProduct(p) { return `#/urun/${p.slug}-p-${p.shortSlug}`; }
function urlForSearch(q)   { return `#/arama?q=${encodeURIComponent(q)}`; }
function urlForCategory(c) { return `#/kategori/${c}`; }
function urlForBrand(b)    { return `#/marka/${b}`; }
function urlForMerchant(m) { return `#/magaza/${m}`; }
function urlForGo(offerId) { return `#/go/${offerId}`; }

Object.assign(window, {
  Icon, STOCK_MAP, formatPrice, formatNum, timeAgoTr,
  parseRoute, navigate, urlForProduct, urlForSearch, urlForCategory, urlForBrand,
  urlForMerchant, urlForGo,
});
