export async function onBeforeRender(pageContext: PageContext) {
  if (!pageContext.current_user)
    return {
      pageContext: {
        pageProps: {
          logged_in: false,
        },
        status: 401,
      },
    };

  const blockedAppeal = await pageContext.kv?.get(
    `blockedappeal_${pageContext.current_user.id}`
  );
  const disabledStatus = await pageContext.kv?.get("appeal_disabled");
  const openAppeals = await pageContext.kv?.list({
    prefix: `appeal_${pageContext.current_user.id}`,
  });

  return {
    pageContext: {
      pageProps: {
        can_appeal:
          !Boolean(disabledStatus) &&
          !Boolean(blockedAppeal) &&
          !Boolean(
            openAppeals.keys.find(
              (appeal) => (appeal.metadata as { [k: string]: any }).open
            )
          ),
        can_toggle:
          pageContext.current_user?.permissions & (1 << 0) ||
          pageContext.current_user?.permissions & (1 << 11),
        disabled: Boolean(disabledStatus),
        logged_in: true,
      },
      status: pageContext.current_user ? 200 : 401,
    },
  };
}

export const description = "Appeal your Discord ban here.";
export const title = "Appeals - Car Crushers";
