import { CollaboratorSurveyStatus, type Question } from '../types';
import { type LokaliseTFunction } from 'src/utils/i18n';

type PillPropsFromStatusParams = {
  status?: CollaboratorSurveyStatus;
  t: LokaliseTFunction;
};

export const getPillPropsFromStatus = ({
  status,
  t,
}: PillPropsFromStatusParams) => {
  if (!status) {
    return undefined;
  }

  const STATUS_PILL_TYPE = {
    [CollaboratorSurveyStatus.PENDING]: 'success',
    [CollaboratorSurveyStatus.FINISHED]: 'highlight',
    [CollaboratorSurveyStatus.IN_PROGRESS]: 'neutral',
    [CollaboratorSurveyStatus.UNCOMPLETED]: 'neutral',
  } as const;

  const STATUS_TRANSLATION: Record<string, string> = {
    [CollaboratorSurveyStatus.UNCOMPLETED]: 'FINISHED',
    [CollaboratorSurveyStatus.FINISHED]: 'COMPLETED',
  };

  return {
    label: t(STATUS_TRANSLATION[status] ?? status),
    type: STATUS_PILL_TYPE[status],
  };
};

export const getStartQuestionIndex = (questions: Question[]) => {
  const firstUnansweredQuestionIndex = questions.findIndex(question => {
    return !question.answered;
  });

  return firstUnansweredQuestionIndex === -1
    ? questions.length - 1
    : firstUnansweredQuestionIndex;
};
