
import * as Yup from "yup";
import { buildFormData } from "../../api/helper/buildFormData";
import moment from 'moment';
import * as dayjs from 'dayjs'

interface formUtilCommon {
  number: number,
  value: number
}

interface ObjectToEdit extends formUtilCommon {

  id?: number,

}

interface InitialValues extends ObjectToEdit {

}
interface ValidateSchema extends formUtilCommon {

}

export const getInitialValues = (objectToEdit: any | null = null): any => {
  //@ts-ignore

  const products = [] as any;
  const categories = [] as any;
  const language = localStorage.getItem("language") ?? "en"

  // Map over the items array and push items into the appropriate array
  objectToEdit?.items?.forEach((item: any) => {
    if (item?.itemable_type === "product") {
      products.push({ value: item?.itemable?.id, label: item?.itemable?.name[language] });
    } else if (item?.itemable_type === "category") {
      categories.push({ value: item?.itemable?.id, label: item?.itemable?.name[language] });
    }
  });
  return {
    id: objectToEdit?.id ?? null,
    name: objectToEdit?.name ,
    code: objectToEdit?.code ,
    //@ts-ignore
    active: objectToEdit?.active_to ? [dayjs(objectToEdit?.active_from), dayjs(objectToEdit?.active_to)] : "",
    minimum_total_to_order: objectToEdit?.minimum_total_to_order ,
    maximum_number_of_uses_per_user: objectToEdit?.maximum_number_of_uses_per_user ,
    maximum_number_of_uses: objectToEdit?.maximum_number_of_uses ,

    coupon_value: objectToEdit?.coupon_value ,
    coupon_type: objectToEdit?.coupon_type ,
    discount_type: objectToEdit?.discount_type ,
    product_attr: products ,
    category_attr: categories ,
    status: objectToEdit?.status ?? 0,


  };
};

export const getInitialValuesForAdd = (objectToEdit: any | null = null): any => {
  return {
    // id: '',
    name: null,
    code: null,
    active: null,
    minimum_total_to_order: null,
    maximum_number_of_uses_per_user: null,
    maximum_number_of_uses: null,
    coupon_value: null,
    coupon_type: null,
    discount_type: null,
    product_attr: null,
    category_attr: null,
    status: 0,

    

  };
};

export const getValidationSchema = (editMode: boolean = false): Yup.Schema<any> => {
  // Validate input
  return Yup.object().shape({
    name: Yup.string().required('Required'),
    code: Yup.string().required('Required'),
    coupon_value: Yup.string().required('Required'),
    active: Yup.mixed().required('Required'),
    discount_type: Yup.string().required('Required'),
    coupon_type: Yup.string().required('Required'),
    minimum_total_to_order: Yup.number().required('Required'),
    maximum_number_of_uses: Yup.number().required('Required'),
    maximum_number_of_uses_per_user: Yup.number().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
}