import * as Yup from "yup";
import { Package, PackageInitialValues } from "../../../types/Package";
import { arrayToObject } from "../../../utils/arrayToObject";
import { objectToArray } from "../../../utils/objectToArray";
export const getInitialValues = (objectToEdit: Partial<Package>): PackageInitialValues => {
  console.log(objectToEdit,"objectToEdit");
  
  const configuration = Array.isArray(objectToEdit?.configuration) ? objectToEdit?.configuration : objectToArray(objectToEdit?.configuration)
  return {
    id: objectToEdit?.id ?? null,
    name: objectToEdit?.name ?? null,
    price: objectToEdit?.price ?? null,
    grade_id: objectToEdit?.grade_id ?? null,
    curriculums_ids: objectToEdit?.curriculums_ids ?? [],
    lessons_ids: objectToEdit?.lessons_ids ?? [],
    subjects_ids: objectToEdit?.subjects_ids ?? [],
    units_ids: objectToEdit?.units_ids ?? [],
    configuration: configuration ?? [{key:"",value:""}],
  };
};

export const getValidationSchema = () => {
  return Yup.object().shape({
    name: Yup.string().required("validation.required"),
    price: Yup.number().required("validation.required").typeError("validation.Must_be_a_number"),
    grade_id: Yup.string().required("validation.required"),
    curriculums_ids: Yup.array().of(Yup.number().required()).min(1,"validation.must_have_on_item").required("validation.required"),
    lessons_ids: Yup.array().of(Yup.number().required()).min(1,"validation.must_have_on_item").required("validation.required"),
    subjects_ids: Yup.array().of(Yup.number().required()).min(1,"validation.must_have_on_item").required("validation.required"),
    units_ids: Yup.array().of(Yup.number().required()).min(1,"validation.must_have_on_item").required("validation.required"),
  });
};
