/**
 * Shared ApplySet utils for Components.
 *
 * @internal
 * @internal
 */
import type Graphic from "../../Graphic.js";
import type FeatureLayer from "../../layers/FeatureLayer.js";
import type SubtypeGroupLayer from "../../layers/SubtypeGroupLayer.js";
import type FeatureService from "../../rest/featureService/FeatureService.js";
import type { EditsResult } from "../../editing/types.js";
import type { ServiceEditsResult } from "../../rest/featureService/types.js";
import type { MapViewOrSceneView } from "../../views/MapViewOrSceneView.js";

/**
 * @internal
 * @internal
 */
export type ApplySetItem = ApplySetAddItem | ApplySetModifyItem | ApplySetDeleteItem;

/**
 * @internal
 * @internal
 */
export type ApplySetSupportedLayer = FeatureLayer | SubtypeGroupLayer;

/**
 * The set of changes which are to be applied to different features and services.
 *
 * @internal
 * @internal
 */
export interface ApplySet {
  /**
   * The list of edits held in the ApplySet
   *
   * @internal
   */
  get items(): ApplySetItem[];
  /**
   * Add an edit to the ApplySet
   *
   * @param item
   * @internal
   */
  push(item: ApplySetItem): void;
  /**
   * Remove all edits from the ApplySet.
   *
   * @internal
   */
  revert(): void;
  /**
   * A feature state provider which will automatically show temporary edits
   *
   * @internal
   */
  featureState: FeatureStateProvider | null;
}

/**
 * Creates an ApplySet to hold edits across multiple layers
 *
 * @internal
 * @internal
 */
export function createApplySet(): ApplySet;

/**
 * The properties required to create an ApplySetItem
 *
 * @internal
 * @internal
 */
export interface ApplySetItemProperties {
  /**
   * The graphic that is affected
   *
   * @internal
   */
  graphic: Graphic;
  /**
   * The layer on which this change is occurring
   *
   * @internal
   */
  layer: ApplySetSupportedLayer;
  /**
   * The reason the change is being applied
   *
   * @internal
   */
  reason?: string;
}

/**
 * An ApplySet item that represents adding a feature to a layer
 *
 * @internal
 * @internal
 */
export interface ApplySetAddItem extends Omit<ApplySetItemProperties, "reason"> {
  /**
   * The type of change
   *
   * @internal
   */
  type: "add";
}

/**
 * Create an ApplySet Add Item
 *
 * @param properties
 * @internal
 * @internal
 */
export function createApplySetAddItem(properties: ApplySetItemProperties): ApplySetAddItem;

/**
 * The properties required to create an ApplySetItem modify item
 *
 * @internal
 * @internal
 */
export interface ApplySetModifyItemProperties {
  /**
   * The graphic that is affected
   *
   * @internal
   */
  graphic: Graphic;
  /**
   * The layer on which this change is occurring
   *
   * @internal
   */
  layer: ApplySetSupportedLayer;
  /**
   * The reason the change is being applied
   *
   * @internal
   */
  reason?: string;
  /**
   * The original version of the graphic, before applying the change
   *
   * @internal
   */
  originalGraphic: Graphic;
}

/**
 * An ApplySet item that represents modifying a feature in a layer
 *
 * @internal
 * @internal
 */
export interface ApplySetModifyItem extends Omit<ApplySetItemProperties, "reason" | "originalGraphic"> {
  /**
   * The type of change
   *
   * @internal
   */
  type: "modify";
  /**
   * The original version of the graphic, before applying the change
   *
   * @internal
   */
  revertGraphic: Graphic;
}

/**
 * Create an ApplySet Modify Item
 *
 * @param properties
 * @internal
 * @internal
 */
export function createApplySetModifyItem(properties: ApplySetModifyItemProperties): ApplySetModifyItem;

/**
 * An ApplySet item that represents deleting a feature in a layer
 *
 * @internal
 * @internal
 */
export interface ApplySetDeleteItem extends Omit<ApplySetItemProperties, "reason"> {
  /**
   * The type of change
   *
   * @internal
   */
  type: "delete";
}

/**
 * Create an ApplySet Delete Item
 *
 * @param properties
 * @internal
 * @internal
 */
export function createApplySetDeleteItem(properties: ApplySetItemProperties): ApplySetDeleteItem;

/**
 * Returned results from calling applyEdits on a Layer
 *
 * @internal
 * @internal
 */
export interface ApplySetLayerEditResult {
  /**
   * The layer on which this change is occurring
   *
   * @internal
   */
  layer: ApplySetSupportedLayer;
  /**
   * The returned results from calling applyEdits
   *
   * @internal
   */
  edits: EditsResult;
  /**
   * The type of applyEdits call
   *
   * @internal
   */
  type: "layer";
}

/**
 * Returned results from calling applyEdits on a Layer
 *
 * @internal
 * @internal
 */
export interface ApplySetServiceEditResult {
  /**
   * The layer on which this change is occurring
   *
   * @internal
   */
  featureService: FeatureService;
  /**
   * The returned results from calling applyEdits
   *
   * @internal
   */
  edits: ServiceEditsResult[];
  /**
   * The type of applyEdits call
   *
   * @internal
   */
  type: "service";
}

/**
 * Writes all the changes listed in an ApplySet, to their respective services.
 *
 * @param applySet
 * @param view
 * @internal
 * @internal
 */
export function writeApplySetToService(applySet: ApplySet, view: MapViewOrSceneView): Promise<(ApplySetLayerEditResult | ApplySetServiceEditResult)[]>;

/**
 * Manages temporary feature edits which are displayed in LayerViews.
 *
 * @internal
 * @internal
 */
export interface FeatureStateProvider {
  /**
   * Clears all temporary feature state from the map
   *
   * @internal
   */
  clearAll: () => void;
}

/**
 * Creates a feature state provider, that will automatically show temporary feature changes
 * on a map.
 *
 * @param view
 * @internal
 * @internal
 */
export function createFeatureStateProvider(view: MapViewOrSceneView): FeatureStateProvider | null;