import { type Theme } from '@material-hu/mui/styles';

import {
  type LikertChartDataItem,
  SentimentType,
} from '@material-hu/components/composed-components/peopleExperience/surveyResults/LikertChart/types';
import { type SelectChartDataItem } from '@material-hu/components/composed-components/peopleExperience/surveyResults/SelectChart/types';

import {
  type QuestionStatsResponse,
  QuestionStatType,
} from '../../../../../../../../types';

const SENTIMENT_TYPE_MAP: Record<QuestionStatType, SentimentType> = {
  [QuestionStatType.POSITIVE]: SentimentType.POSITIVE,
  [QuestionStatType.NEGATIVE]: SentimentType.NEGATIVE,
  [QuestionStatType.NEUTRAL]: SentimentType.NEUTRAL,
};

export const getKeyValueChartData = (
  stats: QuestionStatsResponse,
): SelectChartDataItem[] => {
  return Object.entries(stats.stats).map(([label, value]) => ({
    label,
    value,
  }));
};

export const getLikertChartData = (
  stats: QuestionStatsResponse,
): LikertChartDataItem[] => {
  return stats.choicesStats.map(stat => ({
    label: stat.label,
    percentage: stat.percentage,
    sentimentType: SENTIMENT_TYPE_MAP[stat.type],
  }));
};

type SentimentColorMap = {
  backgroundColor: string;
  textColor: string;
};

export const getQuestionStatColors = (
  theme: Theme,
  sentimentType: SentimentType,
): SentimentColorMap => {
  switch (sentimentType) {
    case SentimentType.POSITIVE:
      return {
        backgroundColor: theme.palette.new.background.feedback.success,
        textColor: theme.palette.new.text.feedback.success,
      };
    case SentimentType.NEGATIVE:
      return {
        backgroundColor: theme.palette.new.background.feedback.error,
        textColor: theme.palette.new.text.feedback.error,
      };
    case SentimentType.NEUTRAL:
      return {
        backgroundColor: theme.palette.new.background.feedback.warning,
        textColor: theme.palette.new.text.feedback.warning,
      };
    default:
      return {
        backgroundColor: theme.palette.new.background.layout.default,
        textColor: theme.palette.new.text.neutral.disabled,
      };
  }
};
