<script lang="ts">
    import { getContext } from 'svelte'
    import { page } from '$app/stores'
    // Components
    import Metas from '$components/Metas.svelte'
    import Button from '$components/atoms/Button.svelte'
    import IconEarth from '$components/atoms/IconEarth.svelte'
    import BoxCTA from '$components/atoms/BoxCTA.svelte'
    import DiscoverText from '$components/atoms/DiscoverText.svelte'
    import PhotoCard from '$components/molecules/PhotoCard.svelte'
    import InteractiveGlobe from '$components/organisms/InteractiveGlobe.svelte'
    import Locations from '$components/organisms/Locations.svelte'
    import Shop from '$components/organisms/Shop.svelte'
    import Newsletter from '$components/organisms/Newsletter.svelte'

    export let photos: any

    const { settings, locations }: any = getContext('global')
    const { path } = $page
</script>

<Metas
    title="Houses Of"
    description=""
    image=""
/>

<main class="homepage">
    <section class="homepage__intro">
        <h1 style="display: none;">Houses Of The World</h1>
        <p class="homepage__title--houses title-huge">Houses</p>
        <p class="homepage__description text-medium">
            {settings.description}
        </p>

        <Button text="Explore locations" url="{path}#locations">
            <IconEarth animate={true} />
        </Button>
    </section>

    <section class="homepage__photos">
        <div class="homepage__collage">
            {#each photos as { image: { id, title } }}
                <PhotoCard id={id} alt={title} />
            {/each}
        </div>
    </section>

    <div class="homepage__ctas" id="ctas">
        <DiscoverText />

        <div class="cards">
            <BoxCTA
                url="{path}#locations"
                icon="/images/icons/globe.svg"
                label="Discover locations"
                alt="Globe"
            />
            <BoxCTA
                url="/photos"
                icon="/images/icons/photos.svg"
                label="Browse all photos"
                alt="Photos"
            />
            <BoxCTA
                url="/shop"
                icon="/images/icons/bag.svg"
                label="Shop our products"
                alt="Shopping bag"
            />
        </div>
    </div>

    <section class="homepage__locations" id="locations">
        <InteractiveGlobe />
        <p class="homepage__title--world title-huge">World</p>

        <Locations {locations} />
    </section>

    <div class="grid-modules">
        <div class="container grid">
            <div class="wrap">
                <Shop />
                <Newsletter />
            </div>
        </div>
    </div>
</main>


<script context="module" lang="ts">
    import { fetchAPI } from '$utils/api'

    export async function load ({ page, fetch, session, stuff }) {
        const res = await fetchAPI(`
            query {
                photo (limit: 11, sort: ["-date_created"]) {
                    image {
                        id
                        title
                    }
                }
            }
        `)

        const { data } = res

        return {
            props: {
                photos: data.photo,
            }
        }
    }
</script>
