import type Graphic from "../Graphic.js";
import type Content from "./content/Content.js";
import type MapView from "../views/MapView.js";
import type SceneView from "../views/SceneView.js";
import type Widget from "../widgets/Widget.js";
import type { ScreenPoint } from "../core/types.js";
import type { ClickEvent } from "../views/input/types.js";

/** @since 5.0 */
export type Alignment = PopupPositionValue | (() => PopupPositionValue);

/** @since 5.0 */
export interface DockOptions {
  /** Indicates whether the dock button is enabled. */
  buttonEnabled?: boolean;
  /** The position of the docked popup. */
  position?: PopupDockPosition;
  /** The breakpoint options for docking. */
  breakpoint?: DockOptionsBreakpoint | boolean;
}

/**
 * @since 5.0
 * @param width - The width breakpoint for docking.
 * @param height - The height breakpoint for docking.
 */
export interface DockOptionsBreakpoint {
  /** The width breakpoint for docking. */
  width?: number;
  /** The height breakpoint for docking. */
  height?: number;
}

/** @since 5.0 */
export interface FetchFeaturesOptions {
  /** The view click event that is passed into the fetchFeatures method. */
  event?: ClickEvent;
  /** An AbortSignal to cancel the fetch operation. */
  signal?: AbortSignal;
}

/** @since 5.0 */
export type PopupDockPosition = PopupPositionValue | (() => PopupPositionValue);

/** @since 5.0 */
export interface PopupOutsideViewOptions {
  /** The height of the popup. */
  popupHeight: number;
  /** The width of the popup. */
  popupWidth: number;
  /** The location on the screen where the popup is anchored. */
  screenLocation: ScreenPoint;
  /** The view in which the popup is displayed. */
  view: MapView | SceneView;
  /** The side of the view where the popup is located. */
  side: PopupOutsideViewSide;
}

/** @since 5.0 */
export type PopupOutsideViewSide = "bottom" | "left" | "right" | "top";

/** @since 5.0 */
export interface PopupPosition {
  /** The offset from the left edge of the screen. */
  left?: number;
  /** The offset from the top edge of the screen. */
  top?: number;
  /** The offset from the right edge of the screen. */
  right?: number;
  /** The offset from the bottom edge of the screen. */
  bottom?: number;
}

/** @since 5.0 */
export type PopupPositionResult = "bottom-center" | "bottom-left" | "bottom-right" | "top-center" | "top-left" | "top-right";

/** @since 5.0 */
export interface PopupPositionStyle {
  /** The offset from the left edge of the screen. */
  left?: string;
  /** The offset from the top edge of the screen. */
  top?: string;
  /** The offset from the right edge of the screen. */
  right?: string;
  /** The offset from the bottom edge of the screen. */
  bottom?: string;
}

/** @since 5.0 */
export type PopupPositionValue = PopupPositionResult | PopupPositionVariable;

/** @since 5.0 */
export type PopupPositionVariable = "auto" | "bottom-end" | "bottom-start" | "top-end" | "top-start";

/** @since 5.0 */
export type PopupTemplateContent = PopupTemplateContentResolved | PopupTemplateContentPromise | PopupTemplateContentFunction;

/**
 * Custom element content.
 *
 * @since 5.0
 */
export type PopupTemplateContentCreated = string | HTMLElement | Widget | null | undefined;

/** @since 5.0 */
export type PopupTemplateContentCreator = PopupTemplateContentCreatorFunction | PopupTemplateContentCreatorPromise | PopupTemplateContentCreated;

/**
 * The function that is called to create the custom content element.
 *
 * @param event - The event that contains the [Graphic](https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html) used to represent the feature.
 * @returns The created custom content element.
 * @since 5.0
 */
export type PopupTemplateContentCreatorFunction = (event: PopupTemplateCreatorEvent) => PopupTemplateContentCreated | PopupTemplateContentCreatorPromise;

/** @since 5.0 */
export type PopupTemplateContentCreatorPromise = Promise<PopupTemplateContentCreated>;

/**
 * The function that is called to destroy the custom content element.
 *
 * @param event - The event that contains the [Graphic](https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html) used to represent the feature.
 * @since 5.0
 */
export type PopupTemplateContentDestroyer = (event: PopupTemplateCreatorEvent) => void;

/**
 * @param event - The event that contains the [Graphic](https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html) used to represent the feature.
 * @since 5.0
 */
export type PopupTemplateContentFunction = (event: PopupTemplateCreatorEvent) => PopupTemplateContentResolved | PopupTemplateContentPromise;

/** @since 5.0 */
export type PopupTemplateContentPromise = Promise<PopupTemplateContentResolved>;

/** @since 5.0 */
export type PopupTemplateContentResolved = PopupTemplateContentCreated | Content[];

/**
 * The event that contains the [Graphic](https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html) used to represent the feature.
 *
 * @since 5.0
 */
export interface PopupTemplateCreatorEvent {
  /**
   * The [Graphic](https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html) used to represent the feature.
   *
   * @since 5.0
   */
  graphic: Graphic;
}

/** @since 5.0 */
export type PopupTemplateTitle = PopupTemplateTitleResolved | PopupTemplateTitlePromise | PopupTemplateTitleFunction;

/**
 * @param event - The event that contains the [Graphic](https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html) used to represent the feature.
 * @since 5.0
 */
export type PopupTemplateTitleFunction = (event: PopupTemplateCreatorEvent) => PopupTemplateTitleResolved | PopupTemplateTitlePromise;

/** @since 5.0 */
export type PopupTemplateTitlePromise = Promise<PopupTemplateTitleResolved>;

/** @since 5.0 */
export type PopupTemplateTitleResolved = string;