import type { SitemapUrlInput } from '#sitemap/types'
import { projectFilesTable as files } from '~/db/schema';
import useDatabase from '~/composables/useDatabase';

export default defineSitemapEventHandler(() => {
    const db = useDatabase();
    const pages = db.select({ path: files.path, lastMod: files.timestamp, navigable: files.navigable, private: files.private, type: files.type }).from(files).all();

    return pages.filter(e => e.type !== 'folder' && e.navigable && !e.private && e.path.split('/').map((_, i, a) => a.slice(0, i).join('/')).every(p => !pages.find(_p => _p.path === p)?.private)).map(e => ({
        loc: `/explore/${encodeURIComponent(e.path)}`,
        lastmod: e.lastMod,
    })) satisfies SitemapUrlInput[];
})