import type Graphic from "../../Graphic.js";
import type Collection from "../../core/Collection.js";
import type Point from "../../geometry/Point.js";
import type ActionButton from "../../support/actions/ActionButton.js";
import type ActionToggle from "../../support/actions/ActionToggle.js";
import type { AbortOptions } from "../../core/promiseUtils.js";
import type { ClickEvent } from "../../views/input/types.js";

export type PopupAction = ActionButton | ActionToggle;

export type PopupPositionResult = "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right";

export type PopupPositionVariable = "auto" | "top-leading" | "top-trailing" | "bottom-leading" | "bottom-trailing";

export type PopupPositionValue = PopupPositionVariable | PopupPositionResult;

export type Alignment = PopupPositionValue | (() => PopupPositionValue);

export type PopupDockPosition = PopupPositionValue | (() => PopupPositionValue);

export interface ActionEvent {
  /**
   * The action clicked by the user. For a description
   * of this object and a specification of its properties, see the [Popup#actions](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#actions) property of this
   * class.
   */
  action: PopupAction;
}

export interface DockOptionsBreakpoint {
  /**
   * The maximum height of the [View](https://developers.arcgis.com/javascript/latest/api-reference/esri-views-View.html)
   *                        at which the popup will be set to dockEnabled automatically.
   *
   * @default 544
   */
  height?: number;
  /**
   * The maximum width of the [View](https://developers.arcgis.com/javascript/latest/api-reference/esri-views-View.html)
   *                        at which the popup will be set to dockEnabled automatically.
   *
   * @default 544
   */
  width?: number;
}

export interface DockOptions {
  /**
   * Defines the dimensions of the [View](https://developers.arcgis.com/javascript/latest/api-reference/esri-views-View.html)
   *                        at which to dock the popup. Set to `false` to disable docking at a breakpoint.
   *
   * @default true
   */
  breakpoint?: DockOptionsBreakpoint | boolean;
  /**
   * If `true`, displays the dock button. If `false`, hides the dock
   *                         button from the popup.
   */
  buttonEnabled?: boolean;
  /**
   * The position in the view at which to dock the popup.
   *                        Can be set as either a string or function. See the table below for known
   *                        string values and their position in the view based on the view's size.
   *                        <br><br>
   * Known Value | View size > breakpoint | View size < breakpoint ([at xsmall view breakpoint](https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#breakpoints))
   * --------------- | ------------------------------- | -------------
   * auto | top-right | top-right
   * top-left | top-left | top-left
   * top-center | top-center | top 100%
   * top-right | top-right | top right
   * bottom-left | bottom-left | bottom left
   * bottom-center | bottom-center | bottom 100%
   * bottom-right | bottom-right | bottom right
   *
   * > [!WARNING]
   * >
   * > If viewing the popup in a mobile  UI, the default dock position is bottom 100%.
   *
   * @default auto
   */
  position?: PopupDockPosition;
}

export type InitialDisplayOptions = "list" | "feature";

export type Action = ActionButton | ActionToggle;

export interface PopupOpenOptions {
  /**
   * This property allows you to specify a collection of [actions](https://developers.arcgis.com/javascript/latest/api-reference/esri-support-actions-ActionButton.html)
   * to the action bar when the Features widget opens.
   */
  actions?: Collection<PopupAction>;
  /**
   * Indicates whether the widget displays its content. If `true`, only the header displays.
   *
   * @default false
   */
  collapsed?: boolean;
  /** Sets the [Popup#content](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#content) of the popup. */
  content?: any;
  /**
   * This property enables multiple features in a popup to display in a list rather than displaying the first selected feature. Setting this to `true`
   * allows the user to scroll through the list of features.
   * This value will only be honored if [PopupViewModel#initialDisplayMode](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup-PopupViewModel.html#initialDisplayMode) is set to `feature`.
   *
   * @default false
   */
  featureMenuOpen?: boolean;
  /** Sets the title to display on the widget while viewing the feature menu. */
  featureMenuTitle?: string;
  /** Sets the popup's [Popup#features](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#features), which populate the title and content of the popup based on each graphic's [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html). */
  features?: Graphic[];
  /**
   * When `true`, indicates the the content of this feature should be fetched and displayed. If no [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html) exists, a default template is created for the layer if
   * [Popup#defaultPopupTemplateEnabled](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#defaultPopupTemplateEnabled) = `true`. In order for this option to work, there must be a valid `view` and `location` set.
   *
   * @default false
   */
  fetchFeatures?: boolean;
  /** Sets the popup's [Popup#location](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#location), which is the geometry used to position the popup. */
  location?: Point;
  /** Sets pending [Popup#promises](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#promises) on the popup. The popup will display once the promises resolve. Each promise must resolve to an array of [Graphics](https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html). */
  promises?: Promise<any>[];
  /**
   * **Since:** 4.23 <br> When `true`, indicates that the focus should be on the popup after it has been opened.
   *
   * @default false
   */
  shouldFocus?: boolean;
  /** Sets the [Popup#title](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#title) of the popup. */
  title?: string;
  /**
   * When `true`, indicates the popup should update its [Popup#location](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#location) for each paginated feature based on the [selectedFeature's](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#selectedFeature) geometry.
   *
   * @default false
   */
  updateLocationEnabled?: boolean;
}

export type State = "disabled" | "ready";

/** Optional properties to use with the [Features#fetchFeatures()](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Features.html#fetchFeatures) and [Popup#fetchFeatures()](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#fetchFeatures) methods. */
export interface FetchFeaturesOptions extends AbortOptions {
  /**
   * The `click` event for either the [MapView](https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html) or [SceneView](https://developers.arcgis.com/javascript/latest/api-reference/esri-views-SceneView.html).
   * The event can be supplied in order to adjust the query radius depending on the pointer type. For example, touch events query a larger radius.
   */
  event?: ClickEvent;
}