/* Globby Wizard — internal TEAM access gate.
   A team member opens the app at the /team slug (or ?team / #team), enters the
   password from GLOBBY_CONFIG.team, and everything unlocks for that browser
   (window.setTeam(true) → window.isGated() returns false everywhere). */

// Was the app opened at the team entry point? Path "/team", a "team" query param,
// or a "#team" hash all count — the query/hash forms make it work locally and on
// any static host where the /team rewrite isn't in play.
window.teamRouteRequested = function () {
  try {
    var slug = (window.GLOBBY_CONFIG && window.GLOBBY_CONFIG.team && window.GLOBBY_CONFIG.team.slug) || "team";
    slug = String(slug).toLowerCase();
    var path = (location.pathname || "").replace(/\/+$/, "").toLowerCase();
    if (path === "/" + slug) return true;
    var sp = new URLSearchParams(location.search || "");
    if (sp.has(slug)) return true;
    var hash = (location.hash || "").replace(/^#/, "").toLowerCase();
    if (hash === slug) return true;
    return false;
  } catch (e) { return false; }
};

// Drop the /team (or ?team / #team) marker from the address bar without a reload,
// so a shared/bookmarked link doesn't keep re-opening the gate after unlock.
window.teamCleanUrl = function () {
  try { if (window.history && history.replaceState) history.replaceState(null, "", "/"); } catch (e) {}
};

function TeamGate(props) {
  const { L, lang, onUnlock } = props;
  const T = (L && L.team) || {};
  const [pw, setPw] = useState("");
  const [err, setErr] = useState(false);

  const submit = function () {
    const expected = (window.GLOBBY_CONFIG && window.GLOBBY_CONFIG.team && window.GLOBBY_CONFIG.team.password) || "";
    if (pw && pw === expected) {
      window.setTeam && window.setTeam(true);
      window.teamCleanUrl && window.teamCleanUrl();
      window.track && window.track("team_unlocked", { lang: lang });
      onUnlock && onUnlock();
      return;
    }
    setErr(true);
  };

  return React.createElement("div", { className: "contact-overlay" },
    React.createElement("div", { className: "contact-modal", style: { maxWidth: 400 } },
      React.createElement("div", { className: "contact-modal-head" },
        React.createElement("div", { className: "hub-badge", style: { margin: "0 auto 10px", background: "rgba(255,255,255,.16)", color: "#fff" } },
          React.createElement(GIcon, { name: "lock", size: 26 })),
        React.createElement("h3", { style: { margin: "0 0 6px", fontSize: 20, fontWeight: 800, color: "#fff" } }, T.title || "Team access"),
        React.createElement("p", { style: { margin: 0, fontSize: 14, color: "rgba(255,255,255,.8)", lineHeight: 1.5 } }, T.subtitle || "Enter the team password to unlock all features.")),
      React.createElement("div", { className: "contact-modal-body" },
        React.createElement("div", { className: "field" },
          React.createElement("label", null, T.label || "Password"),
          React.createElement("input", {
            className: "inp", type: "password", value: pw, autoFocus: true, autoComplete: "off",
            placeholder: T.placeholder || "••••••••",
            onChange: function (e) { setPw(e.target.value); if (err) setErr(false); },
            onKeyDown: function (e) { if (e.key === "Enter") { e.preventDefault(); submit(); } },
          })),
        err ? React.createElement("p", { style: { margin: "2px 2px 6px", fontSize: 13, color: "#DC2626", display: "flex", alignItems: "center", gap: 6 } },
          React.createElement(GIcon, { name: "x", size: 14 }), T.wrong || "Wrong password.") : null),
      React.createElement("div", { className: "contact-modal-foot" },
        React.createElement("button", { className: "btn btn-primary", style: { width: "100%" }, onClick: submit },
          React.createElement(GIcon, { name: "lock", size: 16, stroke: 2.4 }), T.submit || "Unlock"))));
}
window.TeamGate = TeamGate;
