/**
 * Filters the menu structure based on intersections with provided UUIDs
 * @param {string[]} uuids - Array of UUIDs to check for intersection
 * @param {Array} menu - The original menu structure
 * @returns {Array} - Filtered menu structure with only matching items
 */
import Menu from "@/components/menu/store"; // Assuming you have a menu structure imported

// Define TypeScript interfaces for menu structure
interface LanguageTranslation {
  tr: string;
  en: string;
}

interface MenuThirdLevel {
  name: string;
  lg: LanguageTranslation;
  siteUrl: string;
}

interface MenuSecondLevel {
  name: string;
  lg: LanguageTranslation;
  subList: MenuThirdLevel[];
}

interface MenuFirstLevel {
  name: string;
  lg: LanguageTranslation;
  subList: MenuSecondLevel[];
}

// Define interfaces for the filtered menu structure
interface FilteredMenuThirdLevel {
  name: string;
  lg: LanguageTranslation;
  siteUrl: string;
}

interface FilteredMenuSecondLevel {
  name: string;
  lg: LanguageTranslation;
  subList: FilteredMenuThirdLevel[];
}

interface FilteredMenuFirstLevel {
  name: string;
  lg: LanguageTranslation;
  subList: FilteredMenuSecondLevel[];
}

export type { LanguageTranslation };

function transformMenu(siteUrls: string[]) {
  // Process the menu structure
  const filteredMenu: FilteredMenuFirstLevel[] = Menu.reduce(
    (acc: FilteredMenuFirstLevel[], firstLevel: MenuFirstLevel) => {
      // Create a new first level item with empty subList
      const newFirstLevel: FilteredMenuFirstLevel = {
        name: firstLevel.name,
        lg: { ...firstLevel.lg },
        subList: [],
      };

      // Process second level items
      firstLevel.subList.forEach((secondLevel: MenuSecondLevel) => {
        // Create a new second level item with empty subList
        const newSecondLevel: FilteredMenuSecondLevel = {
          name: secondLevel.name,
          lg: { ...secondLevel.lg },
          subList: [],
        };

        // Process third level items
        secondLevel.subList.forEach((thirdLevel: MenuThirdLevel) => {
          // Check if the third level's siteUrl matches exactly
          if (
            thirdLevel.siteUrl &&
            siteUrls.some((url) => url === thirdLevel.siteUrl)
          ) {
            // Create a modified third level item
            const newThirdLevel: FilteredMenuThirdLevel = {
              name: thirdLevel.name,
              lg: { ...thirdLevel.lg },
              siteUrl: thirdLevel.siteUrl,
            };

            // Add the modified third level to the second level's subList
            newSecondLevel.subList.push(newThirdLevel);
          }
        });

        // Only add the second level to the first level if it has any matching third level items
        if (newSecondLevel.subList.length > 0) {
          newFirstLevel.subList.push(newSecondLevel);
        }
      });

      // Only add the first level to the result if it has any matching second level items
      if (newFirstLevel.subList.length > 0) {
        acc.push(newFirstLevel);
      }

      return acc;
    },
    []
  );

  return filteredMenu;
}

export { transformMenu };
