
import * as Yup from "yup";
import { ImageBaseURL } from "../../api/config";



export const getInitialValues = (objectToEdit: any) => {
  if (!objectToEdit || !objectToEdit?.products) {
    return {};
  }
  const products =  objectToEdit?.products?.map((item: any) => {
    // console.log(item,"item");


   const formattedData = item?.info ? Object.entries(item?.info).map(([key, value], index) => ({
    [`Description`]: key,
    [`key`]: value,
  })) : [];

  // console.log(formattedData,"formattedData");
  const images_product = item?.images?.map((item:any)=>{
    return item?.path
  })
  console.log(images_product,"images_product");
  
  
    return ({
    
      name_ar: item?.name["ar"],
      name_en: item?.name["en"],
      name_de: item?.name["de"],
      description_ar: item?.description["ar"],
      description_en: item?.description["en"],
      description_de: item?.description["de"],
      images: images_product,

      main_photo: item?.main_photo,
      price: item?.price,
      // quantity: item?.quantity,
      // products_attributes: item?.products_attributes,
      id:item?.id,
      info:formattedData

    })
  }) 

  const productsInfo = objectToEdit?.products?.info || {};
  const formattedData = Object.entries(productsInfo).map(([key, value], index) => ({
    [`${index}.Description`]: key,
    [`${index}.key`]: value,
  }));
  console.log(products,"products");


  return {
    id:objectToEdit?.id,
    name: objectToEdit?.name ?? "",
    name_ar: objectToEdit?.name?.ar ?? '',
    name_en: objectToEdit?.name?.en ?? '',
    name_de: objectToEdit?.name?.de ?? '',
    description_ar: objectToEdit?.description?.ar ?? '',
    description_en: objectToEdit?.description?.en ?? '',
    description_de: objectToEdit?.description?.de ?? '',
    // attribute: objectToEdit?.attribute ?? "",
    category_id: objectToEdit?.category?.id ?? "",
    price: objectToEdit?.price ?? "",

    variable: [{}, ...products],
    // info: [undefined, ...formattedData] ?? [],
    removedVariant:[],
  }
};

export const getInitialValuesAdd = (objectToEdit: any | null = null) => {
 
  return {
    id: "",
    name:  "",
    name_ar:'',
    name_en: '',
    name_de: '', 
    description_ar:'',
    description_en: '',
    description_de: '', 
    price: "",
    category_id:  "",
    variable: [],
    info:  [],

  }
};

export const getValidationSchema = (editMode: boolean = false) => {
  // validate input  
  return Yup.object().shape({
    name_ar: Yup.string().required('Required'),
    name_en: Yup.string().required('Required'),
    name_de: Yup.string().required('Required'),
    category_id: Yup.string().required('Required'),
    
   

  });
};

export const getDataToSend = (values: any): FormData => {
  const data = { ...values };

  return data;
};

