import type { SDK, DefineAPI } from "caido:plugin";
import type { Request, Response } from "caido:utils";
// import { ImplicitGrantController } from "./controller/implictGrant";
// import { AuthZCodeGrantController } from "./controller/authZCodeGrant";
import { PKCECheck } from "./controller/PKCECheck";
import { redirectUriCheck } from "./controller/redirectUriCheck";

export type API = DefineAPI<{}>;

// const implicitGrantController = new ImplicitGrantController();
// const authZCodeGrantController = new AuthZCodeGrantController();
const pkceCheckController = new PKCECheck();
const redirectUriCheckController = new redirectUriCheck();

export function init(sdk: SDK<API>) {
  // sdk.events.onInterceptRequest(async (sdk, req: Request) => {
  //   const result = csrfCheck.checker(req);

  //   if (result) {
  //     await sdk.findings.create({
  //       title: "Possible SSO Request Detected",
  //       description: `SSO-related parameters detected in request:\n\n${req.getMethod()} ${req.getUrl()} : ${result}`,
  //       request: req,
  //       reporter: "",
  //     });
  //   }
  // });
  sdk.events.onInterceptRequest(
    async(sdk: SDK<DefineAPI<{}>, {}>, req: Request) => {
      await redirectUriCheckController.onRequest(sdk, req);
    }
  ); 

  sdk.events.onInterceptResponse(
    async (sdk: SDK<DefineAPI<{}>, {}>, req: Request, resp: Response) => {
      await pkceCheckController.test(sdk, req);
      await redirectUriCheckController.onResponse(sdk, req, resp);

      // sdk.events.onInterceptRequest(async (sdk, req: Request) => {
      //   const result =
      //     authZCodeGrantController.testReq(req) ||
      //     implicitGrantController.testReq(req);

      //   if (result) {
      //     await pkceCheckController.test(sdk, req);

      //     await sdk.findings.create({
      //       title: "Possible SSO Request Detected",
      //       description: `SSO-related parameters detected in request:\n\n${req.getMethod()} ${req.getUrl()} : ${result}`,
      //       request: req,
      //       reporter: "",
      //     });
    }
  );
}