"use client";

import { withFormik, type FormikBag } from "formik";
import type { JobInfoFormProps, JobInfoFormValues } from "./types";
import { INITIAL_JOB_INFO_VALUES } from "./constant";
import { JobInfoValidationSchema } from "./validation";
import InnerJobInfoForm from "./InnerJobInfoForm";

const JobInfoForm = withFormik<JobInfoFormProps, JobInfoFormValues>({
  displayName: "JobInfoForm",

  enableReinitialize: true,

  mapPropsToValues: (props) => {
    return props.data?.jobInfo || INITIAL_JOB_INFO_VALUES;
  },

  validationSchema: JobInfoValidationSchema,

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

    props.update({ jobInfo: values });
    props.setStep((prev) => prev + 1);
    setSubmitting(false);
  },
})(InnerJobInfoForm);

export default JobInfoForm;
