import createContentPageRouteHandler from "@insite/server-framework/CreateContentPageRouteHandler";

const robots = createContentPageRouteHandler(
    "robotstxtpage",
    "RobotsTxt/RobotsTxtContent",
    "text/plain",
    (contentWidget, request) => {
        // if disallow flag is on, ignore the cms content and return the default disallow text
        const disallowText = "User-Agent: *\nDisallow: /\n";
        const sitemap = contentWidget?.generalFields.sitemap === true;
        const disallow = contentWidget?.generalFields.disallow === true;
        const sitemapIndexText = sitemap
            ? `sitemap: https://${request.headers.host?.toLowerCase()}/sitemapindex.xml`
            : "";
        const upperText = disallow ? disallowText : contentWidget?.generalFields.contentField;
        return `${upperText}\n${sitemapIndexText}`;
    },
);

export default robots;
