/* primitives.jsx — shared UI atoms, icons, palette tokens, the book-cover SVG */

const PALETTES = {
  terracotta: {
    name: "טרקוטה וכחול דיו",
    bg: "#F4EEDF",      // warm parchment
    surface: "#FAF6EB", // soft cream card
    ink: "#1C1815",     // deep warm black
    ink2: "#4A3F36",    // muted brown
    accent: "#B85738",  // terracotta
    accent2: "#1F2D4A", // deep navy
    gold: "#C9A24E",
    rose: "#E5C7B6",
    line: "rgba(28,24,21,.12)",
  },
  sage: {
    name: "מרווה וזהב",
    bg: "#F1EEE6",
    surface: "#F8F5EC",
    ink: "#171815",
    ink2: "#3D4239",
    accent: "#4A6147",
    accent2: "#1B1F1A",
    gold: "#B8945A",
    rose: "#D9C7A8",
    line: "rgba(23,24,21,.12)",
  },
  rose: {
    name: "רוז׳ ושוקולד",
    bg: "#F6EDE7",
    surface: "#FBF3EE",
    ink: "#2B1C18",
    ink2: "#5A3F35",
    accent: "#A04A3B",
    accent2: "#3A1F1A",
    gold: "#C58F5A",
    rose: "#E8B7A0",
    line: "rgba(43,28,24,.12)",
  },
  ink: {
    name: "דיו לילה",
    bg: "#171716",
    surface: "#1F1F1D",
    ink: "#F2EDE2",
    ink2: "#A39E92",
    accent: "#E5894D",
    accent2: "#F2EDE2",
    gold: "#D9B574",
    rose: "#3A2E26",
    line: "rgba(242,237,226,.16)",
  },
};

const FONT_PAIRS = {
  editorial:  { name: "אדיטוריאל",  display: "Frank Ruhl Libre", body: "Assistant" },
  literary:   { name: "ספרותי",     display: "Noto Serif Hebrew", body: "Heebo" },
  modern:     { name: "מודרני",     display: "Rubik",            body: "Rubik" },
  classic:    { name: "קלאסי",      display: "Frank Ruhl Libre", body: "Heebo" },
};

