module.exports = {
    eleventyComputed: {
        permalink: function (data) {
            // get the file path stem
            let stem = data.page.filePathStem;

            // extract the year from the path
            const yearMatch = stem.match(/^\/es\/blog\/(\d{4})\//);
            if (yearMatch) {
                const year = yearMatch[1];

                // check for slug_override first
                if (data.slug_override) {
                    return `/es/blog/${year}/${this.slugify(data.slug_override)}/index.html`;
                }

                // handle blog posts with date prefix: /es/blog/[year]/[year-month-day]-slug
                const blogPostMatch = stem.match(/^\/es\/blog\/(\d{4})\/\d{4}-\d{2}-\d{2}-(.+)$/);
                if (blogPostMatch) {
                    const [, , slug] = blogPostMatch;
                    return `/es/blog/${year}/${this.slugify(slug)}/index.html`;
                }
            }

            // handle index pages or other non-blog-post pages
            if (stem.endsWith("/index")) {
                return `${stem}.html`; // ie /es/blog/index.html
            }

            // default for other pages under /es/blog/
            return `${stem}/index.html`; // ie /es/other-page/index.html
        }
    }
};