"use client";

import React from "react";
import { withFormik, type FormikBag } from "formik";
import InnerJobRequestForm from "./InnerJobRequestForm";
import type { JobRequestFormProps, JobRequestFormValues } from "./types";
import { JobRequestValidationSchema } from "./validation";
import { JOB_REQUEST_EMPTY_VALUES } from "./constant";

const JobRequestForm = withFormik<JobRequestFormProps, JobRequestFormValues>({
  displayName: "JobRequestForm",

  enableReinitialize: true,

  mapPropsToValues: (props) => {
    return {
      jobRequests:
        props.data?.jobRequests?.length > 0
          ? props.data.jobRequests
          : JOB_REQUEST_EMPTY_VALUES.jobRequests,
    };
  },

  validationSchema: JobRequestValidationSchema,

  handleSubmit: async (
    values,
    bag: FormikBag<JobRequestFormProps, JobRequestFormValues>,
  ) => {
    const { props, setSubmitting } = bag;

    props.update({
      jobRequests: values.jobRequests,
    });

    props.setStep((prev) => prev + 1);

    setSubmitting(false);
  },
})(InnerJobRequestForm);

export default JobRequestForm;
