import {
  feedInputCategoryItems,
  livestockCategoryItems,
  managementCategoryItems,
  posCategoryItems,
  quotaCategoryItems,
  taggingCategoryItems,
  transactionCategoryItems,
  unitCategoryItems,
} from "./getCategoryParameters";

type SubItems = {
  name: string;
  path: string;
  component: () => React.ComponentType | any;
};

type Item = {
  page_name: string;
  [key: string]: any;
};

export function getUserAvalablePaths(
  category: string,
  permissions: any[] = []
): SubItems[] {
  if (!Array.isArray(permissions)) {
    return [];
  }

  const filterByAccess = (items: SubItems[]): SubItems[] => {
    if (!permissions?.length) return [];

    const nameSet = new Set(
      permissions.map((item: Item) => item?.page_name).filter(Boolean)
    );

    return items?.filter((item) => item?.name && nameSet.has(item.name)) || [];
  };

  let list: SubItems[] = [];

  switch (category) {
    case "management":
      list = filterByAccess(managementCategoryItems);
      break;
    case "feed-input":
      list = filterByAccess(feedInputCategoryItems);
      break;
    case "quota":
      list = filterByAccess(quotaCategoryItems);
      break;
    case "livestock":
      list = filterByAccess(livestockCategoryItems);
      break;
    case "transactions":
      list = filterByAccess(transactionCategoryItems);
      break;
    case "pos":
      list = filterByAccess(posCategoryItems);
      break;
    case "unit":
      list = filterByAccess(unitCategoryItems);
      break;
    case "tagging":
      list = filterByAccess(taggingCategoryItems);
      break;
    default:
      break;
  }

  return list || [];
}
