import { type PillsProps } from '@material-hu/components/design-system/Pills';

import {
  MenuItems,
  type MenuItemsProps,
  type ReviewDetail,
  ReviewDirection,
} from 'src/pages/dashboard/performance/types';
import { insertIf } from 'src/utils/arrays';
import { type LokaliseTFunction } from 'src/utils/i18n';

export const REVIEW_DIRECTION_ORDER: {
  direction: ReviewDirection;
  titleKey: string;
}[] = [
  { direction: ReviewDirection.BOSS_REVIEW, titleKey: 'types.boss_review' },
  { direction: ReviewDirection.AUTO_REVIEW, titleKey: 'types.auto_review' },
  {
    direction: ReviewDirection.SUBORDINATE_REVIEW,
    titleKey: 'types.subordinate_review',
  },
  { direction: ReviewDirection.PEER_REVIEW, titleKey: 'types.peer_review' },
  {
    direction: ReviewDirection.PEER_EXTERNAL_REVIEW,
    titleKey: 'types.peer_external_review',
  },
  {
    direction: ReviewDirection.OTHERS_REVIEW,
    titleKey: 'types.others_review',
  },
];

export const getReviewBoxMenuItems = (
  isResponsible: boolean,
  t: LokaliseTFunction,
  selfReview: ReviewDetail[],
  downardReview: ReviewDetail[],
  upwardReview: ReviewDetail[],
  peerReview: ReviewDetail[],
  showMyResults: boolean | undefined,
  externalReview: ReviewDetail[],
  othersReview: ReviewDetail[],
  showCalibration: boolean = false,
): MenuItemsProps => [
  ...insertIf(isResponsible, {
    id: MenuItems.TEAM_PROGRESS,
    title: t('team_progress'),
    content: [],
  }),
  ...insertIf(
    isResponsible ||
      selfReview.length > 0 ||
      peerReview.length > 0 ||
      upwardReview.length > 0 ||
      externalReview.length > 0 ||
      othersReview.length > 0,
    {
      id: MenuItems.COMPLETE_REVIEW,
      title: t(showMyResults ? 'completed_reviews' : 'complete_reviews'),
      content: [
        { subHeaderTitle: t('types.auto_review'), array: selfReview },
        {
          subHeaderTitle: t('types.subordinate_review'),
          array: downardReview,
        },
        {
          subHeaderTitle: t('types.boss_review'),
          array: upwardReview,
        },
        {
          subHeaderTitle: t('types.peer_review'),
          array: peerReview,
        },
        {
          subHeaderTitle: '',
          array: externalReview,
        },
        {
          subHeaderTitle: t('types.others_review'),
          array: othersReview,
        },
      ],
    },
  ),
  ...insertIf(showCalibration, {
    id: MenuItems.CALIBRATION,
    title: t('cycles.steps.calibration'),
    content: [],
  }),
  ...insertIf(isResponsible, {
    id: MenuItems.SHARE_RESULTS,
    title: t('deliver_results'),
    content: [],
  }),
  ...insertIf(!!showMyResults, {
    id: MenuItems.MY_RESULTS,
    title: t('my_results'),
    content: [],
  }),
];

const DIRECTIONS_WITHOUT_NAME_SET = new Set([
  ReviewDirection.AUTO_REVIEW,
  ReviewDirection.SUBORDINATE_REVIEW,
  ReviewDirection.CALIBRATION_REVIEW,
]);

export const PILL_TYPE = {
  ERROR: 'error',
  SUCCESS: 'success',
  WARNING: 'warning',
  INFO: 'info',
  HIGHLIGHT: 'highlight',
  NEUTRAL: 'neutral',
  DISABLED: 'disabled',
} as const satisfies Record<string, NonNullable<PillsProps['type']>>;

export enum PerformanceCycleActions {
  IN_PROGRESS = 'IN_PROGRESS',
  SELECT_REVIEWERS = 'SELECT_REVIEWERS',
  SEE_REVIEWS = 'SEE_REVIEWS',
  FINISHED = 'FINISHED',
}
