import dayjs from "dayjs";

import { Order } from "../../models/landing";
import { OrderFormValues } from "../../components/order-form";

const removeAllSpaces = (str: string) => str.replace(/\s+/g, '');

const getValidCarBodyStyle = (fieldValue: string) => {
  const carBodyAsNumber = Number(fieldValue);
  return Number.isNaN(carBodyAsNumber) ? undefined : carBodyAsNumber;
};

export const formatFormValues = ({ phone, carNumber, carBody, carColor, carLocation, availableDatetimeBegin, availableDatetimeEnd }: OrderFormValues): Order.Create => {
  return {
    customer: {
      phone
    },
    car: {
      number: removeAllSpaces(carNumber),
      body: getValidCarBodyStyle(carBody),
      color: carColor
    },
    washing: {
      location: carLocation,
      begin: dayjs(availableDatetimeBegin).toISOString(),
      end: dayjs(availableDatetimeEnd).toISOString(),
    }
  };
};