import { ComponentChildren } from "preact";
import Search from "@islands/Search.tsx";
import { GenericResource } from "@lib/types.ts";

export type Props = {
  children: ComponentChildren;
  title?: string;
  name?: string;
  url: URL | string;
  description?: string;
  context?: { type: string };
  searchResults?: GenericResource[];
};

export const MainLayout = (
  { children, url, context, searchResults }: Props,
) => {
  const _url = typeof url === "string" ? new URL(url) : url;
  const hasSearch = _url?.search?.includes("q=");

  if (hasSearch) {
    return (
      <Search
        q={_url.searchParams.get("q")}
        {...context}
        results={searchResults}
      />
    );
  }

  return <>{children}</>;
};
