// Baswara Bali — products + shared helpers

// Products synced from Accel ERP (company: Baswara Bali, Retail Price Book).
// Source export: images/products/_erp-export.json. Re-export to update prices/descriptions.
const PRODUCTS = [
  {"id": "choc-0018", "code": "CHOC-0018", "name": "Bali Cantik Dark", "weight": "30gr", "price": 20000, "category": "bars", "cacao": "", "description": "", "image": "images/products/choc-0018.jpg", "color": "#3a1f12", "accent": "#8b3a2a"},
  {"id": "choc-0019", "code": "CHOC-0019", "name": "Bali Cantik Milk", "weight": "30gr", "price": 20000, "category": "bars", "cacao": "", "description": "", "image": "images/products/choc-0019.jpg", "color": "#8b5a3c", "accent": "#c4955f"},
  {"id": "choc-0020", "code": "CHOC-0020", "name": "Bali Cantik White", "weight": "30gr", "price": 20000, "category": "bars", "cacao": "", "description": "", "image": "images/products/choc-0020.jpg", "color": "#f0e0c4", "accent": "#d9b46a"},
  {"id": "choc-0015", "code": "CHOC-0015", "name": "Bali Finest Dark 55%", "weight": "", "price": 35000, "category": "bars", "cacao": "55%", "description": "", "image": "images/products/choc-0015.jpg", "color": "#4a2a1a", "accent": "#a06a3a"},
  {"id": "choc-0016", "code": "CHOC-0016", "name": "Bali Finest Dark 70%", "weight": "", "price": 35000, "category": "bars", "cacao": "70%", "description": "", "image": "images/products/choc-0016.jpg", "color": "#2a1810", "accent": "#8b3a2a"},
  {"id": "choc-0002", "code": "CHOC-0002", "name": "I Love Bali Dark", "weight": "40gr", "price": 25000, "category": "bars", "cacao": "", "description": "Rich dark chocolate bar with authentic Bali branding. Part of the \"From Bali With Love\" I Love Bali collection. Halal certified.", "image": "images/products/choc-0002.jpg", "color": "#3a1f12", "accent": "#b48a3c"},
  {"id": "choc-0003", "code": "CHOC-0003", "name": "I Love Bali Milk", "weight": "40gr", "price": 25000, "category": "bars", "cacao": "", "description": "Smooth milk chocolate bar with Bali souvenir packaging. Part of the \"From Bali With Love\" I Love Bali collection. Halal certified.", "image": "images/products/choc-0003.jpg", "color": "#8b5a3c", "accent": "#c4955f"},
  {"id": "choc-0004", "code": "CHOC-0004", "name": "I Love Bali White", "weight": "40gr", "price": 25000, "category": "bars", "cacao": "", "description": "Creamy white chocolate bar with Bali cultural design. Part of the \"From Bali With Love\" I Love Bali collection. Halal certified.", "image": "images/products/choc-0004.jpg", "color": "#e8d5b0", "accent": "#d9b46a"},
  {"id": "choc-0009", "code": "CHOC-0009", "name": "Barong Bar Milk Cookies", "weight": "95gr", "price": 35000, "category": "bars", "cacao": "", "description": "Milk chocolate cookies bar with the traditional Barong Bali design. A 95gr bar perfect for everyday enjoyment. Halal certified.", "image": "images/products/choc-0009.jpg", "color": "#8b5a3c", "accent": "#b48a3c"},
  {"id": "choc-0014", "code": "CHOC-0014", "name": "Chocomail", "weight": "95gr", "price": 35000, "category": "bars", "cacao": "", "description": "White cookies chocolate in a special postcard-style \"Special Gift From Bali\" packaging. 95gr bar. Halal certified.", "image": "images/products/choc-0014.jpg", "color": "#e8d5b0", "accent": "#a06a3a"},
  {"id": "choc-0006", "code": "CHOC-0006", "name": "Barong Praline Cookies", "weight": "12 × 9gr", "price": 40000, "category": "boxes", "cacao": "", "description": "Praline chocolate with cookies featuring the traditional Barong mask design. 12 individually wrapped pieces. Halal certified.", "image": "images/products/choc-0006.jpg", "color": "#5a3a24", "accent": "#b48a3c"},
  {"id": "choc-0007", "code": "CHOC-0007", "name": "Barong Fruity", "weight": "12 × 9gr", "price": 40000, "category": "boxes", "cacao": "", "description": "Fruity chocolate with orange, strawberry, and blueberry flavors in Barong Bali packaging. 12 pieces per box. Halal certified.", "image": "images/products/choc-0007.jpg", "color": "#8b3a2a", "accent": "#e2a07a"},
  {"id": "choc-0008", "code": "CHOC-0008", "name": "Barong Nutz", "weight": "12 × 9gr", "price": 40000, "category": "boxes", "cacao": "", "description": "Chocolate with cashew and almond nuts in Barong Bali packaging. 12 individually wrapped pieces. Halal certified.", "image": "images/products/choc-0008.jpg", "color": "#6b3f24", "accent": "#c4955f"},
  {"id": "choc-0017", "code": "CHOC-0017", "name": "Barong Praline Cream", "weight": "12 × 10gr", "price": 55000, "category": "boxes", "cacao": "", "description": "", "image": "images/products/choc-0017.jpg", "color": "#5a3a24", "accent": "#d9b46a"},
  {"id": "choc-0028", "code": "CHOC-0028", "name": "Barong Bali Chocolate Cookies", "weight": "130g · 10 pcs", "price": 50000, "category": "boxes", "cacao": "", "description": "Barong Bali rasa cokelat isi 10 pcs", "image": "images/products/choc-0028.jpg", "color": "#3a2418", "accent": "#b48a3c"},
  {"id": "choc-0027", "code": "CHOC-0027", "name": "Barong Bali Vanilla Cookies", "weight": "130g · 10 pcs", "price": 50000, "category": "boxes", "cacao": "", "description": "Barong Bali rasa vanilla isi 10 pcs", "image": "images/products/choc-0027.jpg", "color": "#c4955f", "accent": "#e2c48a"},
  {"id": "choc-0005", "code": "CHOC-0005", "name": "I Love Bali Gift", "weight": "3 × 45gr", "price": 72000, "category": "gifts", "cacao": "", "description": "Premium gift box featuring 3 I Love Bali chocolate bars (45gr each). Perfect Bali souvenir gift set. Halal certified.", "image": "images/products/choc-0005.jpg", "color": "#8b3a2a", "accent": "#d9b46a"},
  {"id": "choc-0011", "code": "CHOC-0011", "name": "Gumsberry Set 5", "weight": "5 × 25gr", "price": 40000, "category": "gifts", "cacao": "", "description": "Chocolate mix featuring Bali landmarks: Lovina, Tirta Empul, Monkey Forest, and Tanah Lot. 5 bars at 25gr each. Halal certified.", "image": "images/products/choc-0011.jpg", "color": "#3a6a8a", "accent": "#d9b46a"},
  {"id": "choc-0021", "code": "CHOC-0021", "name": "Gumsberry Set 3", "weight": "3 × 25gr", "price": 25000, "category": "gifts", "cacao": "", "description": "", "image": "images/products/choc-0021.jpg", "color": "#3a6a8a", "accent": "#d9b46a"},
  {"id": "choc-0013", "code": "CHOC-0013", "name": "Coffee Choc Gift", "weight": "15 × 8gr", "price": 45000, "category": "gifts", "cacao": "", "description": "Indonesian coffee-flavored chocolate gift set. 15 individually wrapped pieces at 8gr each. Halal certified.", "image": "images/products/choc-0013.jpg", "color": "#3a2418", "accent": "#a06a3a"},
  {"id": "choc-0010", "code": "CHOC-0010", "name": "Bali Cantik Gift Set 6", "weight": "6 × 30gr", "price": 115000, "category": "premium", "cacao": "", "description": "Premium gift collection with 6 chocolate flavors: Dark, Milk, White, Chilli, Ginger, and Cinnamon. Each bar 30gr. Halal certified.", "image": "images/products/choc-0010.jpg", "color": "#4a2d1a", "accent": "#b48a3c"},
  {"id": "choc-0012", "code": "CHOC-0012", "name": "Coffee Choc Gift", "weight": "36 × 8gr", "price": 65000, "category": "premium", "cacao": "", "description": "Large gift set of Indonesian coffee-flavored chocolate. 36 individually wrapped pieces at 8gr each. Halal certified.", "image": "images/products/choc-0012.jpg", "color": "#2a1810", "accent": "#b48a3c"},
];

