import type { EventedAccessor } from "../core/Evented.js";

export interface CredentialProperties extends Partial<Pick<Credential, "expires" | "isAdmin" | "oAuthState" | "scope" | "server" | "ssl" | "token" | "userId">> {}

export interface CredentialEvents {
  /** Fires when the token associated with the credential is updated or changed. */
  "token-change": void;
  /**
   * Fires when a credential object is destroyed. After the credential is
   * destroyed, remove any map layers that use this credential. Any tokens
   * generated via OAuth will automatically be revoked.
   *
   * @see [IdentityManagerBase.destroyCredentials()](https://developers.arcgis.com/javascript/latest/references/core/identity/IdentityManagerBase/#destroyCredentials)
   */
  destroy: void;
}

/**
 * Represents a credential object used to access a secure ArcGIS resource.
 *
 * @since 4.0
 * @see [Access ArcGIS Online items using OAuth 2.0](https://developers.arcgis.com/javascript/latest/sample-code/identity-oauth-basic/)
 * @see [Access Secure Services](https://developers.arcgis.com/javascript/latest/secure-resources/)
 * @see [Authentication and OAuth 2](https://developers.arcgis.com/authentication/)
 * @see [Guide topic - Proxy pages](https://developers.arcgis.com/javascript/latest/proxies/)
 * @see [ArcGIS Organization portals](https://developers.arcgis.com/javascript/latest/arcgis-organization-portals/)
 */
export default class Credential extends EventedAccessor {
  /**
   * @deprecated
   * Do not directly reference this property.
   * Use EventNames and EventTypes helpers from \@arcgis/core/Evented
   */
  "@eventTypes": CredentialEvents;
  constructor(properties?: CredentialProperties);
  /**
   * Token expiration time specified as number of milliseconds since 1 January 1970 00:00:00 UTC.
   *
   * @see [Date - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
   */
  accessor expires: number | null;
  /** Indicates that this credential was created to access the [ArcGIS Server Administrator REST API](https://developers.arcgis.com/rest/enterprise-administration/server/overview/). */
  accessor isAdmin: boolean;
  /**
   * The Identity Manager's
   * [IdentityManagerBase.setOAuthRedirectionHandler()](https://developers.arcgis.com/javascript/latest/references/core/identity/IdentityManagerBase/#setOAuthRedirectionHandler)
   * returns an object that contains a `state` property. This information is returned for this property.
   */
  accessor oAuthState: any;
  /** The scope of the credential. */
  accessor scope: "portal" | "server";
  /** The server url. */
  accessor server: string;
  /** Indicates whether the resources accessed using this credential should be fetched over HTTPS protocol. */
  accessor ssl: boolean;
  /** Token generated by the token service using the specified userId and password. */
  accessor token: string;
  /** User associated with the Credential object. */
  accessor userId: string;
  /**
   * Destroys the credential. When the credential is destroyed, you should
   * remove any map layers that are using this credential. Any tokens
   * generated via OAuth will automatically be revoked.
   *
   * @see [IdentityManagerBase.destroyCredentials()](https://developers.arcgis.com/javascript/latest/references/core/identity/IdentityManagerBase/#destroyCredentials)
   */
  destroy(): void;
  /**
   * Generates a new token and updates the Credential's [token](https://developers.arcgis.com/javascript/latest/references/core/identity/Credential/#token) property with
   * the newly acquired token. Tokens are typically kept valid using a timer that automatically
   * triggers a refresh before the token expires. Use this method in cases where the
   * timer has been delayed or stopped.
   */
  refreshToken(): Promise<any> | undefined;
}