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

/** @since 5.0 */
export interface QueryTableDataSourceProperties extends Partial<Pick<QueryTableDataSource, "geometryType" | "oidFields" | "query" | "workspaceId">> {
  /**
   * The spatial reference of the geometry of each feature in the table source.
   *
   * @since 5.0
   */
  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.
 *
 * @since 5.0
 * @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 - Query table data source](https://developers.arcgis.com/rest/services-reference/enterprise/data-source-object/#query-table-data-source)
 * @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 class QueryTableDataSource extends JSONSupport {
  /** @since 5.0 */
  constructor(properties?: QueryTableDataSourceProperties);
  /**
   * The geometry type of each record in the table.
   *
   * @since 5.0
   */
  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/references/core/rest/layerSources/JoinTableDataSource/).
   *
   * @since 5.0
   */
  accessor oidFields: string;
  /**
   * The SQL query used to filter records.
   *
   * @since 5.0
   */
  accessor query: string;
  /**
   * The spatial reference of the geometry of each feature in the table source.
   *
   * @since 5.0
   */
  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.
   *
   * @since 5.0
   */
  get type(): "query-table";
  /**
   * The workspace where the data resides (defined in ArcGIS Server Manager).
   *
   * @since 5.0
   */
  accessor workspaceId: string;
}