import type SpatialReference from "../../../geometry/SpatialReference.js";
import type { JSONSupport, JSONSupportMixinProperties } from "../../../core/JSONSupport.js";
import type { FeatureGeometryType } from "../../../geometry/types.js";
import type { SpatialReferenceProperties } from "../../../geometry/SpatialReference.js";

export interface QueryTableDataSourceProperties extends JSONSupportMixinProperties, Partial<Pick<QueryTableDataSource, "geometryType" | "oidFields" | "query" | "workspaceId">> {
  /** The spatial reference of the geometry of each feature in the table source. */
  spatialReference?: SpatialReferenceProperties;
}

/**
 * A query table is a feature class or table defined by a SQL query on the fly.
 * Query layers allow both spatial and nonspatial information
 * stored in a database to be easily integrated into map service
 * operations. Since a query table uses SQL to directly query database
 * tables and views, spatial information used by a query table is
 * not required to be in a geodatabase.
 *
 * This data source is useful for scenarios where you have a table containing
 * multiple records that match to a single geometry in either another table or a map service layer.
 * You can use the QueryTableDataSource to select only a subset of those matching records
 * and join them to the table with geometries
 * so records in both tables have a one-to-one relationship with each other.
 *
 * @see [Sample - MapImageLayer: dynamic data layer with query table](https://developers.arcgis.com/javascript/latest/sample-code/layers-dynamicdatalayer-query-table/)
 * @see [ArcGIS REST API - Data source object](https://developers.arcgis.com/documentation/common-data-types/data-source-object.htm)
 * @example
 * let layer = new MapImageLayer({
 *   url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
 *   sublayers: [{
 *     title: "Places",
 *     renderer: renderer,
 *     source: {
 *       type: "data-layer",
 *       dataSource: {
 *         type: "query-table",
 *         workspaceId: "MyDatabaseWorkspaceIDSSR2",
 *         query: "SELECT * FROM ss6.gdb.Places",
 *         oidFields: "objectid"
 *       }
 *     }
 *   }]
 * });
 */
export default abstract class QueryTableDataSource extends JSONSupport {
  /** The geometry type of each record in the table. */
  geometryType: FeatureGeometryType;
  /**
   * The field name(s) containing the unique IDs for each
   * record in the table. This can be a comma separated list if the query table is used
   * in a [JoinTableDataSource](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-support-source-DataLayerSource.html#JoinTableDataSource).
   */
  accessor oidFields: string;
  /** The SQL query used to filter records. */
  accessor query: string;
  /** The spatial reference of the geometry of each feature in the table source. */
  get spatialReference(): SpatialReference;
  set spatialReference(value: SpatialReferenceProperties);
  /**
   * This value is always `query-table` and
   * is inferred when the `query` property of this object is set.
   */
  get type(): "query-table";
  /** The workspace where the data resides (defined in ArcGIS Server Manager). */
  accessor workspaceId: string;
}