const CATEGORIES = [
  { id: "all", label: "Semua", labelEn: "All" },
  { id: "bars", label: "Batang", labelEn: "Single Bar" },
  { id: "boxes", label: "Kotak", labelEn: "Box Set" },
  { id: "gifts", label: "Hadiah", labelEn: "Gift Set" },
  { id: "premium", label: "Premium", labelEn: "Premium Gift" },
];

// Shipping rates synced from Accel ERP (courier_service_rate, origin "Denpasar (Origin)").
// IDR per kg, min 1 kg, weight rounded up. Product weights in the ERP are placeholders,
// so every order is charged at SHIPPING.assumedKg until real weights exist.
const SHIPPING = {
  origin: "Denpasar",
  defaultCourier: "JNE",
  assumedKg: 1,
  couriers: [
    { code: "JNE", name: "JNE Express" },
    { code: "JT", name: "J&T Express" },
    { code: "SICEPAT", name: "SiCepat Express" },
    { code: "ANTERAJA", name: "AnterAja" },
    { code: "SAP", name: "SAP Express" },
  ],
  zones: ["Jakarta", "Bandung", "Semarang", "Yogyakarta", "Surabaya", "Makassar", "Medan"],
  ratePerKg: {
    JNE:      { Bandung: 19000, Jakarta: 18000, Makassar: 18000, Medan: 52000, Semarang: 15000, Surabaya: 12000, Yogyakarta: 14000 },
    JT:       { Bandung: 18000, Jakarta: 17000, Makassar: 17000, Medan: 50000, Semarang: 14000, Surabaya: 11000, Yogyakarta: 13000 },
    SICEPAT:  { Bandung: 17000, Jakarta: 16000, Makassar: 16000, Medan: 48000, Semarang: 14000, Surabaya: 11000, Yogyakarta: 13000 },
    ANTERAJA: { Bandung: 17000, Jakarta: 16000, Makassar: 16000, Medan: 49000, Semarang: 14000, Surabaya: 11000, Yogyakarta: 13000 },
    SAP:      { Bandung: 16000, Jakarta: 15000, Makassar: 15000, Medan: 45000, Semarang: 13000, Surabaya: 10000, Yogyakarta: 12000 },
  },
};
const OTHER_ZONE = "Kota lain";

