import { Incident, IncidentEdit } from 'myApi';
import { USERS } from '../MOCK';
import moment from 'moment';

export type TSubmitData = Omit<Incident, 'assigned_to'> & {
  assigned_to: number | null | string;
};

export const prepareDataForSubmit = (card: TSubmitData): IncidentEdit => {
  const modifiedIncident: IncidentEdit = {};

  if (card.assigned_to) {
    if (typeof card.assigned_to === 'string') {
      modifiedIncident.assigned_to = USERS.find(
        (user) => user.label === card.assigned_to,
      )?.value;
    } else {
      modifiedIncident.assigned_to = card.assigned_to;
    }
  }
  if (card.status || card.status === 0) {
    modifiedIncident.status = card.status;
    if (card.status === 0) {
      modifiedIncident.assigned_to = null;
    }
  }
  if (card.deadline) {
    modifiedIncident.deadline = moment(card.deadline).format('YYYY-MM-DD');
  }
  if (card.comment) {
    modifiedIncident.comment = card.comment;
  }

  return modifiedIncident;
};
