<script lang="ts">
    import {
        baseURL,
        socialIcons,
        websiteName,
        streetAddress,
        regionAddress,
        zipCode,
        countryAddress,
        localityAddress,
        email,
    } from "../../../../config"
    import { location } from "../../../store"
    export let createdAt: Date | string = new Date(),
        updatedAt: Date | string = new Date(),
        article = false,
        blog = false,
        FAQ = false,
        FAQDetails: {
            question: string
            answer: string
        }[] = [],
        metaDescription = "",
        title = "",
        product: BKDFProduct

    createdAt = new Date(createdAt).toLocaleDateString("de-DE", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
    })
    updatedAt = new Date(updatedAt).toLocaleDateString("de-DE", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
    })

    const defaultProps = {
        author: {
            "@id": `${baseURL}/#company`,
        },
        publisher: {
            "@id": `${baseURL}/#company`,
        },
        inLanguage: {
            "@type": "Language",
            name: "German",
        },
        datePublished: createdAt,
        dateModified: updatedAt,
        isPartOf: {
            "@id": `${baseURL}/#website`,
        },
    }

    const schemaOrgEntity = {
        "@type": ["Store", "Organization"],
        "@id": `${baseURL}/#company`,
        name: websiteName,
        url: baseURL,
        email,
        address: {
            "@type": "PostalAddress",
            streetAddress,
            addressLocality: localityAddress,
            addressRegion: regionAddress,
            postalCode: zipCode,
            addressCountry: countryAddress,
        },
        image: {
            "@type": "ImageObject",
            "@id": `${baseURL}/#image`,
            inLanguage: {
                "@type": "Language",
                name: "German",
            },
            contentUrl: `${baseURL}/api/_/assets/logo/logo-blue.svg`,
            width: 576,
            height: 158,
            caption: "BinKrassDuFass Logo",
        },
        logo: {
            "@type": "ImageObject",
            "@id": `${baseURL}/#logo`,
            url: baseURL,
            contentUrl: `${baseURL}/api/_/assets/logo/logo-blue.svg`,
            caption: "BinKrassDuFass Logo",
            inLanguage: {
                "@type": "Language",
                name: "German",
            },
        },
        priceRange: "$$",
        location: {
            "@id": location,
        },
        sameAs: [Object.values(socialIcons)],
    }

    const schemaOrgWebsite = {
        "@type": "WebSite",
        "@id": `${baseURL}/#website`,
        url: baseURL,
        name: "BKDF",
        description: "BinKrassDuFass Online Store",
        ...defaultProps,
    }

    const schemaOrgWebPage = {
        "@type": "WebPage",
        "@id": `${$location.url}#webpage`,
        url: $location.url,
        name: title,
        ...defaultProps,
        description: metaDescription,

        potentialAction: [
            {
                "@type": "ReadAction",
                target: [$location.url],
            },
        ],
    }

    let schemaOrgArticle = null
    if (article || blog) {
        schemaOrgArticle = {
            "@type": "Article",
            "@id": `${$location.url}#article`,
            headline: title,
            description: metaDescription,
            ...defaultProps,
            articleSection: ["blog"],
        }
    }

    let schemaOrgBlog = null
    if (blog) {
        schemaOrgBlog = {
            "@type": "BlogPosting",
            "@id": `${$location.url}#blog`,
            headline: title,
            description: metaDescription,
            ...defaultProps,
        }
    }

    let schemaOrgFAQ = null
    if (FAQ) {
        schemaOrgFAQ = {
            "@type": "FAQPage",
            "@id": `${$location.url}#faq`,
            ...defaultProps,
            mainEntity: FAQDetails.map((faq) => ({
                "@type": "Question",
                name: faq.question,
                acceptedAnswer: {
                    "@type": "Answer",
                    text: faq.answer,
                },
            })),
        }
    }

    let schemaOrgProduct = null
    if (product)
        schemaOrgProduct = {
            "@context": "http://schema.org/",
            "@type": "Product",
            "@id": `${$location.url}/#product`,
            ...defaultProps,
            image: {
                url: product.images[0]?.url,
                caption: product.images[0]?.altText,
            },
            name: product.title,
            description: product.description,
            sku: product.sku,
            brand: "BinKrassDuFass",
            category: product.categories[0]?.name,
            mainEntityOfPage: {
                "@id": `${$location.url}#webpage`,
            },
            releaseDate: createdAt,
            dateModified: updatedAt,
            url: $location.url,
            offers: {
                "@type": "Offer",
                availability: "http://schema.org/InStock",
                url: $location.url,
                price: product.priceRange.minVariantPrice.amount,
                priceCurrency: product.priceRange.minVariantPrice.currencyCode,
                itemCondition: "NewCondition",
                seller: {
                    "@type": "Organization",
                    "@id": `${baseURL}/#company`,
                },
            },
        }

    const schemaOrgArray = [
        schemaOrgEntity,
        schemaOrgWebsite,
        schemaOrgWebPage,
        schemaOrgProduct,
        schemaOrgFAQ,
        schemaOrgArticle,
        schemaOrgBlog,
    ].filter(Boolean)
    const schemaOrgObject = {
        "@context": "https://schema.org",
        "@graph": schemaOrgArray,
    }
    let jsonLdString = JSON.stringify(schemaOrgObject)
    let jsonLdScript = `
		<script type="application/ld+json">
			${jsonLdString}
		${"<"}/script>
	`
</script>

<svelte:head>
    {@html jsonLdScript}
</svelte:head>
