import type { WebChartHeatChartHeatRulesTypes } from "../../spec/chart-object-literals.js";
import type { WebChartDataItem } from "../../spec/data-source.js";
import type { Color, ISimpleFillSymbol, ISimpleLineSymbol, ISimpleMarkerSymbol } from "../../spec/rest-js-types.js";
import type { WebChartPieChartGroupSlice, WebChartPieChartSlice } from "../../spec/web-chart.js";

export const ChartSubType: {
    readonly NonSpecific: "nonSpecific";
    readonly BarAndLineNoAggregation: "barAndLineNoAggregation";
    readonly BarAndLineMonoField: "barAndLineMonoField";
    readonly BarAndLineMultiFields: "barAndLineMultiFields";
    readonly BarAndLineFromFields: "barAndLineFromFields";
    readonly BarAndLineSplitBy: "barAndLineSplitBy";
    readonly BarAndLineSplitByNoAggregation: "barAndLineSplitByNoAggregation";
    readonly PieNoAggregation: "pieNoAggregation";
    readonly PieFromCategory: "pieFromCategory";
    readonly PieFromFields: "pieFromFields";
    readonly BoxPlotMonoField: "boxPlotMonoField";
    readonly BoxPlotMultiFields: "boxPlotMultiFields";
    readonly BoxPlotMonoFieldAndCategory: "boxPlotMonoFieldAndCategory";
    readonly BoxPlotMultiFieldsAndCategory: "boxPlotMultiFieldsAndCategory";
    readonly BoxPlotMonoFieldAndSplitBy: "boxPlotMonoFieldAndSplitBy";
    readonly BoxPlotMultiFieldsAndSplitBy: "boxPlotMultiFieldsAndSplitBy";
    readonly BoxPlotMonoFieldAndCategoryAndSplitBy: "boxPlotMonoFieldAndCategoryAndSplitBy";
};

export type ChartSubType = (typeof ChartSubType)[keyof typeof ChartSubType];

export const SelectionSource: {
    readonly SelectionByClick: "SelectionByClick";
    readonly SelectionByRange: "SelectionByRange";
    readonly ProgrammaticSelection: "ProgrammaticSelection";
    readonly ClearSelection: "ClearSelection";
};

export type SelectionSource = (typeof SelectionSource)[keyof typeof SelectionSource];

export type SelectionIndexes = Map<number, { indexesToSelect?: number[]; }>;

export interface SelectionData {
  selectionItems?: WebChartDataItem[];
  selectionIndexes?: SelectionIndexes;
  selectionOIDs?: (number | string)[];
  selectionSource?: SelectionSource;
}

export interface SelectionThemeProperties {
  elementColor?: Color;
  elementOutlineColor?: Color;
  elementOutlineWidth?: number;
  rotation?: number;
  transformation?: {
      opacity?: number;
      scale?: number;
  };
}

export interface SelectionTheme {
  selectedElementsTheme?: SelectionThemeProperties;
  nonSelectedElementsTheme?: SelectionThemeProperties;
}

export interface LoaderColors {
  curtainColor?: Color;
  spinnerColor?: Color;
}

export interface HeatSeriesColorChangePayload {
  heatRulesType?: WebChartHeatChartHeatRulesTypes;
  seriesColors?: HeatSeriesColorChangePayloadClassSymbols[] | HeatSeriesColorChangePayloadGradient;
}

export interface HeatSeriesColorChangePayloadGradient {
  startColor: readonly [number, number, number, number];
  endColor: readonly [number, number, number, number];
}

export type HeatSeriesColorChangePayloadClassSymbols = {
  label: string;
  symbol: ISimpleFillSymbol;
};

export type BoxPlotSymbols = { fillSymbol?: ISimpleFillSymbol; } | {
    lineSymbol: ISimpleLineSymbol;
    markerSymbol?: ISimpleMarkerSymbol;
};

export type LineChartSymbols = {
  lineSymbol: ISimpleLineSymbol;
  markerSymbol?: ISimpleMarkerSymbol;
};

export type SeriesSymbols = BoxPlotSymbols | ISimpleFillSymbol | ISimpleLineSymbol | ISimpleMarkerSymbol | LineChartSymbols | PieSlicesSymbols;

/**
 * Type used to provide information regarding a pie chart's slices:
 * - `slices` contains an array of `WebChartPieChartSlice` related to the non-grouped slices.
 * - `groupedSlices` contains an array of `WebChartPieChartSlice` related to the non-visible grouped slices.
 * - `groupOfSlices` contains a `WebChartPieChartSlice`related to the group of slices.
 *
 * The `WebChartPieChartSlice` contains information regarding a slice's id, symbol and label.
 */
export type PieSlicesSymbols = {
  slices: WebChartPieChartSlice[];
  groupedSlices?: WebChartPieChartSlice[];
  groupOfSlices?: WebChartPieChartGroupSlice;
};

/**
 * Map containing a chart's series symbols.
 * The numeric key is the series' position in the series array.
 * The value is either:
 *   - A fill symbol (bar chart and histogram).
 *   - A marker symbol (scatterplot).
 *   - An object containing a line symbol and a marker symbol (line chart).
 */
export type SeriesSymbolsMap = Map<number, SeriesSymbols | undefined>;