import { DoughnutController, PolarAreaController } from 'chart.js';
import { isDefined } from '../../../internal/type-checks.mjs';

function getBorderColor(i, palette) {
  return palette.datasetBorderColors[i % palette.datasetBorderColors.length];
}
function getBackgroundColor(i, palette) {
  return palette.datasetBackgroundColors[i % palette.datasetBackgroundColors.length];
}
function colorizeDefaultDataset(dataset, i, palette) {
  dataset.borderColor = getBorderColor(i, palette);
  dataset.backgroundColor = getBackgroundColor(i, palette);
  return ++i;
}
function colorizeDoughnutDataset(dataset, i, palette) {
  dataset.backgroundColor = dataset.data.map(() => getBorderColor(i++, palette));
  return i;
}
function colorizePolarAreaDataset(dataset, i, palette) {
  dataset.backgroundColor = dataset.data.map(() => getBackgroundColor(i++, palette));
  return i;
}
function getPalette(chart) {
  const chartStyles = window.getComputedStyle(chart.canvas);
  const textColor = chartStyles.getPropertyValue("--text-color");
  const datasetBorderColors = Array.from(
    { length: 10 },
    (_, i) => chartStyles.getPropertyValue(`--dataset-stroke-color-${i}`)
  );
  const datasetBackgroundColors = Array.from(
    { length: 10 },
    (_, i) => chartStyles.getPropertyValue(`--dataset-fill-color-${i}`)
  );
  const xAxisColor = chartStyles.getPropertyValue("--x-axis-color");
  const xAxisGridColor = chartStyles.getPropertyValue("--x-axis-grid-color");
  const xAxisTickColor = chartStyles.getPropertyValue("--x-axis-tick-color");
  const xAxisLabelColor = chartStyles.getPropertyValue("--x-axis-label-color");
  const xTickLabelColor = chartStyles.getPropertyValue("--x-tick-label-color");
  const yAxisColor = chartStyles.getPropertyValue("--y-axis-color");
  const yAxisGridColor = chartStyles.getPropertyValue("--y-axis-grid-color");
  const yAxisTickColor = chartStyles.getPropertyValue("--y-axis-tick-color");
  const yAxisLabelColor = chartStyles.getPropertyValue("--y-axis-label-color");
  const yTickLabelColor = chartStyles.getPropertyValue("--y-tick-label-color");
  return {
    textColor,
    datasetBorderColors,
    datasetBackgroundColors,
    xAxis: {
      color: xAxisColor,
      gridColor: xAxisGridColor,
      tickColor: xAxisTickColor,
      axisLabelColor: xAxisLabelColor,
      tickLabelColor: xTickLabelColor
    },
    yAxis: {
      color: yAxisColor,
      gridColor: yAxisGridColor,
      tickColor: yAxisTickColor,
      axisLabelColor: yAxisLabelColor,
      tickLabelColor: yTickLabelColor
    }
  };
}
function getDatasetColorizer(chart, palette) {
  let i = 0;
  return (dataset, datasetIndex) => {
    const controller = chart.getDatasetMeta(datasetIndex).controller;
    if (controller instanceof DoughnutController) {
      i = colorizeDoughnutDataset(dataset, i, palette);
    } else if (controller instanceof PolarAreaController) {
      i = colorizePolarAreaDataset(dataset, i, palette);
    } else if (controller) {
      i = colorizeDefaultDataset(dataset, i, palette);
    }
  };
}
function isCartesianScaleOptions(scale) {
  return isDefined(scale) && ["linear", "category", "time", "timeseries", "logarithmic"].includes(scale.type);
}
function isRadialScaleOptions(scale) {
  return isDefined(scale) && scale.type === "radialLinear";
}
const FlowUiColorsPlugin = {
  id: "flowUiColors",
  defaults: {
    enabled: true,
    colorDatasets: true,
    colorAxis: true
  },
  beforeLayout(chart, _args, options) {
    if (!options.enabled) {
      return;
    }
    const palette = getPalette(chart);
    const {
      data: { datasets },
      options: chartOptions
    } = chart.config;
    if (!chartOptions) {
      return;
    }
    chartOptions.color = palette.textColor;
    if (options.colorDatasets) {
      const colorizer = getDatasetColorizer(chart, palette);
      datasets.forEach(colorizer);
    }
    if (options.colorAxis && chartOptions?.scales) {
      for (const scale of Object.values(chartOptions.scales)) {
        if (isCartesianScaleOptions(scale)) {
          const axisPalette = scale.axis === "x" ? palette.xAxis : palette.yAxis;
          scale.border = scale.border || {};
          scale.grid = scale.grid || {};
          scale.ticks = scale.ticks || {};
          scale.border.color = axisPalette.color;
          scale.grid.color = axisPalette.gridColor;
          scale.grid.tickColor = axisPalette.gridColor;
          scale.title.color = axisPalette.axisLabelColor;
          scale.ticks.color = axisPalette.tickLabelColor;
        } else if (isRadialScaleOptions(scale)) {
          const axisPalette = palette.xAxis;
          scale.grid = scale.grid || {};
          scale.ticks = scale.ticks || {};
          scale.grid.color = axisPalette.gridColor;
          scale.grid.tickColor = axisPalette.gridColor;
          scale.ticks.color = axisPalette.tickLabelColor;
        }
      }
    }
    if (chartOptions.plugins?.title) {
      chartOptions.plugins.title.color = palette.textColor;
    }
  }
};

export { FlowUiColorsPlugin as default };
