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

export type Props = {
  children: ComponentChildren;
  title?: string;
  name?: string;
  url: URL;
  description?: string;
  context?: { type: string };
  searchResults?: SearchResult;
};

export const MainLayout = (
  { children, url, title, 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}</>;
};