// Returns the fee in IDR, or null when the ERP has no rate (confirmed via WhatsApp).
const shippingFee = (courier, zone, kg = SHIPPING.assumedKg) => {
  const rate = (SHIPPING.ratePerKg[courier] || {})[zone];
  return rate == null ? null : rate * Math.max(1, Math.ceil(kg));
};
const minShippingFee = () =>
  Math.min(...Object.values(SHIPPING.ratePerKg).flatMap((r) => Object.values(r)));

const formatIDR = (n) =>
  "IDR " + n.toLocaleString("id-ID");

// Virtual store settings from Accel ERP (virtual_store_config + banner files)
const STORE = {
  name: "Baswara Bali",
  tagline: "Indonesian Chocolate - From Bali With Love",
  heroBanner: "images/banners/banner-popart.jpg",           // active config BONI_VSTORE_CONFIG_001
  catalogBanner: "images/banners/banner-brown-light.jpg",   // "brown light" theme configs
};

const WA_NUMBER = "6282275767739"; // Accel ERP virtual_store_config
const waLink = (msg) =>
  `https://wa.me/${WA_NUMBER}?text=${encodeURIComponent(msg)}`;

// Simple inline icons
function Icon({ name, size = 20, stroke = 1.6 }) {
  const props = {
    width: size,
    height: size,
    viewBox: "0 0 24 24",
    fill: "none",
    stroke: "currentColor",
    strokeWidth: stroke,
    strokeLinecap: "round",
    strokeLinejoin: "round",
  };
  switch (name) {
    case "bag":
      return (
        <svg {...props}>
          <path d="M5 8h14l-1 12H6L5 8z" />
          <path d="M9 8V6a3 3 0 1 1 6 0v2" />
        </svg>
      );
    case "search":
      return (
        <svg {...props}>
          <circle cx="11" cy="11" r="7" />
          <path d="M20 20l-3.5-3.5" />
        </svg>
      );
    case "back":
      return (
        <svg {...props}>
          <path d="M15 6l-6 6 6 6" />
        </svg>
      );
    case "close":
      return (
        <svg {...props}>
          <path d="M6 6l12 12M18 6L6 18" />
        </svg>
      );
    case "plus":
      return (
        <svg {...props}>
          <path d="M12 5v14M5 12h14" />
        </svg>
      );
    case "minus":
      return (
        <svg {...props}>
          <path d="M5 12h14" />
        </svg>
      );
    case "trash":
      return (
        <svg {...props}>
          <path d="M4 7h16M9 7V5a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2M6 7l1 12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-12" />
        </svg>
      );
    case "heart":
      return (
        <svg {...props}>
          <path d="M12 20s-7-4.5-7-10a4 4 0 0 1 7-2.6A4 4 0 0 1 19 10c0 5.5-7 10-7 10z" />
        </svg>
      );
    case "wa":
      return (
        <svg viewBox="0 0 24 24" width={size} height={size} fill="currentColor">
          <path d="M17.5 14.4c-.3-.1-1.7-.8-2-.9-.3-.1-.5-.1-.7.1-.2.3-.8.9-1 1.1-.2.2-.4.2-.7.1-1.7-.8-2.7-1.5-3.8-3.4-.3-.5.3-.5.8-1.5.1-.2 0-.3 0-.5 0-.1-.7-1.6-.9-2.2-.2-.6-.5-.5-.7-.5h-.6c-.2 0-.5.1-.8.4-.3.3-1 1-1 2.5s1 2.9 1.2 3.1c.1.2 2.1 3.2 5.1 4.5 1.9.8 2.6.9 3.6.7.6-.1 1.7-.7 2-1.4.2-.7.2-1.2.2-1.4 0-.1-.2-.2-.5-.4z" />
          <path d="M12 2a10 10 0 0 0-8.5 15.2L2 22l4.9-1.5A10 10 0 1 0 12 2zm0 18.2a8.2 8.2 0 0 1-4.2-1.2l-.3-.2-3 .9.9-2.9-.2-.3A8.2 8.2 0 1 1 12 20.2z" />
        </svg>
      );
    case "check":
      return (
        <svg {...props}>
          <path d="M5 13l4 4L19 7" />
        </svg>
      );
    case "chevron":
      return (
        <svg {...props}>
          <path d="M9 6l6 6-6 6" />
        </svg>
      );
    case "filter":
      return (
        <svg {...props}>
          <path d="M3 6h18M6 12h12M10 18h4" />
        </svg>
      );
    case "star":
      return (
        <svg {...props} fill="currentColor" stroke="none">
          <path d="M12 2l2.9 6.5 7.1.7-5.4 4.8 1.6 7L12 17.6 5.8 21l1.6-7L2 9.2l7.1-.7L12 2z" />
        </svg>
      );
    case "leaf":
      return (
        <svg {...props}>
          <path d="M5 19c4-9 10-12 16-13-1 8-5 14-13 16-2 .5-3-1-3-3z" />
          <path d="M7 17c3-3 6-5 10-7" />
        </svg>
      );
    case "package":
      return (
        <svg {...props}>
          <path d="M3 7l9-4 9 4-9 4-9-4z" />
          <path d="M3 7v10l9 4 9-4V7" />
          <path d="M12 11v10" />
        </svg>
      );
    case "share":
      return (
        <svg {...props}>
          <circle cx="6" cy="12" r="2" />
          <circle cx="18" cy="6" r="2" />
          <circle cx="18" cy="18" r="2" />
          <path d="M8 11l8-4M8 13l8 4" />
        </svg>
      );
    default:
      return null;
  }
}

