
// const filterItemByPeriod = (items: any[], period: string): any[] => {
//     if (period === 'all') return items;


//     const now = new Date();
//     const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

//     return items.filter((item) => {
//       const date = new Date(item.date);

//       switch (period) {
//         case 'today': {
//           const itemDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
//           return itemDay.getTime() === today.getTime();
//         }

//         case 'week': {
//           const weekStart = new Date(today);
//           weekStart.setDate(today.getDate() - today.getDay() + 1); // Lundi de cette semaine
//           return date >= weekStart && date <= now;
//         }

//         case 'month': {
//           return (
//             date.getMonth() === now.getMonth() &&
//             date.getFullYear() === now.getFullYear()
//           );
//         }

//         case '30days': {
//           const thirtyDaysAgo = new Date(today);
//           thirtyDaysAgo.setDate(today.getDate() - 30);
//           return date >= thirtyDaysAgo && date <= now;
//         }

//         case 'quarter': {
//           const currentQuarter = Math.floor(now.getMonth() / 3);
//           const devisQuarter = Math.floor(date.getMonth() / 3);
//           return (
//             devisQuarter === currentQuarter &&
//             date.getFullYear() === now.getFullYear()
//           );
//         }

//         case 'year': {
//           return date.getFullYear() === now.getFullYear();
//         }

//         default:
//           return true;
//       }
//     });
// };

type PeriodType = 'all' | 'today' | 'week' | 'month' | 'quarter' | 'year' | CustomPeriod;

interface CustomPeriod {
  id: 'custom';
  start: string;
  end: string;
}

// Fonction utilitaire pour vérifier si c'est une période custom
const isCustomPeriod = (period: PeriodType): period is CustomPeriod => {
  return typeof period === 'object' && period !== null && period.id === 'custom';
};



const filterItemByPeriod = <T extends { date?: string; date_creation?: string }>(
  items: T[],
  period: PeriodType,
  dateField: 'date' | 'date_creation' = 'date'
): T[] => {
  const now = new Date();
  now.setHours(23, 59, 59, 999);

  // Si "all", retourner tous les items
  if (period === 'all') {
    return items;
  }

  // Si période custom avec start et end
  if (isCustomPeriod(period)) {
    const startDate = new Date(period.start);
    startDate.setHours(0, 0, 0, 0);
    
    const endDate = new Date(period.end);
    endDate.setHours(23, 59, 59, 999);

    return items.filter(item => {
      const itemDate = item[dateField];
      if (!itemDate) return false;
      
      const date = new Date(itemDate);
      return date >= startDate && date <= endDate;
    });
  }

  // Périodes prédéfinies
  let startDate: Date;

  switch (period) {
    case 'today':
      startDate = new Date(now);
      startDate.setHours(0, 0, 0, 0);
      break;
    case 'week':
      startDate = new Date(now);
      startDate.setDate(now.getDate() - 7);
      startDate.setHours(0, 0, 0, 0);
      break;
    case 'month':
      startDate = new Date(now);
      startDate.setMonth(now.getMonth() - 1);
      startDate.setHours(0, 0, 0, 0);
      break;
    case 'quarter':
      startDate = new Date(now);
      startDate.setMonth(now.getMonth() - 3);
      startDate.setHours(0, 0, 0, 0);
      break;
    case 'year':
      startDate = new Date(now);
      startDate.setFullYear(now.getFullYear() - 1);
      startDate.setHours(0, 0, 0, 0);
      break;
    default:
      return items;
  }

  return items.filter(item => {
    const itemDate = item[dateField];
    if (!itemDate) return false;
    
    const date = new Date(itemDate);
    return date >= startDate && date <= now;
  });
};

const getPreviousPeriodItems = <T extends { date?: string; date_creation?: string }>(
  items: T[],
  period: PeriodType,
  dateField: 'date' | 'date_creation' = 'date'
): T[] => {
  const now = new Date();

  // Si "all", pas de période précédente
  if (period === 'all') {
    return [];
  }

  let startDate: Date;
  let endDate: Date;

  // Si période custom
  if (isCustomPeriod(period)) {
    const customStart = new Date(period.start);
    const customEnd = new Date(period.end);
    
    // Calculer la durée de la période custom
    const duration = customEnd.getTime() - customStart.getTime();
    
    // Période précédente = même durée avant la date de début
    endDate = new Date(customStart.getTime() - 1); // Jour avant le début
    endDate.setHours(23, 59, 59, 999);
    
    startDate = new Date(endDate.getTime() - duration);
    startDate.setHours(0, 0, 0, 0);
  } else {
    // Périodes prédéfinies
    switch (period) {
      case 'today':
        // Hier
        startDate = new Date(now);
        startDate.setDate(now.getDate() - 1);
        startDate.setHours(0, 0, 0, 0);
        endDate = new Date(startDate);
        endDate.setHours(23, 59, 59, 999);
        break;
      case 'week':
        // Semaine précédente (J-14 à J-7)
        startDate = new Date(now);
        startDate.setDate(now.getDate() - 14);
        startDate.setHours(0, 0, 0, 0);
        endDate = new Date(now);
        endDate.setDate(now.getDate() - 7);
        endDate.setHours(23, 59, 59, 999);
        break;
      case 'month':
        // Mois précédent (M-2 à M-1)
        startDate = new Date(now);
        startDate.setMonth(now.getMonth() - 2);
        startDate.setHours(0, 0, 0, 0);
        endDate = new Date(now);
        endDate.setMonth(now.getMonth() - 1);
        endDate.setHours(23, 59, 59, 999);
        break;
      case 'quarter':
        // Trimestre précédent (M-6 à M-3)
        startDate = new Date(now);
        startDate.setMonth(now.getMonth() - 6);
        startDate.setHours(0, 0, 0, 0);
        endDate = new Date(now);
        endDate.setMonth(now.getMonth() - 3);
        endDate.setHours(23, 59, 59, 999);
        break;
      case 'year':
        // Année précédente (A-2 à A-1)
        startDate = new Date(now);
        startDate.setFullYear(now.getFullYear() - 2);
        startDate.setHours(0, 0, 0, 0);
        endDate = new Date(now);
        endDate.setFullYear(now.getFullYear() - 1);
        endDate.setHours(23, 59, 59, 999);
        break;
      default:
        return [];
    }
  }

  return items.filter(item => {
    const itemDate = item[dateField];
    if (!itemDate) return false;
    
    const date = new Date(itemDate);
    return date >= startDate && date <= endDate;
  });
};



const hasSpecialChars = (text: string): boolean => {
  const regexSpecialChars =
    /[&é~"#'{(\[\-|è`_\\ç^à@)\]°=+}^¨$£¤ù%*µ,?;.:/!§€]/;

  const forbiddenWord = /\bBAOR01\b/;

  return regexSpecialChars.test(text) || forbiddenWord.test(text);
};



// Export de la fonction utilitaire
export { filterItemByPeriod, getPreviousPeriodItems, hasSpecialChars };