import { ItemWithSubItems } from "../types/userPermissions";
import { filterByAccess } from "./filterByAccess";
import {
  feedInputCategoryItems,
  livestockCategoryItems,
  managementCategoryItems,
  posCategoryItems,
  quotaCategoryItems,
  taggingCategoryItems,
  transactionCategoryItems,
  unitCategoryItems,
} from "./getCategoryParameters";
import { getUserAvalablePaths } from "./getUserAvalablePaths";
import LogoManagement from "../assets/images/svg/management.svg?react";
import LogoFeedInput from "../assets/images/svg/feed-input.svg?react";
import LogoWage from "../assets/images/svg/wage.svg?react";
import LogoLivestock from "../assets/images/svg/livestock.svg?react";
import LogoPos from "../assets/images/svg/pos.svg?react";
import LogoTransactions from "../assets/images/svg/transactions.svg?react";
import LogoUnits from "../assets/images/svg/units.svg?react";
import LogoTagging from "../assets/images/svg/tagging.svg?react";

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

export function getUserPermissions(
  permissions: Item[] = []
): ItemWithSubItems[] {
  if (!permissions || !permissions.length) {
    return [];
  }
  const managementItems = filterByAccess(permissions, managementCategoryItems);

  const feedInputItems = filterByAccess(permissions, feedInputCategoryItems);

  const wageItems = filterByAccess(permissions, quotaCategoryItems);

  const livestockItems = filterByAccess(permissions, livestockCategoryItems);

  const posItems = filterByAccess(permissions, posCategoryItems);

  const unitItems = filterByAccess(permissions, unitCategoryItems);

  const taggingItems = filterByAccess(permissions, taggingCategoryItems);

  const transactionItems = filterByAccess(
    permissions,
    transactionCategoryItems
  );

  const items: ItemWithSubItems[] = [];

  if (managementItems.length) {
    items.push({
      en: "management",
      fa: "مدیریت",
      icon: LogoManagement,
      subItems: getUserAvalablePaths("management", permissions),
    });
  }

  if (unitItems.length) {
    items.push({
      en: "unit",
      fa: "مدیریت واحد ها",
      icon: LogoUnits,
      subItems: getUserAvalablePaths("unit", permissions),
    });
  }

  if (feedInputItems.length) {
    items.push({
      en: "feed-input",
      fa: "نهاده",
      icon: LogoFeedInput,
      subItems: getUserAvalablePaths("feed-input", permissions),
    });
  }
  if (wageItems.length) {
    items.push({
      en: "quota",
      fa: "سهمیه",
      icon: LogoWage,
      subItems: getUserAvalablePaths("quota", permissions),
    });
  }

  if (livestockItems.length) {
    items.push({
      en: "livestock",
      fa: "امور دام",
      icon: LogoLivestock,
      subItems: getUserAvalablePaths("livestock", permissions),
    });
  }

  if (taggingItems.length) {
    items.push({
      en: "livestock",
      fa: "پلاک کوبی",
      icon: LogoTagging,
      subItems: getUserAvalablePaths("tagging", permissions),
    });
  }
  if (transactionItems.length) {
    items.push({
      en: "transactions",
      fa: "تراکنش ها",
      icon: LogoTransactions,
      subItems: getUserAvalablePaths("transactions", permissions),
    });
  }
  if (posItems.length) {
    items.push({
      en: "pos",
      fa: "شرکت پرداخت",
      icon: LogoPos,
      subItems: getUserAvalablePaths("pos", permissions),
    });
  }

  return items;
}
