import { type TFunction } from 'react-i18next';

import { pickBy } from 'lodash-es';

import { type LokaliseTFunction } from 'src/utils/i18n';

import { type AutocompleteField, ProfileField } from './types';

export const uncheckAllOptions = values =>
  Object.keys(values).reduce((obj, cur) => ({ ...obj, [cur]: false }), {});

export const putCheckInOptions = values =>
  values.reduce((obj, cur) => ({ ...obj, [cur.value]: cur.checked }), {});

export const getOptionsSelected = (selectedFields, allFields) => {
  const selectedIds = Object.keys(selectedFields)
    .filter(key => selectedFields[key])
    .map(Number);

  const selectedCount = selectedIds.length;

  return {
    selectedCount,
    allSelected: selectedCount === allFields.length,
    someSelected: selectedCount > 0 && selectedCount < allFields.length,
  };
};

export const getAllProfileFields = (
  t: TFunction | LokaliseTFunction,
): AutocompleteField[] => [
  {
    value: ProfileField.COMPLETE_NAME,
    label: t('COMPLETE_NAME'),
  },
  {
    value: ProfileField.EMAIL,
    label: t('EMAIL'),
  },
  {
    value: ProfileField.DIRECT_BOSS,
    label: t('DIRECT_BOSS'),
  },
  {
    value: ProfileField.BIRTHDATE,
    label: t('BIRTHDATE'),
  },
  {
    value: ProfileField.HIRING_DATE,
    label: t('HIRING_DATE'),
  },
  {
    value: ProfileField.EMPLOYEE_INTERNAL_ID,
    label: t('EMPLOYEE_INTERNAL_ID'),
  },
];

export const selectedItemsToString = (items: Record<string, boolean>) =>
  Object.keys(pickBy(items)).toString();
