import type * as common from "@esri/solution-common";
import type Portal from "@arcgis/core/portal/Portal.js";
import type identityCredential from "@arcgis/core/identity/Credential.js";
import type { IOrgSettings } from "./interfaces.js";

export class Auth {
  constructor(clientId?: string, portalURL?: string, locale?: string, signInParam?: string);
  get CanDeleteSolution(): boolean;
  get CanSearchArcGISOnline(): boolean;
  get CanUpdateSolution(): boolean;
  get CanUseAI(): boolean;
  get CanViewOrg(): boolean;
  get Credential(): identityCredential | undefined;
  get IsEnterprisePortal(): boolean;
  get IsSignedIn(): boolean;
  get Portal(): Portal;
  get PortalSubset(): IPortalSubset | undefined;
  get PortalUrl(): string;
  /**
   * Enriched user privileges, pre-computed once at sign-in.
   *
   * Includes the base portal privileges plus custom solution-specific
   * privileges (Velocity, Notebook Server, Hub Premium, Location Tracking,
   * Workflow Manager) that would otherwise require additional network
   * requests each time a solution details page is opened. Falls back to
   * the base portal privileges if enrichment hasn't completed yet (e.g.,
   * during sign-in or for anonymous sessions).
   */
  get PortalUserEnrichedPrivileges(): string[];
  get PortalUserName(): string;
  get PortalUserOrg(): string | undefined;
  get PortalUserPrivileges(): string[];
  get PortalUserSubset(): IPortalUserSubset | undefined;
  get RestUserSession(): common.UserSession;
  get SignInParam(): string | undefined;
  /**
   * Checks if the ArcGIS Online group is reachable for Enterprise portals.
   *
   * @param agoBasedEnterpriseSolutionsGroupId
   */
  checkArcGISOnlineReachability(agoBasedEnterpriseSolutionsGroupId?: string): Promise<void>;
  destroy(): void;
  /**
   * Initializes the Auth object. This method must be called before any other Auth methods.
   * If the user is not authorized to use the app, the page will be redirected to an error page.
   *
   * @param isEnterprise
   * @param agoBasedEnterpriseSolutionsGroupId
   * @returns Promise that resolves with the initialized Auth object
   */
  initialize(isEnterprise?: boolean, agoBasedEnterpriseSolutionsGroupId?: string): Promise<Auth>;
  /** Refreshes the user session token */
  refreshSession(): Promise<common.UserSession> | undefined;
  /**
   * Signs into app and refreshes the window.location or switches to the supplied URL.
   *
   * @param signInParamValue - If supplied, the parameter `signInParam` with the supplied value
   * will be added to the redirect uri's search params
   */
  signIn(signInParamValue?: string): Promise<void>;
  /**
   * Signs out of app.
   * When the user is...
   *  on the solutions page they should stay on that page
   *  on a solutions details page they should stay on that page
   *  on the deployments page they should stay on that page
   *  on a deployed solutions page they should be sent to the deployments page
   *  on the resources page they should stay on that page
   */
  signOut(): void;
}

export interface IPortalSubset {
  name: string;
  id: string;
  restUrl: string;
  portalUrl: string;
  urlKey: string;
  hostName: string;
  orgUrl: string;
  locationTrackingEnabled: boolean;
  hubPremiumEnabled: boolean;
  workflowURL: string;
  isReadOnly: boolean;
  orgSettings: IOrgSettings;
}

export interface IPortalUserSubset {
  fullName: string;
  username: string;
  culture: string;
  thumbnailUrl: string;
  privileges: string[];
  orgId: string;
  role: string;
  roleId?: string;
}