import { Handlers } from "$fresh/server.ts";
import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { getTypeSenseClient } from "@lib/typesense.ts";
import { json } from "@lib/helpers.ts";
import { parseResourceUrl, searchResource } from "@lib/search.ts";

export const handler: Handlers = {
  async GET(req, ctx) {
    const session = ctx.state.session;
    if (!session) {
      throw new AccessDeniedError();
    }

    const searchParams = parseResourceUrl(req.url);

    // Perform the Typesense search
    const searchResults = await searchResource(searchParams);

    return json(searchResults);
  },
};