// Product photo, fills its (position: relative) container
function ProductPhoto({ p, fit = "cover" }) {
  return (
    <img src={p.image} alt={p.name} loading="lazy" style={{
      position: "absolute", inset: 0, width: "100%", height: "100%",
      objectFit: fit, background: "#fff", display: "block",
    }} />
  );
}

const categoryLabel = (id) => (CATEGORIES.find((c) => c.id === id) || {}).label || "";

// Cocoa bar SVG illustration (decorative, used in landing hero)
function ChocolateBar({ color = "#3a1f12", accent = "#b48a3c", width = 200 }) {
  return (
    <svg viewBox="0 0 200 280" width={width} style={{ display: "block" }}>
      <defs>
        <linearGradient id={`g-${color.replace("#", "")}`} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" stopColor={color} stopOpacity="1" />
          <stop offset="1" stopColor={color} stopOpacity="0.7" />
        </linearGradient>
      </defs>
      {/* wrapper */}
      <rect x="40" y="20" width="120" height="200" rx="4" fill={`url(#g-${color.replace("#", "")})`} />
      {/* gold band */}
      <rect x="40" y="100" width="120" height="36" fill={accent} opacity="0.9" />
      {/* frangipani dot */}
      <circle cx="100" cy="118" r="14" fill="#fff8e7" />
      <circle cx="100" cy="118" r="5" fill={accent} />
      {/* base bar */}
      <rect x="20" y="220" width="160" height="28" rx="3" fill={color} />
      <rect x="20" y="220" width="160" height="6" rx="3" fill="#000" opacity="0.18" />
      {/* squares */}
      {[0,1,2,3].map(i => (
        <rect key={i} x={28 + i*38} y="226" width="32" height="16" fill="none" stroke="#000" strokeOpacity="0.18" />
      ))}
      {/* shadow */}
      <ellipse cx="100" cy="258" rx="70" ry="6" fill="#000" opacity="0.12" />
    </svg>
  );
}

Object.assign(window, {
  PRODUCTS, CATEGORIES, formatIDR, WA_NUMBER, waLink,
  Icon, ChocolateBar,
});
