import { type ShiftRotationDetail } from 'src/types/shifts';

import {
  type RotationDayShiftRef,
  type ShiftRotationFormData,
} from '../../../form';

export const parseRotationToFormData = (
  rotation: ShiftRotationDetail,
): ShiftRotationFormData => ({
  name: rotation.name,
  description: rotation.description ?? '',
  days: rotation.days.map((day, dayIndex) => ({
    id: `day-${dayIndex}`,
    type: day.type === 'WORK' ? 'shift' : 'rest',
    shifts:
      day.type === 'WORK'
        ? day.shifts.map(
            (shift, shiftIndex): RotationDayShiftRef => ({
              id: dayIndex * 100 + shiftIndex,
              shiftCode: shift.shiftCode,
              name: shift.name ?? '',
              color: shift.color,
              description: shift.description ?? undefined,
              timeSlots: shift.timeSlots.map(({ startTime, endTime }) => ({
                startTime,
                endTime,
              })),
            }),
          )
        : [],
  })),
});
