// Tremor Raw chartColors [v0.1.0]

export type ColorUtility = "bg" | "stroke" | "fill" | "text";

export const chartColors = {
  blue: {
    bg: "bg-blue-500",
    stroke: "stroke-blue-500",
    fill: "fill-blue-500",
    text: "text-blue-500",
  },
  emerald: {
    bg: "bg-emerald-500",
    stroke: "stroke-emerald-500",
    fill: "fill-emerald-500",
    text: "text-emerald-500",
  },
  violet: {
    bg: "bg-violet-500",
    stroke: "stroke-violet-500",
    fill: "fill-violet-500",
    text: "text-violet-500",
  },
  amber: {
    bg: "bg-amber-500",
    stroke: "stroke-amber-500",
    fill: "fill-amber-500",
    text: "text-amber-500",
  },
  gray: {
    bg: "bg-gray-500",
    stroke: "stroke-gray-500",
    fill: "fill-gray-500",
    text: "text-gray-500",
  },
  cyan: {
    bg: "bg-cyan-500",
    stroke: "stroke-cyan-500",
    fill: "fill-cyan-500",
    text: "text-cyan-500",
  },
  pink: {
    bg: "bg-pink-500",
    stroke: "stroke-pink-500",
    fill: "fill-pink-500",
    text: "text-pink-500",
  },
  lime: {
    bg: "bg-lime-500",
    stroke: "stroke-lime-500",
    fill: "fill-lime-500",
    text: "text-lime-500",
  },
  fuchsia: {
    bg: "bg-fuchsia-500",
    stroke: "stroke-fuchsia-500",
    fill: "fill-fuchsia-500",
    text: "text-fuchsia-500",
  },
  brandblue: {
    bg: "bg-[#14163d]",
    stroke: "stroke-[#14163d]",
    fill: "fill-[#14163d]",
    text: "text-[#14163d]",
  },
  midblue: {
    bg: "bg-[#2d348f]",
    stroke: "stroke-[#2d348f]",
    fill: "fill-[#2d348f]",
    text: "text-[#2d348f]",
  },
  brandmidblue: {
    bg: "bg-[#3943b7]",
    stroke: "stroke-[#3943b7]",
    fill: "fill-[#3943b7]",
    text: "text-[#3943b7]",
  },
  brandbrown: {
    bg: "bg-[#c4a47c]",
    stroke: "stroke-[#c4a47c]",
    fill: "fill-[#c4a47c]",
    text: "text-[#c4a47c]",
  },
  brandtan: {
    bg: "bg-[#d3b488]",
    stroke: "stroke-[#d3b488]",
    fill: "fill-[#d3b488]",
    text: "text-[#d3b488]",
  },
  brandlightblue: {
    bg: "bg-[#eff6fc]",
    stroke: "stroke-[#eff6fc]",
    fill: "fill-[#eff6fc]",
    text: "text-[#eff6fc]",
  },
  epc_a: {
    bg: "bg-[#117d58]",
    stroke: "stroke-[#117d58]",
    fill: "fill-[#117d58]",
    text: "text-[#117d58]",
  },
  epc_b: {
    bg: "bg-[#2da55c]",
    stroke: "stroke-[#2da55c]",
    fill: "fill-[#2da55c]",
    text: "text-[#2da55c]",
  },
  epc_c: {
    bg: "bg-[#8dbd40]",
    stroke: "stroke-[#8dbd40]",
    fill: "fill-[#8dbd40]",
    text: "text-[#8dbd40]",
  },
  epc_d: {
    bg: "bg-[#f7cd14]",
    stroke: "stroke-[#f7cd14]",
    fill: "fill-[#f7cd14]",
    text: "text-[#f7cd14]",
  },
  epc_e: {
    bg: "bg-[#f3a96a]",
    stroke: "stroke-[#f3a96a]",
    fill: "fill-[#f3a96a]",
    text: "text-[#f3a96a]",
  },
  epc_f: {
    bg: "bg-[#ef8026]",
    stroke: "stroke-[#ef8026]",
    fill: "fill-[#ef8026]",
    text: "text-[#ef8026]",
  },
  epc_g: {
    bg: "bg-[#e41e3b]",
    stroke: "stroke-[#e41e3b]",
    fill: "fill-[#e41e3b]",
    text: "text-[#e41e3b]",
  },
} as const satisfies {
  [color: string]: {
    [key in ColorUtility]: string;
  };
};

export type AvailableChartColorsKeys = keyof typeof chartColors;

export const AvailableChartColors: AvailableChartColorsKeys[] = Object.keys(
  chartColors
) as Array<AvailableChartColorsKeys>;

export const constructCategoryColors = (
  categories: string[],
  colors: AvailableChartColorsKeys[]
): Map<string, AvailableChartColorsKeys> => {
  const categoryColors = new Map<string, AvailableChartColorsKeys>();
  categories.forEach((category, index) => {
    categoryColors.set(category, colors[index % colors.length]);
  });
  return categoryColors;
};

export const getColorClassName = (
  color: AvailableChartColorsKeys,
  type: ColorUtility
): string => {
  const fallbackColor = {
    bg: "bg-gray-500",
    stroke: "stroke-gray-500",
    fill: "fill-gray-500",
    text: "text-gray-500",
  };
  return chartColors[color]?.[type] ?? fallbackColor[type];
};

// Tremor Raw getYAxisDomain [v0.0.0]

export const getYAxisDomain = (
  autoMinValue: boolean,
  minValue: number | undefined,
  maxValue: number | undefined
) => {
  const minDomain = autoMinValue ? "auto" : (minValue ?? 0);
  const maxDomain = maxValue ?? "auto";
  return [minDomain, maxDomain];
};

// Tremor Raw hasOnlyOneValueForKey [v0.1.0]

export function hasOnlyOneValueForKey(
  array: any[],
  keyToCheck: string
): boolean {
  const val: any[] = [];

  for (const obj of array) {
    if (Object.prototype.hasOwnProperty.call(obj, keyToCheck)) {
      val.push(obj[keyToCheck]);
      if (val.length > 1) {
        return false;
      }
    }
  }

  return true;
}
