/* ScreenGuide — launches the SAME spotlight TourGuide for each
   screen on first visit. Kept as a thin controller so App stays clean. */
(function() {
  var useState = React.useState;
  var useEffect = React.useEffect;

  function ScreenGuide(props) {
    var screen = props.screen;
    var user = props.user;
    var welcomeBusy = props.welcomeBusy;
    var _tourId = useState(null);
    var tourId = _tourId[0], setTourId = _tourId[1];

    useEffect(function() {
      setTourId(null);
      if (welcomeBusy) return;
      if (!screen || !window.CBTour || !window.CB_TOURS) return;
      if (window.CBTour.shouldRun(user)) return; // welcome first
      if (!window.CB_TOURS[screen]) return;
      if (!window.CBTour.shouldShowScreen(screen, user)) return;

      var t = setTimeout(function() { setTourId(screen); }, 700);
      return function() { clearTimeout(t); };
    }, [screen, welcomeBusy, user && (user._id || user.email)]);

    if (!tourId || !window.CBTourGuide) return null;

    return React.createElement(window.CBTourGuide, {
      tourId: tourId,
      user: user,
      onClose: function() {
        setTourId(null);
        if (props.onClose) props.onClose();
      },
    });
  }

  window.CBScreenGuide = ScreenGuide;
})();
