import { ServerlessEventObject } from "@twilio-labs/serverless-runtime-types/types";

export type UserAgentHeader = { "user-agent"?: string };

/**
 * Helper method to BLOCK discordbot from scraping API links
 * This is a bit of a hack until we process event links from UI instead of raw API
 */
export function shouldBlockRequest(event: ServerlessEventObject<{}, UserAgentHeader>): boolean {
  let headers = event?.request?.headers;
  let userAgentString = "";

  if (headers && headers['user-agent']) {
    userAgentString = headers['user-agent'];
  }

  console.log("[BlockUserAgent] got useragent", userAgentString);

  let blockList = ["Discord", "bot", "facebook"];

  console.log("[BlockUserAgent] blocked useragents are", blockList);

  let willBlock = blockList.some(term => userAgentString.includes(term));

  console.log("[BlockUserAgent] should block request: ", willBlock);
  return willBlock;
};
