import path from "path";
import glob from "fast-glob";
import assert from "assert";
import { render_with_code } from "./docgen.js";
import fs from "fs";
import { Feed } from "feed";
// have to use the client config to avoid pino crashing rollup with json errors
import { base_host } from "../client/config.js";

const POSTS = {};
// this keeps POSTS alive, for some reason it disappears during processing
process.POSTS = POSTS;

export const load = (source) => {
  assert(source !== undefined, "source for markdown processing can't be undefined");
  // just a dumb cache to avoid reading the files over and over
  // cache only works if load isn't async!
  let cache = POSTS[source];

  if(cache === undefined) {
    cache = []; // this is fresh so make a new one

    let source_path = path.join(process.cwd(), "..", source);
    let posts_regex = path.join(source_path, "*.md");
    assert(!posts_regex.includes("//"), `You have a // in your path which fast-glob hates: ${posts_regex}`);

    const files = glob.sync(posts_regex.replace(/\\/g, "/"));

    for(let post of files.reverse()) {
      try {
        const {content, toc, metadata} = render_with_code(source_path, post);
        assert(content && toc && metadata, `Rendering ${source} failed.`);

        // so many Svelte builders (like 7ty) need slug that we should so it here
        cache.push({content, toc, metadata, slug: metadata.slug});
      } catch(error) {
        console.error(error, `Failure processing post: ${post}`);
      }
    }

    POSTS[source] = cache;
    return cache;
  } else {
    return cache;
  }
}

export const render_feed = (index_config, posts) => {
  const index = JSON.parse(fs.readFileSync(index_config));
  const feed = new Feed(index);
  index.updated = new Date();

  for(let post of posts) {
    post.url = `${base_host}/blog/${post.slug}/`;

    feed.addItem({
      title: post.metadata.title,
      id: post.metadata.url,
      link: post.metadata.url,
      date: new Date(post.metadata.date),
      description: post.metadata.summary,
      content: post.content,
      author: index.author
    });
  }

  fs.writeFileSync('../public/feed.rss', feed.rss2());
  fs.writeFileSync('../public/feed.json', feed.json1());
  fs.writeFileSync('../public/feed.atom', feed.atom1());
}

export const index = (source) => {
  assert(source !== undefined, "source must be defined");
  return load(source);
}

export default { load, index, render_feed };