/* ─────────── Icons (stroke-based, 1.6px) ─────────── */
const Icon = ({ d, size = 20, stroke = "currentColor", strokeWidth = 1.6, fill = "none", children }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={fill} stroke={stroke} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
    {children || (d && <path d={d} />)}
  </svg>
);
const IconHeart = (p) => <Icon {...p}><path d="M12 20s-7-4.35-7-10a4 4 0 0 1 7-2.65A4 4 0 0 1 19 10c0 5.65-7 10-7 10z"/></Icon>;
const IconChat  = (p) => <Icon {...p}><path d="M4 6a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-5l-4 3v-3H6a2 2 0 0 1-2-2V6z"/><path d="M8 9h8M8 12h5"/></Icon>;
const IconShield= (p) => <Icon {...p}><path d="M12 3l8 3v6c0 5-3.5 8.5-8 9-4.5-.5-8-4-8-9V6l8-3z"/><path d="M9 12l2 2 4-4"/></Icon>;
const IconSpark = (p) => <Icon {...p}><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"/></Icon>;
const IconBag   = (p) => <Icon {...p}><path d="M6 8h12l-1 12H7L6 8z"/><path d="M9 8a3 3 0 0 1 6 0"/></Icon>;
const IconSearch= (p) => <Icon {...p}><circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5"/></Icon>;
const IconMenu  = (p) => <Icon {...p}><path d="M4 7h16M4 12h16M4 17h16"/></Icon>;
const IconClose = (p) => <Icon {...p}><path d="M5 5l14 14M19 5L5 19"/></Icon>;
const IconArrowL= (p) => <Icon {...p}><path d="M19 12H5M11 6l-6 6 6 6"/></Icon>; /* points left = "next" in RTL */
const IconArrowR= (p) => <Icon {...p}><path d="M5 12h14M13 6l6 6-6 6"/></Icon>;
const IconPlus  = (p) => <Icon {...p}><path d="M12 5v14M5 12h14"/></Icon>;
const IconCheck = (p) => <Icon {...p}><path d="M5 12l4 4 10-10"/></Icon>;
const IconQuote = (p) => <Icon size={p.size||32} stroke="none" fill={p.fill||"currentColor"}><path d="M9 11H5.5C4 11 3 12 3 13.5V17c0 1.5 1 2.5 2.5 2.5H8c1 0 1.5-.5 1.5-1.5V13c0-.5 0-1-.5-1.5-.5-.5-.5-1.5 0-2 0-1.5-.5-3-2-4L6 6c2 .5 3 2 3 5zM20 11h-3.5c-1.5 0-2.5 1-2.5 2.5V17c0 1.5 1 2.5 2.5 2.5H19c1 0 1.5-.5 1.5-1.5V13c0-.5 0-1-.5-1.5-.5-.5-.5-1.5 0-2 0-1.5-.5-3-2-4L17 6c2 .5 3 2 3 5z"/></Icon>;
const IconStar  = (p) => <Icon {...p} stroke="none" fill="currentColor"><path d="M12 2l2.9 6.6 7.1.7-5.4 4.8 1.6 7-6.2-3.7L5.8 21l1.6-7L2 9.3l7.1-.7L12 2z"/></Icon>;
const IconWa    = (p) => {
  const size = p.size || 20;
  const fill = p.fill || "currentColor";
  return (
    <svg width={size} height={size} viewBox="0 0 32 32" fill={fill} aria-hidden="true">
      <path d="M16.003 0C7.174 0 .005 7.169.005 15.998c0 2.823.737 5.586 2.141 8.018L0 32l8.197-2.146a16.07 16.07 0 0 0 7.799 2c8.829.004 15.999-7.165 16.001-15.994.004-8.83-7.165-16.001-15.994-16.001v-0.143zm0 29.281c-2.428 0-4.806-.65-6.886-1.883l-.494-.295-5.116 1.34 1.366-4.99-.322-.515a13.252 13.252 0 0 1-2.029-7.082C2.522 8.564 8.567 2.519 16 2.519c3.6 0 6.985 1.404 9.527 3.952a13.398 13.398 0 0 1 3.95 9.535c-.003 7.434-6.048 13.478-13.474 13.478z"/>
      <path d="M23.39 19.122c-.404-.203-2.393-1.181-2.764-1.317-.371-.135-.64-.203-.91.203-.269.405-1.042 1.316-1.279 1.587-.236.27-.471.304-.876.101-.404-.203-1.708-.629-3.253-2.008-1.202-1.072-2.014-2.397-2.25-2.802-.236-.405-.025-.624.178-.826.183-.181.404-.473.607-.71.203-.236.27-.405.405-.674.135-.27.067-.507-.034-.71-.101-.202-.91-2.193-1.247-3.003-.328-.787-.661-.68-.91-.693-.236-.012-.504-.014-.774-.014a1.485 1.485 0 0 0-1.078.506c-.371.405-1.415 1.383-1.415 3.374s1.449 3.914 1.65 4.184c.203.27 2.85 4.353 6.905 6.106 2.965 1.281 4.131 1.387 5.612 1.165.902-.135 2.781-1.137 3.171-2.234.39-1.098.39-2.039.27-2.234-.118-.219-.437-.337-.842-.54z"/>
    </svg>
  );
};
const IconMail  = (p) => <Icon {...p}><path d="M4 6h16v12H4z"/><path d="M4 7l8 6 8-6"/></Icon>;
const IconPhone = (p) => <Icon {...p}><path d="M5 4h3l2 5-2 1a12 12 0 0 0 6 6l1-2 5 2v3a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2z"/></Icon>;
const IconInsta = (p) => <Icon {...p}><rect x="3" y="3" width="18" height="18" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.5" cy="6.5" r="1" fill="currentColor" stroke="none"/></Icon>;
const IconBook  = (p) => <Icon {...p}><path d="M4 5a2 2 0 0 1 2-2h13v16H6a2 2 0 0 0-2 2V5z"/><path d="M4 19a2 2 0 0 1 2-2h13"/></Icon>;
const IconCards = (p) => <Icon {...p}><rect x="5" y="6" width="11" height="14" rx="2" transform="rotate(-6 10.5 13)"/><rect x="9" y="4" width="11" height="14" rx="2" transform="rotate(6 14.5 11)"/></Icon>;
const IconGroup = (p) => <Icon {...p}><circle cx="9" cy="9" r="3"/><circle cx="17" cy="10" r="2.5"/><path d="M3 19a6 6 0 0 1 12 0"/><path d="M14 19a5 5 0 0 1 7 0"/></Icon>;
const IconGift  = (p) => <Icon {...p}><path d="M3 9h18v4H3z"/><path d="M5 13v8h14v-8M12 9v12"/><path d="M12 9c-2 0-4-1-4-3s2-3 4 0c2-3 4-2 4 0s-2 3-4 3z"/></Icon>;
const IconChevD = (p) => <Icon {...p}><path d="M6 9l6 6 6-6"/></Icon>;

