'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-8277cbb3.js');
const domContextHooks_module = require('./dom-context-hooks.module-e7b6727c.js');
const index_module = require('./index.module-f8c8495f.js');

function usePortalProtectedRoute({ requireEmailVerification, redirectTo, redirectToUnverified, }) {
  var _a;
  const userIdent = index_module.J();
  const authenticated = !!(userIdent === null || userIdent === void 0 ? void 0 : userIdent.jwt);
  const emailVerified = (_a = userIdent === null || userIdent === void 0 ? void 0 : userIdent.managedIdentity) === null || _a === void 0 ? void 0 : _a.emailVerified;
  const { pathname, search } = index_module.kn();
  const nextPageParam = new URLSearchParams();
  nextPageParam.append("nextPage", `${pathname}${search}`);
  domContextHooks_module.useEffect(() => {
    if (!authenticated) {
      return index_module.jn.push({
        pathname: redirectTo,
        search: "?" + nextPageParam.toString(),
      });
    }
    if (requireEmailVerification && !emailVerified) {
      return index_module.jn.push({
        pathname: redirectToUnverified || redirectTo,
        search: "?" + nextPageParam.toString(),
      });
    }
  }, []);
  return !!userIdent;
}

const PortalProtectedRoute = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.ignored = true;
    /**
     * Redirect participants to this page they are not logged in.
     *
     * @uiName Unauthenticated participant redirect
     */
    this.redirectTo = "/";
    /**
     * @uiName Require email verification
     */
    this.requireEmailVerification = false;
    /**
     * Redirect participants to this page when email verification is required but they have not verified their email.
     *
     * @uiName Unverified participant redirect
     */
    this.redirectToUnverified = "/emailVerification";
    domContextHooks_module.h$1(this);
  }
  disconnectedCallback() { }
  render() {
    const hasUser = index_module.isDemo()
      ? usePortalProtectedRouteDemo()
      : usePortalProtectedRoute(this);
    return (index.h(index.Host, { style: { display: hasUser ? "contents" : "none" } }, index.h("slot", null)));
  }
};
function usePortalProtectedRouteDemo({}) {
  return true;
}

exports.sqm_portal_protected_route = PortalProtectedRoute;
