import {
  useLocation
} from "../chunk.3LWSN4DC.js";
import {
  AuthContext
} from "../chunk.IH25ZNBZ.js";
import "../chunk.IOTCVWDR.js";
import "../chunk.JNDOIKBN.js";
import "../chunk.DYBFOFDN.js";
import "../chunk.NFF22PP7.js";

// src/runtime/auth/index.ts
import {
  createElement,
  useContext
} from "react";
function useAuth() {
  const authContext = useContext(AuthContext);
  const location = useLocation();
  const redirectTo = locationToString(location);
  if (authContext.isAuthenticated) {
    return {
      ...authContext,
      createLoginUrl(options = {}) {
        const query = [`return_to=${encodeURIComponent(options.returnTo || redirectTo)}`];
        if (options.audience) {
          query.push(`audience=${encodeURIComponent(options.audience)}`);
        }
        if (Array.isArray(options.scope)) {
          query.push(`scope=${encodeURIComponent(options.scope.join(" "))}`);
        }
        return `${authContext.loginUrl}?${query.join("&")}`;
      },
      createLogoutUrl(options = {}) {
        const query = [`return_to=${encodeURIComponent(options.returnTo || redirectTo)}`];
        return `${authContext.loginUrl}?${query.join("&")}`;
      }
    };
  }
  return {
    ...authContext,
    createLoginUrl(options = {}) {
      const query = [`return_to=${encodeURIComponent(options.returnTo || redirectTo)}`];
      if (options.audience) {
        query.push(`audience=${encodeURIComponent(options.audience)}`);
      }
      if (Array.isArray(options.scope)) {
        query.push(`scope=${encodeURIComponent(options.scope.join(" "))}`);
      }
      return `${authContext.loginUrl}?${query.join("&")}`;
    }
  };
}
function useAuthorized(options) {
  const authContext = useContext(AuthContext);
  const location = useLocation();
  const redirectTo = locationToString(location);
  const query = [`return_to=${encodeURIComponent(redirectTo)}`];
  if (options?.audience) {
    query.push(`audience=${encodeURIComponent(options.audience)}`);
  }
  if (options?.scope) {
    query.push(`scope=${encodeURIComponent(options.scope.join(" "))}`);
  }
  if (!authContext.isAuthenticated) {
    return {
      ...authContext,
      isAuthorized: false
    };
  }
  if (typeof options?.audience === "string" && authContext.credentials.claims.aud !== options.audience) {
    return {
      ...authContext,
      isAuthorized: false
    };
  }
  const scopes = new Set(authContext.credentials.scope);
  if (Array.isArray(options?.scope) && !options?.scope.every((requiredScope) => scopes.has(requiredScope))) {
    return {
      ...authContext,
      isAuthorized: false
    };
  }
  return {
    ...authContext,
    isAuthorized: true
  };
}
function withAuthenticationRequired(Component, options = {}) {
  function handleFallback(authState) {
    const loginUrl = authState.createLoginUrl({
      audience: options.audience,
      scope: options.scope
    });
    if (options.fallback) {
      return options.fallback({loginUrl});
    }
    window.location.href = loginUrl;
    throw new Error("Invariant violation: Window expected to have synchronously navigated");
  }
  return function WithAuthenticationRequired(props) {
    const authState = useAuth();
    if (!authState.isAuthenticated) {
      return handleFallback(authState);
    }
    if (options.audience && options.audience !== authState.credentials.audience) {
      return handleFallback(authState);
    }
    if (Array.isArray(options.scope) && options.scope.length) {
      const accessTokenScopes = new Set(authState.credentials.scope);
      if (!options.scope.every((requiredScope) => accessTokenScopes.has(requiredScope))) {
        return handleFallback(authState);
      }
    }
    return createElement(Component, props);
  };
}
function locationToString(location) {
  const queryString = location.search ? `?${location.search}` : "";
  const hashString = location.hash ? `#${location.hash}` : "";
  return `${location.pathname || ""}${queryString}${hashString}`;
}
export {
  useAuth,
  useAuthorized,
  withAuthenticationRequired
};