/* ─────────── Book cover (real image) ─────────── */
function BookCover({ width = 260, palette, rotate = 0, style = {} }) {
  const w = width, h = width * 1.5;
  return (
    <img
      src="/assets/book-cover.jpg"
      alt="ספר מנהיגות מלידה"
      width={w}
      height={h}
      style={{
        width: w, height: "auto", maxWidth: "100%",
        filter: "drop-shadow(0 30px 50px rgba(28,24,21,.35))",
        transform: `rotate(${rotate}deg)`,
        ...style,
      }}
    />
  );
}

/* ─────────── Buttons ─────────── */
function PrimaryBtn({ children, onClick, palette, size="md", as, href, ...rest }){
  const pad = size==="lg" ? "17px 32px" : size==="sm" ? "9px 18px" : "13px 26px";
  const fs  = size==="lg" ? 14.5 : size==="sm" ? 12.5 : 13.5;
  const Tag = as || (href ? "a" : "button");
  return (
    <Tag onClick={onClick} href={href} {...(Tag === "button" ? { type:"button" } : {})} {...rest} style={{
      display:"inline-flex", alignItems:"center", gap:10, padding:pad, fontSize:fs, fontWeight:500, letterSpacing:".04em",
      background: palette.ink, color: palette.bg, border:0, borderRadius:2,
      cursor:"pointer",
      transition:"transform .15s ease, background .3s ease, letter-spacing .3s ease",
    }}
    onMouseEnter={e=>{ e.currentTarget.style.background = palette.accent; e.currentTarget.style.letterSpacing = ".08em"; }}
    onMouseLeave={e=>{ e.currentTarget.style.background = palette.ink; e.currentTarget.style.letterSpacing = ".04em"; }}
    onMouseDown={e=>e.currentTarget.style.transform="scale(.98)"}
    onMouseUp={e=>e.currentTarget.style.transform="none"}
    >{children}</Tag>
  );
}
function GhostBtn({ children, onClick, palette, size="md", as, href, ...rest }){
  const pad = size==="lg" ? "16px 30px" : size==="sm" ? "8px 16px" : "12px 24px";
  const fs  = size==="lg" ? 14.5 : size==="sm" ? 12.5 : 13.5;
  const Tag = as || (href ? "a" : "button");
  return (
    <Tag onClick={onClick} href={href} {...(Tag === "button" ? { type:"button" } : {})} {...rest} style={{
      display:"inline-flex", alignItems:"center", gap:10, padding:pad, fontSize:fs, fontWeight:500, letterSpacing:".04em",
      background:"transparent", color: palette.ink, border:`1px solid ${palette.ink}`, borderRadius:2,
      cursor:"pointer",
      transition:"all .3s ease",
    }}
    onMouseEnter={e=>{ e.currentTarget.style.background = palette.ink; e.currentTarget.style.color = palette.bg; }}
    onMouseLeave={e=>{ e.currentTarget.style.background = "transparent"; e.currentTarget.style.color = palette.ink; }}
    >{children}</Tag>
  );
}
function LinkArrow({ children, palette, color }){
  return (
    <span style={{display:"inline-flex", alignItems:"center", gap:6, color: color || palette.accent, fontWeight:600, borderBottom:`1px solid currentColor`, paddingBottom:2}}>
      {children}
      <IconArrowL size={14} />
    </span>
  );
}

/* ─────────── Section eyebrow ─────────── */
function Eyebrow({ children, palette, color, mark }){
  const c = color || palette.accent;
  return (
    <div className="eyebrow" style={{ color: c, display:"inline-flex", alignItems:"center", gap:12 }}>
      <span style={{ width:36, height:1, background: c, display:"inline-block", opacity:.55 }}/>
      <span style={{ width:3, height:3, borderRadius:999, background: c, display:"inline-block" }}/>
      {mark}
      {children}
    </div>
  );
}

/* ─────────── Pill chip ─────────── */
function Chip({ children, palette, tone="default" }){
  const styles = tone==="solid"
    ? { background: palette.ink, color: palette.bg }
    : tone==="gold"
    ? { background: "transparent", color: palette.ink, border:`1px solid ${palette.gold}` }
    : { background: "transparent", color: palette.ink2, border:`1px solid ${palette.line}` };
  return (
    <span style={{ display:"inline-flex", alignItems:"center", gap:6, padding:"6px 12px", borderRadius:999, fontSize:12.5, fontWeight:500, ...styles }}>{children}</span>
  );
}

