import './styles/main.scss'
import App from './App.vue'
import { ViteSSG } from 'vite-ssg'
import routes from './routes'
import strapiPlugin from './plugins/strapi'
import gsapPlugin from './plugins/gsap'
import intersectPlugin from './plugins/intersect'
import { strapi } from '@strapi/client'

export const createApp = ViteSSG(App, routes, ({ app }) => {
    // Plugins
    app.use(strapiPlugin)
    app.use(gsapPlugin)
    app.use(intersectPlugin)
})

// Prerender mosaic item routes
export async function includedRoutes(_, routes) {
    const client = strapi({
        baseURL: `${import.meta.env.VITE_STRAPI_URL}/api`,
        auth: import.meta.env.VITE_STRAPI_API_TOKEN,
    })

    const response = await client.single('global').find({ pLevel: '5' })
    const global = response.data

    const itemRoutes =
        global?.mosaic_items?.map((item) => `/${item.slug}`) ?? []

    return routes.flatMap((route) => {
        return route.name === 'mosaic-item' ? itemRoutes : route.path
    })
}
