import type { LayerEvents } from "../Layer.js";

export interface RefreshableLayerProperties extends Partial<Pick<RefreshableLayer, "refreshInterval">> {}

export interface RefreshEvent {
  /** Indicates if the layer's data has changed. */
  dataChanged?: boolean;
}

export interface RefreshableLayerEvents extends LayerEvents {
  /**
   * Fires if the layer has the [FeatureLayer.refreshInterval](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#refreshInterval) set or when [FeatureLayer.refresh()](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#refresh) method is called.
   * The event payload indicates if the layer's data has changed.
   *
   * @since 4.21
   * @see [FeatureLayer.refreshInterval](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#refreshInterval)
   * @see [FeatureLayer.refresh()](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#refresh)
   * @see [Sample - GeoJSONLayer with dynamic URL](https://developers.arcgis.com/javascript/latest/sample-code/layers-geojson-refresh/)
   * @example
   * // listen to layer's refresh event to fetch the attachments
   * // for the updated features.
   * layer.on("refresh", function(event){
   *   if (event.dataChanged){
   *     const query = layer.createQuery();
   *     layer.queryObjectIds(query).then(function (objectIds) {
   *       let attachmentQuery = {
   *         objectIds: objectIds,
   *         definitionExpression: layer.definitionExpression,
   *         attachmentTypes: ["image/jpeg"]
   *       };
   *       layer.queryAttachments(attachmentQuery).then(function (attachments) {
   *         attachmentQuery.objectIds.forEach(function (objectId) {
   *           if (attachments[objectId]) {
   *             // process the updated attachments
   *             let attachment = attachments[objectId];
   *           }
   *         });
   *       })
   *       .catch(function (error) {
   *         console.log("attachment query error", error);
   *       });
   *     });
   *   }
   * });
   * @example
   * // listen to layer's refresh event to fetch object ids of completed features
   * layer.on("refresh", function(event){
   *   if (event.dataChanged){
   *     const query = layer.createQuery();
   *     query.where = "Status = 'Completed'";
   *     layer.queryObjectIds(query).then((objectIds) => {
   *      // process returned features
   *     });
   *   }
   * });
   */
  refresh: RefreshEvent;
}

/**
 * Mixin for layers that support refresh and refreshInterval.
 *
 * @since 4.6
 */
export abstract class RefreshableLayer {
  constructor(...args: any[]);
  /**
   * @deprecated
   * Do not directly reference this property.
   * Use EventNames and EventTypes helpers from \@arcgis/core/Evented
   */
  "@eventTypes": RefreshableLayerEvents;
  /**
   * Refresh interval of the layer in minutes. Value of `0` indicates no refresh.
   *
   * @default 0
   * @since 4.6
   * @see [RefreshableLayer.refresh()](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-mixins-RefreshableLayer.html#refresh)
   * @see [refresh event](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-mixins-RefreshableLayer.html#event-refresh)
   * @example
   * // the layer will be refreshed every minute.
   * layer.refreshInterval = 1;
   */
  accessor refreshInterval: number;
  /**
   * Fetches all the data for the layer.
   *
   * @since 4.6
   * @see [RefreshableLayer.refreshInterval](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-mixins-RefreshableLayer.html#refreshInterval)
   * @see [refresh event](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-mixins-RefreshableLayer.html#event-refresh)
   */
  refresh(): void;
}