import { toast } from 'react-toastify';


export const isvalidation = (attributes: any[] , t: any) => {
  const validationResults: boolean[] = [];
  let previousNames: { [key: string]: string } = {};

  attributes?.slice(1)?.forEach((item: any, index: number) => {
    if (item && Object.keys(item).length > 0) {
      const itemName = t('name');
      const itemNumber = index + 1;

      const currentItemNames = {
        name_ar: item.name_ar,
        name_en: item.name_en,
        name_de: item.name_de,
      };

      if (
        previousNames.name_ar === currentItemNames.name_ar ||
        previousNames.name_en === currentItemNames.name_en ||
        previousNames.name_de === currentItemNames.name_de
      ) {
        toast.error(`${itemName} ${t('unique_error_names')}`);
        validationResults.push(false);
      }

      previousNames = currentItemNames;

      // Check for other validation rules
      if (!item?.name_ar || !item?.name_en || !item?.name_de) {
        toast.error(`${t('required_name')} ${itemNumber}`);
        validationResults.push(false);
      } else if (!item?.type) {
        toast.error(`${t('required_type')} ${itemNumber}`);
        validationResults.push(false);
      } else if (item?.type === "image") {
        if (Array.isArray(item.AttributeValue)) {
          item.AttributeValue.slice(1)?.forEach((attrItem: any, index: number) => {
            if (attrItem && Object.keys(attrItem).length > 0) {
              if (attrItem.image === null) {
                toast.error(`${t('required_image')} ${index + 1}`);
                validationResults.push(false);
              } else if (!attrItem?.value_ar || !attrItem?.value_en || !attrItem?.value_de) {
                toast.error(`${t('required_text')} ${index + 1}`);
                validationResults.push(false);
              } else {
                validationResults.push(true);
              }
            }
          });
        }
      } else if (item?.type === "color") {
        if (Array.isArray(item.AttributeValue)) {
          item.AttributeValue.slice(1)?.forEach((attrItem: any, index: number) => {
            if (attrItem && Object.keys(attrItem).length > 0) {
              const hexColorRegex = /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
              if (attrItem && !hexColorRegex.test(attrItem?.value_en)) {
                toast.error(`${t('required_color')} ${index + 1}`);
                validationResults.push(false);
              } else {
                validationResults.push(true);
              }
            }
          });
        }
      } else if (item?.type === "text") {
        if (Array.isArray(item.AttributeValue)) {
          item.AttributeValue.slice(1)?.forEach((attrItem: any, index: number) => {
            if (attrItem && Object.keys(attrItem).length > 0) {
              if (attrItem && (!attrItem?.value_ar || !attrItem?.value_en || !attrItem?.value_de)) {
                toast.error(` ${t('required_text')} ${index + 1}`);
                validationResults.push(false);
              } else {
                validationResults.push(true);
              }
            }
          });
        }
      } else {
        validationResults.push(true);
      }
    }
  });

  return validationResults;
};
