
import * as Yup from "yup";
import { buildFormData } from "../../api/helper/buildFormData";
import { ImageBaseURL } from "../../api/config";
import { fetchImage } from "../../Hooks/imageUrlToFile";
import { useState } from "react";


export const getInitialValues = (objectToEdit: any | null = null): any => {
  
  const Attribute = objectToEdit[1]?.map((item: any) => {
    const AttributeValue = item?.attribute_value?.map((item:any)=>(
      {
        value_ar: item?.value["ar"],
        value_en: item?.value["en"],
        value_de: item?.value["de"],
         id  : item?.id,
         image : item?.image,
         Edited:false
      
      } )
   )
    return (
      {
        name_ar: item?.name["ar"],
        name_en: item?.name["en"],
        name_de: item?.name["de"],
        type  : item?.type,
        id  : item?.id,
        icon  : item?.icon,
        category_id : item?.category_id,
        AttributeValue : [{},...AttributeValue ?? {}],
        Edited:false
        
      }
  )
  })
  const imageUrl = ImageBaseURL + objectToEdit[0]?.photo?.replace("public", "/storage")

 
  return {
    id: objectToEdit[0]?.id ?? 0,
    name_ar: objectToEdit[0]?.name["ar"],
    name_en: objectToEdit[0]?.name["en"],
    name_de: objectToEdit[0]?.name["de"],
    parent_id: objectToEdit[0]?.parent_id ?? "",
    photo: objectToEdit[0]?.photo ?? '',
    Attribute: [{},...Attribute],
    removedAttribute:[],
    removedAttributeValue:[],


  };
};
export const getInitialValuesForAdd = (objectToEdit: any | null = null): any => {
  return {
    name_ar: '',
    name_en: '',
    name_de: '',
    parent_id: null,
    photo: '',
    Attribute: [{}],


  };
};



export const getValidationSchema = (editMode: boolean = false): Yup.Schema<any> => {
  // Validate input
  return Yup.object().shape({
    name_ar: Yup.string().required('Required'),
    name_en: Yup.string().required('Required'),
    name_de: Yup.string().required('Required'),
    // parent_id: Yup.string().required('Required'),
    photo: Yup.string().required('Required'),

  });
};




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


  const formData = new FormData();
  buildFormData(formData, data);
  return formData;
};

export const ChangeDataToPrint = (data: any) => {

  let new_array = data
  for (let i = 0; i < data.length; i++) {
    new_array[i]['status'] = !data[i]['deleted_at'] ? 'available' : 'unavailable'
    delete new_array[i]['deleted_at']
  }
  return new_array
}