export async function onBeforeRender(pageContext: PageContext) {
  const { current_user: currentUser } = pageContext;

  if (!currentUser)
    return {
      pageContext: {
        pageProps: {
          logged_in: false,
        },
        status: 401,
      },
    };

  const newItemPermissions = {
    game_ban: [1 << 5],
    inactivity: [1 << 2, 1 << 9, 1 << 10],
    infraction: [1 << 0, 1 << 2, 1 << 6, 1 << 7]
  };

  const newItemNames: { [k: string]: string } = {
    game_ban: "Game Ban",
    inactivity: "Inactivity Notice",
    infraction: "Infraction",
  };

  const typePermissions = {
    appeal: [1 << 0, 1 << 1],
    gma: [1 << 5],
    report: [1 << 5],
  };

  const typeNames: { [k: string]: string } = {
    appeal: "Discord Appeals",
    gma: "Game Appeals",
    report: "Game Reports",
  };

  const allowedNewItems = [];
  const allowedTypes = [];

  for (const [item, ints] of Object.entries(newItemPermissions)) {
    if (ints.find((i) => currentUser.permissions & i))
      allowedNewItems.push({ name: newItemNames[item], value: item })
  }

  for (const [type, ints] of Object.entries(typePermissions)) {
    if (ints.find((i) => currentUser.permissions & i))
      allowedTypes.push({ name: typeNames[type], value: type });
  }

  if (!allowedTypes.length)
    return {
      pageContext: {
        pageProps: {
          entry_types: [],
          item_types: [],
          logged_in: true,
        },
        status: 403,
      },
    };

  return {
    pageContext: {
      pageProps: {
        entry_types: allowedTypes,
        item_types: allowedNewItems,
        logged_in: true,
      },
    },
  };
}
