/**
 * Various utils for working with [Popup](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html) widget functionality.
 *
 * @since 4.16
 */
import type PopupTemplate from "../PopupTemplate.js";
import type EditFieldsInfo from "../layers/support/EditFieldsInfo.js";
import type FeatureReductionBinning from "../layers/support/FeatureReductionBinning.js";
import type FeatureReductionCluster from "../layers/support/FeatureReductionCluster.js";
import type Field from "../layers/support/Field.js";
import type FieldInfo from "../popup/FieldInfo.js";
import type FieldsContent from "../popup/content/FieldsContent.js";

/**
 * Creates a popup template given the specified [Config](https://developers.arcgis.com/javascript/latest/api-reference/esri-support-popupUtils.html#Config) information.
 *
 * @param config - A configuration object containing properties for creating a [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 * @param options - Options for creating the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 * @returns The popup template, or `null` if no fields are set.
 * @since 4.16
 * @example
 * // Sets the configuration for the popup template.
 * // Each object in this array is autocast as an instance of esri/layers/support/Field
 * const fields = [{
 *   name: "NAME",
 *   alias: "Name",
 *   type: "string"
 * }, {
 *   name: "County",
 *   alias: "County",
 *   type: "string"
 * }, {
 *   name: "ALAND",
 *   alias: "Land",
 *   type: "double"
 * }];
 *
 * const config = {
 *   displayField: "County",
 *   fields: fields,
 *   title: "County land"
 * };
 *
 * // This sets the options to ignore all fields of "date" type and sets two visible fields
 * const templateOptions = {
 *   ignoreFieldTypes: ["date"],
 *   visibleFieldNames: new Set(["NAME", "ALAND"])
 * };
 *
 * const template = popupUtils.createPopupTemplate(config, templateOptions);
 * featureLayer.popupTemplate = template;
 */
export function createPopupTemplate(config: Config, options?: Partial<CreatePopupTemplateOptions>): PopupTemplate | null | undefined;

/**
 * Creates a default popup template to use for [FeatureReductionBinning](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-support-FeatureReductionBinning.html)
 * or [FeatureReductionCluster](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-support-FeatureReductionCluster.html) visualizations. The template will consist
 * of a list of all the aggregate fields defined in the featureReduction property of the layer.
 *
 * @param params - Parameters for creating a feature reduction popup template.
 * @returns The popup template, or `null` if no [AggregateFields](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-support-AggregateField.html)
 *   are set on the feature reduction object.
 * @since 4.25
 * @example
 * const popupTemplate = popupUtils.createPopupTemplateForFeatureReduction(layer);
 * layer.featureReduction.popupTemplate = popupTemplate;
 */
export function createPopupTemplateForFeatureReduction(params: CreatePopupTemplateForFeatureReductionParams): PopupTemplate | null | undefined;

/**
 * Creates [fields](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-content-FieldsContent.html) content used for populating a [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 *
 * @param config - A configuration object containing properties for creating [FieldsContent](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-content-FieldsContent.html).
 * @param options - Options for creating the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 * @returns The`fields` content used to populate the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html). This content contains an array of [FieldInfo](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-FieldInfo.html).
 * @since 4.16
 * @example
 * // Sets the configuration for the popup template.
 * // Each object in this array is autocast as an instance of esri/layers/support/Field
 * const fields = [{
 *   name: "NAME",
 *   alias: "Name",
 *   type: "string"
 * }, {
 *   name: "County",
 *   alias: "County",
 *   type: "string"
 * }, {
 *   name: "ALAND",
 *   alias: "Land",
 *   type: "double"
 * }];
 *
 * // This sets the options to ignore all fields of "date" type and sets two visible fields
 * const templateOptions = {
 *   ignoreFieldTypes: ["date"],
 *   visibleFieldNames: new Set(["NAME", "ALAND"])
 * };
 *
 * // Set the FieldInfo
 * const fieldsConfig = {fields: fields};
 *
 * // Create the Fields Content
 * const fieldsContent = popupUtils.createFieldsContent(fieldsConfig, templateOptions);
 *
 * // Create the template and pass in the fields content
 * const template = {
 *   title: "County Land",
 *   outFields: ["*"],
 *   content: [fieldsContent]
 *   };
 *
 * // Set the feature layer's popup template
 * featureLayer.popupTemplate = template;
 */
export function createFieldsContent(config: FieldInfosConfig, options?: Partial<CreatePopupTemplateOptions>): FieldsContent;