/* ─────────── Photo placeholder ─────────── */
function PhotoPlaceholder({ label, ratio = "4/5", palette, tone="rose", style={} }){
  const bg = tone==="rose" ? palette.rose : tone==="navy" ? palette.accent2 : tone==="gold" ? palette.gold : palette.ink;
  const fg = tone==="rose" ? palette.accent2 : palette.bg;
  return (
    <div style={{
      width:"100%", aspectRatio: ratio, background: bg, color: fg,
      borderRadius: 4, position:"relative", overflow:"hidden", ...style,
    }}>
      <svg viewBox="0 0 200 250" preserveAspectRatio="xMidYMid slice" style={{ position:"absolute", inset:0, width:"100%", height:"100%", opacity:.4 }}>
        <defs>
          <pattern id={`ph-${label}`} width="14" height="14" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
            <line x1="0" y1="0" x2="0" y2="14" stroke={fg} strokeWidth=".6"/>
          </pattern>
        </defs>
        <rect width="200" height="250" fill={`url(#ph-${label})`} opacity=".4"/>
      </svg>
      <div style={{ position:"absolute", inset:0, display:"flex", flexDirection:"column", justifyContent:"flex-end", padding:"14px 16px" }}>
        <div style={{ fontSize:10, opacity:.7, letterSpacing:".15em", textTransform:"uppercase" }}>תמונה</div>
        <div style={{ fontSize:14, fontWeight:600 }}>{label}</div>
      </div>
    </div>
  );
}

/* ─────────── Reveal-on-scroll wrapper ─────────── */
function Reveal({ children, delay = 0, y = 14, once = true, as: Tag = "div", style = {}, ...rest }) {
  const ref = React.useRef(null);
  const [shown, setShown] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el || typeof IntersectionObserver === "undefined") { setShown(true); return; }
    const io = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) {
        setShown(true);
        if (once) io.disconnect();
      } else if (!once) {
        setShown(false);
      }
    }, { threshold: 0.15, rootMargin: "0px 0px -40px 0px" });
    io.observe(el);
    return () => io.disconnect();
  }, [once]);
  return (
    <Tag
      ref={ref}
      {...rest}
      style={{
        ...style,
        opacity: shown ? 1 : 0,
        transform: shown ? "translateY(0)" : `translateY(${y}px)`,
        transition: `opacity .7s ease ${delay}ms, transform .8s cubic-bezier(.2,.8,.2,1) ${delay}ms`,
        willChange: "opacity, transform",
      }}
    >
      {children}
    </Tag>
  );
}

/* ─────────── Helpers ─────────── */
const formatILS = (n) => `₪${n.toLocaleString("he-IL")}`;

/* rich text — content edited in /admin can mark a word with **two asterisks**
   to have it emphasised. Anything else is rendered as plain text. */
function rt(text, color, underline) {
  if (text == null) return null;
  return String(text).split("**").map((part, i) =>
    i % 2
      ? <em key={i} style={{
          fontStyle: "italic", fontWeight: 600, color: color || "inherit",
          borderBottom: underline ? `1.5px solid ${color || "currentColor"}` : "none",
        }}>{part}</em>
      : <React.Fragment key={i}>{part}</React.Fragment>
  );
}

/* icons pickable from /admin, by name */
const ICONS_BY_NAME = {
  heart: IconHeart, chat: IconChat, spark: IconSpark, shield: IconShield,
  gift: IconGift, book: IconBook, cards: IconCards, group: IconGroup, star: IconStar,
};
const iconByName = (name) => ICONS_BY_NAME[name] || IconHeart;

/* publish */
Object.assign(window, {
  PALETTES, FONT_PAIRS,
  Icon, IconHeart, IconChat, IconShield, IconSpark, IconBag, IconSearch, IconMenu, IconClose,
  IconArrowL, IconArrowR, IconPlus, IconCheck, IconQuote, IconStar, IconWa, IconMail, IconPhone, IconInsta,
  IconBook, IconCards, IconGroup, IconGift, IconChevD,
  BookCover, PrimaryBtn, GhostBtn, LinkArrow, Eyebrow, Chip, PhotoPlaceholder, formatILS, Reveal,
  rt, iconByName,
});
