import type EsriError from "../core/Error.js";
import type PortalItem from "./PortalItem.js";
import type PortalItemResource from "./PortalItemResource.js";
import type PortalFolder from "./PortalFolder.js";
import type { AbortOptions } from "../core/promiseUtils.js";

/** @since 5.0 */
export interface AddItemParameters {
  /**
   * The item to add to the user's content.
   *
   * @since 5.0
   */
  item: PortalItem;
  /**
   * The component used to stream
   * the data represented by the item to the client.
   *
   * @since 5.0
   */
  data?: string | any;
  /**
   * The portal folder in
   * which to store the item.
   *
   * @since 5.0
   */
  folder?: string | PortalFolder;
}

/**
 * The result of the [PortalUser.deleteItems()](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalUser.html#deleteItems) method containing the [item](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalItem.html), if deletion is successful, and error, if any.
 *
 * @since 5.0
 */
export interface DeleteItemsResult {
  /**
   * An item from the `items` parameter of [PortalUser.deleteItems()](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalUser.html#deleteItems).
   *
   * @since 5.0
   */
  item: PortalItem;
  /**
   * Indicates whether the item was successfully deleted.
   *
   * @since 5.0
   */
  success: boolean;
  /**
   * The error if the item was not deleted.
   *
   * @since 5.0
   */
  error?: EsriError | null;
}

/** @since 5.0 */
export interface FetchBasemapsOptions extends AbortOptions {
  /**
   * When `true` the basemaps based on [Portal.basemapGalleryGroupQuery3D](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-Portal.html#basemapGalleryGroupQuery3D) are also fetched,
   *  if no `basemapGalleryGroupQuery` is passed as an argument. When `true` and a custom `basemapGalleryGroupQuery` is passed, also 3D basemaps from the custom group are fetched.
   *  When not specified, it will default to [Portal.use3dBasemaps](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-Portal.html#use3dBasemaps).
   *
   * @since 5.0
   */
  include3d?: boolean;
}

/** @since 5.0 */
export interface FetchItemsParameters {
  /**
   * The folder to retrieve items from. When `folder` is not specified, the `includeSubfolderItems` parameter can be used to include items from subfolders.
   *
   * @since 5.0
   */
  folder?: PortalFolder;
  /**
   * Option to retrieve items from the recycle bin instead.
   *
   * @default false
   * @since 5.0
   */
  inRecycleBin?: boolean;
  /**
   * Option to include items from subfolders along with items in the root folder. This parameter does not apply when the `folder` parameter is specified.
   *
   * @default false
   * @since 5.0
   */
  includeSubfolderItems?: boolean;
  /**
   * The maximum number of results to be included in the result set response. The maximum value allowed
   * is 100. The start property combined with the num property can be used to paginate the search results.
   *
   * @default 10
   * @since 5.0
   */
  num?: number;
  /**
   * A comma-delimited list of fields to sort by. Allowed values are `created`, `modified`, `size`, and `type`.
   *
   * @default created
   * @since 5.0
   */
  sortField?: string;
  /**
   * The order in which to sort the results. Allowed values are `asc` for ascending order and `desc` for descending order.
   *
   * @default asc
   * @since 5.0
   */
  sortOrder?: "asc" | "desc";
  /**
   * The index of the first entry in the result set response. The index is 1-based.
   *
   * @default 1
   * @since 5.0
   */
  start?: number;
}

/**
 * The result of the [PortalUser.fetchItems()](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalUser.html#fetchItems) method containing an array of the fetched [portal items](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalItem.html), the next entry index, and the total number of results.
 *
 * @since 5.0
 */
export interface FetchItemsResult {
  /**
   * An array containing user's portal items.
   *
   * @since 5.0
   */
  items: PortalItem[];
  /**
   * The next entry index if the current result set doesn't contain all results.
   *
   * @since 5.0
   */
  nextStart: number;
  /**
   * The total number of results.
   *
   * @since 5.0
   */
  total: number;
}

