import { GetServerSideProps } from "next";
import getSiteConfig from "libs/getSiteConfig";

export default function Opensearch(host: ReturnType<typeof getSiteConfig>) {
  return `<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
                       xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  <ShortName>${host.siteName}</ShortName>
  <Description>Search in ${host.siteName}</Description>
  <InputEncoding>UTF-8</InputEncoding>
  <Image width="16" height="16" type="image/x-icon">https://${host.host}/favicon.ico</Image>
  <Url type="text/html" method="get" template="https://${host.host}/search?q={searchTerms}&amp;ref=opensearch"/>
  <moz:SearchForm>https://${host.host}/search</moz:SearchForm>
</OpenSearchDescription>`;
}

export const getServerSideProps: GetServerSideProps<{}> = ({ res, req }) => {
  res.setHeader("Content-Type", "text/xml");
  res.write(Opensearch(getSiteConfig(req.headers.host)));
  res.end();

  return Promise.resolve({
    props: {},
  });
};
