import RequestError, { HttpStatusCode } from "../../../helper/request_error.js";
import Client, { IClient } from "../../../models/client.js";

export async function getClientWithOrigin(client_id: string, origin: string) {
   const client = await Client.findOne({
      client_id,
   });

   const clientNotFoundError = new RequestError(
      "Client not found!",
      HttpStatusCode.BAD_REQUEST
   );

   if (!client) throw clientNotFoundError;

   const clientUrl = new URL(client.redirect_url);

   if (clientUrl.hostname !== origin) throw clientNotFoundError;

   return client;
}
