import { SoftrUserGroupId, User } from "../auth/user.js";
import { SoftrContext } from "../auth/context.js";

//#region src/route/visibility.d.ts
type AudienceSet = readonly ["NON_LOGGED_IN_USERS"] | readonly ["LOGGED_IN_USERS"] | readonly ["NON_LOGGED_IN_USERS", "LOGGED_IN_USERS"] | readonly ["LOGGED_IN_USERS", "NON_LOGGED_IN_USERS"];
/**
 * Who may see a route or region. `customGroups` narrows only the
 * `LOGGED_IN_USERS` half.
 *
 * @example
 * { userGroupTypes: ["NON_LOGGED_IN_USERS", "LOGGED_IN_USERS"] }
 * { userGroupTypes: ["NON_LOGGED_IN_USERS"] }
 * { userGroupTypes: ["LOGGED_IN_USERS"], customGroups: ["…"] }
 * { userGroupTypes: ["NON_LOGGED_IN_USERS", "LOGGED_IN_USERS"], customGroups: ["…"] }
 */
type VisibilityRule = {
  userGroupTypes: AudienceSet;
  customGroups?: ReadonlyArray<SoftrUserGroupId>;
};
declare const visibilityBrand: unique symbol;
/** A rule {@link defineVisibility} has produced. */
type Visibility<R extends VisibilityRule = VisibilityRule> = R & {
  readonly [visibilityBrand]: "visibility";
};
/**
 * Why a visitor was denied.
 *
 * - `unauthenticated` — the route needs a signed-in visitor and there is none.
 * - `forbidden` — signed in, but in none of the groups the route names.
 * - `alreadyAuthenticated` — the route is for signed-out visitors only.
 */
declare const ACCESS_REASONS: readonly ["unauthenticated", "forbidden", "alreadyAuthenticated"];
type AccessReason = (typeof ACCESS_REASONS)[number];
type AccessCheck = {
  ok: true;
} | {
  ok: false;
  reason: AccessReason;
};
/** The one field of {@link SoftrContext} every access check reads. */
type AccessContext = Pick<SoftrContext, "user">;
/** The decision and, when a visitor is denied, the reason. Throws nothing. */
declare function checkAccess(visibility: Visibility, context: AccessContext): AccessCheck;
/** Narrows `context.user` only for a rule naming exactly one audience; naming both narrows to neither. */
type Narrowed<R extends VisibilityRule> = R["userGroupTypes"] extends ReadonlyArray<infer T> ? [T] extends ["LOGGED_IN_USERS"] ? AccessContext & {
  user: User;
} : [T] extends ["NON_LOGGED_IN_USERS"] ? AccessContext & {
  user: null;
} : AccessContext : AccessContext;
/**
 * Declares who may see a region of the page. A route's own rule is the
 * `visibility` of its `definePage`.
 *
 * @example
 * const staffOnly = defineVisibility({ userGroupTypes: ["LOGGED_IN_USERS"], customGroups: ["…"] })
 *
 * <Audience visibility={staffOnly} fallback={null}>…</Audience>
 */
declare function defineVisibility<const R extends VisibilityRule>(rule: R): Visibility<R>;
/**
 * Whether the visitor passes the rule, narrowing `context.user` where the rule
 * allows it. Throws nothing, so it is safe in a component.
 */
declare function isAuthorized<R extends VisibilityRule>(visibility: Visibility<R>, context: AccessContext): context is Narrowed<R>;
//#endregion
export { AccessContext, AccessReason, Narrowed, Visibility, VisibilityRule, checkAccess, defineVisibility, isAuthorized };