import { omit } from 'lodash-es';

import {
  Incidence,
  type SegmentationGroup,
  UserTrackingStatus,
} from 'src/types/timeTracking';

import { type TwoFieldGroup } from 'src/components/AdvancedFilter/types';
import { putCheckInOptions } from 'src/components/AdvancedFilter/utils';

const INCIDENCES_ORDER = [
  Incidence.LATE,
  Incidence.ABSENT,
  Incidence.LOCATION_INCIDENCE,
  'PERMIT_DAY',
  Incidence.AUTO_CLOSE,
  Incidence.UNDERWORKED,
  Incidence.EXTRA_HOURS,
];

export const USER_STATUSES = Object.values(UserTrackingStatus).map(status => ({
  label: `STATUS_FILTER_${status}`,
  value: status,
}));

export const COMMON_INCIDENCES_FILTERS = Object.values(
  omit(Incidence, 'FACIAL_RECOGNITION_SKIPPED', 'NOT_CLOCKED_IN'),
)
  .sort((a, b) => INCIDENCES_ORDER.indexOf(a) - INCIDENCES_ORDER.indexOf(b))
  .map(incidence => ({
    label: incidence,
    value: incidence,
  }));

const FILTER_INCIDENCES_OPTIONS = [
  ...Object.values(
    omit(Incidence, 'FACIAL_RECOGNITION_SKIPPED', 'NOT_CLOCKED_IN'),
  ),
  'PERMIT_DAY', // Shown as an incidence filter but it's a flag
];

export const EXTENDED_INCIDENCES_FILTERS = FILTER_INCIDENCES_OPTIONS.sort(
  (a, b) => INCIDENCES_ORDER.indexOf(a) - INCIDENCES_ORDER.indexOf(b),
).map(incidence => ({
  label: incidence,
  value: incidence as Incidence,
}));

export const getInitialFilterValues = (
  segmentationGroups: SegmentationGroup[],
  regionSites: TwoFieldGroup[],
) => ({
  segmentationItems: putCheckInOptions(segmentationGroups || []),
  users: [],
  statusItems: putCheckInOptions([]),
  incidencesItems: putCheckInOptions([]),
  siteIds: putCheckInOptions(regionSites || []),
  hasHoliday: false,
  hasTimeOff: false,
  includeDeactivated: false,
});
