import lume from "lume/mod.ts";
import inline from "lume/plugins/inline.ts";
import postcss from "lume/plugins/postcss.ts";

const markdown = { options: { breaks: true } };

const site = lume({}, { markdown });

site.use(inline());

const languages = [
  {
    name: 'deutsch',
    slug: 'de',
    urlPrefix: '',
  },
  {
    name: 'english',
    slug: 'en',
    urlPrefix: 'en',
  }
]
site.data('languages', languages);

site.preprocess([ '.md' ], (pages) => {
  for (const page of pages) {
    const urlParts = (page.data.url || '').replace(/^\/|\/$/g, '').split('/');
    const langPrefix = urlParts[0];
    const currentLang = (() => {
      if (langPrefix.length === 2) {
        return languages.find(lang => lang.urlPrefix === langPrefix);
      }

      return languages.find(lang => lang.urlPrefix.length === 0);
    })() || languages[0];

    page.data.language = currentLang;
    page.data.otherLang = languages.find(l => l.slug !== currentLang.slug);
    page.data.baseURL = page.data.url.replace(new RegExp(`^\\/${currentLang.slug}`), '');
  }
});

site.use(postcss());

site.loadAssets([ ".js" ]);

site.copy("fonts");
site.copy("img");
site.copy("favicon.ico");

site.filter("title", (value = '') => value + (value ? " | " : "") + "MiOM");

site.ignore(
  ".direnv",
  "README.md",
  "CHANGELOG.md",
  "LICENSE.md",
  "docker-compose.yml",
);

export default site;
