// shared.jsx — ikony, formatowanie kwot i stan zamówienia kalendarza.

const CalIcon = {
  Locker: ({ size = 20 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <rect x="3.5" y="3.5" width="17" height="17" rx="2.5" stroke="currentColor" strokeWidth="1.6"/>
      <path d="M12 3.5v17M3.5 9.5H12M3.5 15h8.5M12 12h8.5" stroke="currentColor" strokeWidth="1.6"/>
      <circle cx="9.6" cy="12.2" r="0.9" fill="currentColor"/>
    </svg>
  ),
  Card: ({ size = 20 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <rect x="3" y="5.5" width="18" height="13" rx="2.5" stroke="currentColor" strokeWidth="1.6"/>
      <path d="M3 10h18M7 15h4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  Mail: ({ size = 20 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <rect x="3" y="5.5" width="18" height="13" rx="2.5" stroke="currentColor" strokeWidth="1.6"/>
      <path d="m4 7 8 6 8-6" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/>
    </svg>
  ),
  Calendar: ({ size = 20 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <rect x="3.5" y="5" width="17" height="15.5" rx="2.5" stroke="currentColor" strokeWidth="1.6"/>
      <path d="M3.5 10h17M8 3v4M16 3v4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  ArrowLeft: ({ size = 18 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M15 5l-7 7 7 7" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  Minus: ({ size = 18 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M6 12h12" stroke="currentColor" strokeWidth="2" strokeLinecap="round"/>
    </svg>
  ),
  Plus: ({ size = 18 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M12 6v12M6 12h12" stroke="currentColor" strokeWidth="2" strokeLinecap="round"/>
    </svg>
  ),
};

const COVER_SRC = "/kalendarz/assets/okladka-2027.webp";
const ORDER_STORAGE_KEY = "zc_calendar_order_v1";

const FALLBACK_CONFIG = {
  productName: "Kalendarz ścienny 2027 — Jezioro Zegrzyńskie",
  unitAmount: 8900,
  shippingAmount: 1599,
  currency: "pln",
  maxQty: 5,
  shippingNote: "To przedsprzedaż. O wysyłce kalendarza poinformujemy Cię e-mailem.",
  salesOpen: false,
};

function formatMoney(grosze) {
  const value = Number(grosze || 0) / 100;
  return value.toLocaleString("pl-PL", {
    minimumFractionDigits: Number.isInteger(value) ? 0 : 2,
    maximumFractionDigits: 2,
  }) + "\u00A0zł";
}

const EMPTY_ORDER = { qty: 1, name: "", email: "", phone: "", locker: null };

function loadStoredOrder() {
  try {
    const raw = sessionStorage.getItem(ORDER_STORAGE_KEY);
    if (!raw) return { ...EMPTY_ORDER };
    return { ...EMPTY_ORDER, ...JSON.parse(raw) };
  } catch (_) {
    return { ...EMPTY_ORDER };
  }
}

function storeOrder(order) {
  try {
    sessionStorage.setItem(ORDER_STORAGE_KEY, JSON.stringify(order));
  } catch (_) {}
}

function clearStoredOrder() {
  try {
    sessionStorage.removeItem(ORDER_STORAGE_KEY);
  } catch (_) {}
}

// Te same reguły co validateOrder w api/_lib/calendar.js.
function normalizePolishPhone(value) {
  let digits = String(value || "").replace(/[^\d+]/g, "");
  if (digits.startsWith("+48")) digits = digits.slice(3);
  else if (digits.startsWith("0048")) digits = digits.slice(4);
  else if (digits.length === 11 && digits.startsWith("48")) digits = digits.slice(2);
  return /^[4-8]\d{8}$/.test(digits) ? `+48${digits}` : null;
}

function validateCustomer(order) {
  const errors = {};
  const name = order.name.replace(/\s+/g, " ").trim();
  if (name.length < 3 || !name.includes(" ")) errors.name = "Podaj imię i nazwisko.";
  if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(order.email.trim())) errors.email = "Podaj poprawny adres e-mail.";
  if (!normalizePolishPhone(order.phone)) errors.phone = "Podaj polski numer komórkowy (9 cyfr).";
  return errors;
}

function formatPhone(value) {
  const normalized = normalizePolishPhone(value);
  if (!normalized) return value;
  const d = normalized.slice(3);
  return `+48 ${d.slice(0, 3)} ${d.slice(3, 6)} ${d.slice(6)}`;
}

const CTA_STYLE = {
  background: "#ffcf33",
  color: "#151515",
  boxShadow: "inset 0 1px 0 rgba(255,255,255,0.55), 0 12px 32px rgba(255,207,51,0.28)",
};

const NEXT_STYLE = {
  background: "rgba(255,255,255,0.96)",
  color: "#0a0a0a",
  boxShadow: "inset 0 1px 0 rgba(255,255,255,0.8), 0 10px 28px rgba(255,255,255,0.12)",
};

const MONO_LABEL = {
  fontFamily: "'JetBrains Mono', monospace",
  fontSize: 11,
  letterSpacing: "0.26em",
  textTransform: "uppercase",
  color: "rgba(255,255,255,0.6)",
};

Object.assign(window, {
  CalIcon,
  COVER_SRC,
  CTA_STYLE,
  EMPTY_ORDER,
  FALLBACK_CONFIG,
  MONO_LABEL,
  NEXT_STYLE,
  clearStoredOrder,
  formatMoney,
  formatPhone,
  loadStoredOrder,
  normalizePolishPhone,
  storeOrder,
  validateCustomer,
});
