// Cookie consent — rounded white card anchored bottom-centre, shown on first
// load. Persists the choice in localStorage so it never reappears after the
// visitor accepts or declines. Brand colours and fonts only.
const COOKIE_KEY = "nourrir.cookieConsent";

function CookieConsent() {
  const [open, setOpen] = React.useState(false);

  React.useEffect(() => {
    let choice = null;
    try { choice = localStorage.getItem(COOKIE_KEY); } catch (e) {}
    if (!choice) {
      // small delay so the card eases in after the page settles
      const t = setTimeout(() => setOpen(true), 900);
      return () => clearTimeout(t);
    }
  }, []);

  const decide = (value) => {
    try { localStorage.setItem(COOKIE_KEY, value); } catch (e) {}
    setOpen(false);
  };

  if (!open) return null;

  return (
    <React.Fragment>
      <div className="cookie-scrim" aria-hidden="true"/>
      <div className="cookie" role="dialog" aria-modal="true" aria-label="Cookie consent" aria-live="polite">
      <div className="cookie__top">
        <span className="cookie__icon" aria-hidden="true"><Icon name="cookie" size={22}/></span>
        <button className="cookie__manage" onClick={() => decide("managed")}>Manage preferences</button>
      </div>

      <h3 className="cookie__title">This website uses cookies</h3>
      <p className="cookie__body">
        We use cookies to improve your experience and to analyse traffic on our
        site. You can accept all cookies or decline non-essential ones.
      </p>

      <div className="cookie__actions">
        <button className="cookie__btn" onClick={() => decide("accepted")}>Accept all</button>
        <button className="cookie__btn" onClick={() => decide("essential")}>Essential only</button>
      </div>
      </div>
    </React.Fragment>
  );
}
window.CookieConsent = CookieConsent;
