/* Cookie Banner — Rubin */
/* global React, ReactDOM, window */

const CookieBanner = () => {
  const [visible, setVisible] = React.useState(false);
  const lang = window.TWEAK_DEFAULTS?.lang || localStorage.rubin_lang || 'en';

  React.useEffect(() => {
    if (!localStorage.getItem('rubin_cookie_v1')) {
      setVisible(true);
    }
  }, []);

  const save = (val) => {
    localStorage.setItem('rubin_cookie_v1', val);
    setVisible(false);
  };

  if (!visible) return null;

  const content = {
    en: {
      msg: "We use a single localStorage entry to remember this choice. No analytics, no tracking. ",
      link: "Learn more",
      ok: "Accept",
      no: "Reject"
    },
    pl: {
      msg: "Zapisujemy Twoją decyzję w jednym wpisie localStorage. Bez analityki, bez śledzenia. ",
      link: "Dowiedz się więcej",
      ok: "Zaakceptuj",
      no: "Odrzuć"
    }
  }[lang] || { msg: "", link: "", ok: "", no: "" };

  return (
    <div className="cookie-banner">
      <div className="cookie-banner-content">
        <span>
          {content.msg}
          <a href="/cookies.html">{content.link}</a>
        </span>
        <div className="cookie-banner-btns">
          <button className="cookie-btn cookie-btn-subtle" onClick={() => save('rejected')}>{content.no}</button>
          <button className="cookie-btn cookie-btn-primary" onClick={() => save('accepted')}>{content.ok}</button>
        </div>
      </div>
    </div>
  );
};

window.CookieBanner = CookieBanner;

// Auto-mount when loaded (Babel-standalone executes this after React is available)
(function() {
  const mount = () => {
    let el = document.getElementById('cookie-banner-root');
    if (!el) {
      el = document.createElement('div');
      el.id = 'cookie-banner-root';
      document.body.appendChild(el);
    }
    const root = ReactDOM.createRoot(el);
    root.render(<CookieBanner />);
  };

  if (document.readyState === 'complete') {
    mount();
  } else {
    window.addEventListener('load', mount);
  }
})();