/** @since 5.0 */
export interface FetchRelatedItemsParameters {
  /**
   * The type of relationship between the two items.
   * See [Relationship types](https://developers.arcgis.com/rest/users-groups-and-items/relationship-types.htm) for a complete listing of types.
   *
   * @since 5.0
   */
  relationshipType: string;
  /**
   * The direction of the relationship. Can either
   *   be `forward` (from origin to destination) or `reverse` (from destination to origin).
   *
   * @since 5.0
   */
  direction?: "forward" | "reverse";
}

/**
 * A resource reference returned in the result of [PortalItem.fetchResources()](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalItem.html#fetchResources).
 *
 * @since 5.0
 */
export interface FetchResource {
  /**
   * Relative path of the resource.
   *
   * @since 5.0
   */
  resource: PortalItemResourceWithPath;
  /**
   * When the resource was created.
   *
   * @since 5.0
   */
  created: Date;
  /**
   * The size of the resource in bytes.
   *
   * @since 5.0
   */
  size: number;
}

/**
 * Parameters used when fetching portal item resources using [PortalItem.fetchResources()](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalItem.html#fetchResources).
 *
 * @since 5.0
 */
export interface FetchResourcesParameters {
  /**
   * The index of the first entry in the result set response. The index is 1-based. The
   *   `start`property, along with the `num` property can be used to paginate the search results.
   *
   * @default 1
   * @since 5.0
   */
  start?: number;
  /**
   * The maximum number of results to be included in the [FetchResourcesResult.resources](#FetchResourcesResult) set response. The maximum value allowed is
   *   `100`. The `start` property combined with the `num` property can be used to paginate the search results.
   *
   * @default 10
   * @since 5.0
   */
  num?: number;
  /**
   * A field to sort the results.
   *
   * @default resource
   * @since 5.0
   */
  sortField?: "created" | "size" | "resource";
  /**
   * The order in which to sort the results.
   *   Possible Value | Description
   *   ---------------|------------
   *   asc | Sort the results in ascending order.
   *   desc | Sort the results in descending order.
   *
   * @default asc
   * @since 5.0
   */
  sortOrder?: "asc" | "desc";
}

/**
 * Object returned when [PortalItem.fetchResources()](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalItem.html#fetchResources) promise resolves.
 *
 * @since 5.0
 */
export interface FetchResourcesResult {
  /**
   * The fetched resources.
   *
   * @since 5.0
   */
  resources: FetchResource[];
  /**
   * The next entry index if the current result set doesn't contain all results.
   *
   * @since 5.0
   */
  nextStart: number;
  /**
   * The total number of results.
   *
   * @since 5.0
   */
  total: number;
}

/** @since 5.0 */
export interface ItemUpdateParameters {
  /**
   * **Optional**. The component used to stream
   * the data represented by the item to the client.
   *
   * @since 5.0
   */
  data?: any;
}

/**
 * Members returned in the result of [PortalGroup.fetchMembers()](https://developers.arcgis.com/javascript/latest/api-reference/esri-portal-PortalGroup.html#fetchMembers).
 *
 * @since 5.0
 */
export interface Members {
  /**
   * The admins.
   *
   * @since 5.0
   */
  admins: string[];
  /**
   * The owner.
   *
   * @since 5.0
   */
  owner: string;
  /**
   * The users.
   *
   * @since 5.0
   */
  users: string[];
}

/** @since 5.0 */
export type PortalAuthMode = "anonymous" | "auto" | "immediate" | "no-prompt";

/** @since 5.0 */
export type PortalGroupSortField = "title" | "uploaded" | "modified" | "username" | "created" | "type" | "owner" | "avg-rating" | "num-ratings" | "num-comments" | "num-views";

/** @since 5.0 */
export interface PortalItemResourceAddOrUpdateOptions extends AbortOptions {
  /**
   * Indicates the level of access to the resource. The default is
   *   "inherit" which causes the resource to have the same access level as the owning item.
   *
   * @default "inherit"
   * @since 5.0
   */
  access?: "inherit" | "private";
}

/** @since 5.0 */
export interface PortalItemResourceWithPath extends PortalItemResource {}