/**
 * Creates an array of [FieldInfo](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-FieldInfo.html) used for populating [FieldsContent](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-content-FieldsContent.html).
 *
 * @param config - A configuration object containing properties for creating [FieldInfo](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-FieldInfo.html).
 * @param options - Options for creating the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 * @returns An array of [FieldInfo](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-FieldInfo.html) content used to populate the [FieldsContent](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-content-FieldsContent.html),
 * which in turn is used to populate the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 * By default, system [fields](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-FieldInfo.html),
 * e.g. `Shape__Area` and `Shape__Length`, `OID`, etc, do not display.
 * @since 4.16
 * @example
 * // Sets the configuration for the popup template.
 * // Each object in this array is autocast as an instance of esri/layers/support/Field
 * const fields = [{
 *   name: "NAME",
 *   alias: "Name",
 *   type: "string"
 * }, {
 *   name: "County",
 *   alias: "County",
 *   type: "string"
 * }, {
 *   name: "ALAND",
 *   alias: "Land",
 *   type: "double"
 * }];
 *
 * // This sets the options to ignore all fields of "date" type and sets two visible fields
 * const templateOptions = {
 *   ignoreFieldTypes: ["date"],
 *   visibleFieldNames: new Set(["NAME", "ALAND"])
 * };
 *
 * // Set the FieldInfo
 * const fieldsConfig = {fields: fields};
 *
 * // Create the FieldInfos
 * const fieldInfos = popupUtils.createFieldInfos(fieldsConfig, templateOptions);
 *
 * // Sets the FieldsContent
 * const fieldsContent = new FieldsContent({
 *   fieldInfos: fieldInfos
 * });
 *
 * // Create the template and pass in the fields content
 * const template = {
 *   title: "County Land",
 *   outFields: ["*"],
 *   content: [fieldsContent]
 *   };
 *
 * // Set the feature layer's popup template
 * featureLayer.popupTemplate = template;
 */
export function createFieldInfos(config: FieldInfosConfig, options?: Partial<CreatePopupTemplateOptions>): FieldInfo[];

/**
 * A configuration object containing field properties for creating [FieldsContent](https://developers.arcgis.com/javascript/latest/api-reference/esri-popup-content-FieldsContent.html) for a [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 *
 * @example
 * // Sets the configuration for the popup template.
 * // Each object in this array is autocast as an instance of esri/layers/support/Field
 * const fieldsConfig = [{
 *   name: "NAME",
 *   alias: "Name",
 *   type: "string"
 * }, {
 *   name: "County",
 *   alias: "County",
 *   type: "string"
 * }, {
 *   name: "ALAND",
 *   alias: "Land",
 *   type: "double"
 * }];
 */
export interface FieldInfosConfig {
  /** The fields that record who adds or edits data in the feature service and when the edit is made. */
  editFieldsInfo?: EditFieldsInfo | null;
  /** The fields displayed within the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html). */
  fields?: Field[] | null;
  /** The object id field. */
  objectIdField?: string | null;
}

/**
 * A configuration object containing properties for creating a [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 *
 * @example
 * // Sets the configuration for the popup template.
 * // Each object in this array is autocast as an instance of esri/layers/support/Field
 * const fields = [{
 *   name: "NAME",
 *   alias: "Name",
 *   type: "string"
 * }, {
 *   name: "County",
 *   alias: "County",
 *   type: "string"
 * }, {
 *   name: "ALAND",
 *   alias: "Land",
 *   type: "double"
 * }];
 *
 * const config = {
 *   displayField: "County",
 *   fields: fields,
 *   title: "County land"
 * };
 */
export interface Config extends FieldInfosConfig {
  /** The display field. */
  displayField?: string | null;
  /** The title for the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html). */
  title?: string | null;
}

export interface CreatePopupTemplateForFeatureReductionParams {
  /** The FeatureReduction object for which to create a default popup template. */
  featureReduction: FeatureReductionBinning | FeatureReductionCluster;
  /**
   * The fields of the layer aggregated during the feature reduction process. These should
   *   correspond to any fields used in the definition of the aggregate fields of the feature reduction instance provided to this function.
   */
  fields?: Field[] | null;
  /** The title to display in the popup template. */
  title?: string;
}

/**
 * Options for creating the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html).
 *
 * @example
 * // This sets the options to ignore all fields of "date" type and sets two visible fields
 * const templateOptions = {
 *   ignoreFieldTypes: ["date"],
 *   visibleFieldNames: new Set(["NAME", "ALAND"])
 * };
 */
export interface CreatePopupTemplateOptions {
  /** An array of field types to ignore when creating the popup. System fields such as `Shape_Area` and `Shape_length`, in addition to `geometry`, `blob`, `raster`, `guid` and `xml` field types are automatically ignored. */
  ignoreFieldTypes?: Field["type"][] | null;
  /** An array of field names set to be visible within the [PopupTemplate](https://developers.arcgis.com/javascript/latest/api-reference/esri-PopupTemplate.html). */
  visibleFieldNames?: Set<string> | null;
}