const apiSsrBaseURL =
    "http://localhost:" + context.config.server().api.port + "/api/v1/_/" + context.api().namespace + "/"

const now = { $date: new Date().toISOString() }
const publishedFilter = {
    active: true,
    $or: [
        { publication: { $exists: false } },
        { publication: null },
        {
            $and: [
                {
                    $or: [
                        { "publication.from": { $exists: false } },
                        { "publication.from": null },
                        { "publication.from": { $lte: now } },
                    ],
                },
                {
                    $or: [
                        { "publication.to": { $exists: false } },
                        { "publication.to": null },
                        { "publication.to": { $gte: now } },
                    ],
                },
            ],
        },
    ],
}

module.exports = {
    apiSsrBaseURL,
    publishedFilter,
    ssrValidatePath: function (/** @type {string} */ path) {
        // validate if path ssr rendering is ok, -1 = NOTFOUND, 0 = NO SSR, 1 = SSR
        // pe. use context.readCollection("product", {filter: {path: path}}) ... to validate dynamic urls

        // // / is fixed url
        // if (path == "/cart") return 1

        // // path starts with /products/ is product
        // if (path?.startsWith("/products/")) {
        //     const slug = path?.replace(/^\/products\//, "")
        //     const resp = context.db.find("product", {
        //         filter: {
        //             $and: [{ slug: slug }, publishedFilter],
        //         },

        //         selector: { _id: 1 },
        //     })
        //     if (resp && resp.length) {
        //         return 1
        //     }
        // }

        // // // all other sites are in db
        // //path = path?.replace(/^\//, "")
        // const resp = context.db.find("content", {
        //     filter: {
        //         $and: [{ $or: [{ path }, { "alternativePaths.path": path }] }, publishedFilter],
        //     },

        //     selector: { _id: 1 },
        // })
        // if (resp && resp.length) {
        //     return 1
        // }

        // not found
        return -1
    },
    ssrPublishCheckCollections: [
        /*"content", "product"*/
    ],
}
