import type { WebChartTemporalBinningUnits } from "./chart-object-literals.js";
import type { Color, IExtent, ISimpleLineSymbol, ISymbol, SimpleFillSymbolStyle, SimpleLineSymbolStyle, SimpleMarkerSymbolStyle } from "./rest-js-types.js";

/** JSON representation of the color object. */
export interface RGBObject {
  /** Red component of the color, in the range [0, 255]. */
  r: number;
  /** Green component of the color, in the range [0, 255]. */
  g: number;
  /** Blue component of the color, in the range [0, 255]. */
  b: number;
  /**
   * Alpha component of the color.
   *
   * @default 1.0
   */
  a?: number;
}

export type IRendererSymbol = ISymbol
  & {
  style?: SimpleFillSymbolStyle | SimpleLineSymbolStyle | SimpleMarkerSymbolStyle;
  color: Color;
  outline?: ISimpleLineSymbol;
  label?: string;
  uniqueValue?: string;
};

export type WebChartDataItemColor = Color | RGBArray | RGBObject | string;

export type WebChartDataItemValue = Date | IExtent | IRendererSymbol | number[] | string[] | WebChartDataItem[] | WebChartDataItemColor | boolean | number | string | null | undefined;

/** JSON representation of a typical data item. */
export type WebChartDataItem = Record<string, WebChartDataItemValue>;

/** JSON representation of the processed data for a box plot. */
export interface WebChartBoxPlotDataItem {
  dataItems: WebChartDataItem[];
  outliers?: WebChartDataItem[];
  meanLines?: WebChartDataItem[];
}

/** JSON representation of the processed data for a scatterplot trend line. */
export interface ScatterplotTrendLineData {
  r2?: number;
  trendLinePoints?: [WebChartDataItem, WebChartDataItem];
}

/** JSON representation of the processed data for a scatterplot. */
export interface WebChartScatterplotDataItem extends ScatterplotTrendLineData {
  dataItems: WebChartDataItem[];
  aggregated?: boolean;
}

export interface TimeIntervalInfo {
  unit?: WebChartTemporalBinningUnits;
  size?: number;
}

/** JSON representation of the generic data structure for bar chart, line chart, combo bar line chart, gauge, heat chart, pie chart and radar chart. */
export interface WebChartGenericDataItem {
  dataItems: WebChartDataItem[];
  /**
   * Time interval information.
   *
   * Only applicable to bar chart, line chart.
   */
  timeIntervalInfo?: TimeIntervalInfo;
  /** Indicates whether the server limit has been reached when fetching data. */
  serverLimitReached?: boolean;
}

/** JSON representation of a histogram bin. */
export interface WebChartBin {
  count: number;
  /** Minimum value in the bin. */
  minValue: number;
  /** Maximum value in the bin. */
  maxValue: number;
  /** Symbol associated with the bin. */
  symbol?: WebChartDataItem;
  /** Whether this is the last bin. */
  isLastBin?: boolean;
}

/** JSON representation of the processed data for a histogram. */
export interface WebChartHistogramDataItem {
  mean: number;
  median: number;
  stddev: number;
  max: number;
  min: number;
  /** Sum of the values of all the points. */
  sum?: number;
  /** Number of all the points/features in un-binned data (including only non-null entries). */
  count?: number;
  /** Number of null entries in the pool of points. */
  nulls?: number;
  bins: WebChartBin[];
}

export type WebChartDataTypes = WebChartBoxPlotDataItem | WebChartGenericDataItem | WebChartHistogramDataItem | WebChartScatterplotDataItem;

export type RGBArray = [number, number, number];