import { getUrl } from "@insite/client-framework/ServerSideRendering";
import { Website } from "@insite/client-framework/Services/WebsiteService";

const prepareOrganizationStructuredPageData = (website: Website) => {
    const url: { protocol: string; host: string } = IS_SERVER_SIDE ? getUrl()! : window.location;
    const authority = `${url.protocol}//${url.host}`;

    const data = {
        "@context": "https://schema.org",
        "@type": "Organization",
        name: website.companyName,
        description: website.description,
        address: {
            "@type": "PostalAddress",
            streetAddress: [website.address1, website.address2].filter(o => o).join(", "),
            addressLocality: website.city,
            addressRegion: website.state,
            postalCode: website.postalCode,
            addressCountry: website.companyCountry,
        },
        telephone: website.companyPhone,
        url: website.companyUrl,
        logo:
            website.companyLogoImagePath &&
            (website.companyLogoImagePath.indexOf("http") === 0
                ? website.companyLogoImagePath
                : authority + website.companyLogoImagePath),
        sameAs: website.companySocialAccounts ? website.companySocialAccounts.split(",") : [],
    } as any;

    return JSON.stringify(data);
};

export default prepareOrganizationStructuredPageData;
