// The incremental cache: one JSON manifest under `.fus-cache`.

///|
/// Read the manifest from `cache_dir`, returning `{}` when absent or invalid.
pub async fn read_manifest(cache_dir : String) -> Json {
  let path = @core.join_path(cache_dir, "manifest.json")
  let text = @core.read_text_or(path, "")
  if text == "" {
    return Json::object(Map([]))
  }
  @json.parse(text) catch {
    _ => Json::object(Map([]))
  }
}

///|
/// Write the manifest, creating the directory when needed.
///
/// `version` guards future format changes and `global` hashes every shared input
/// (config, layouts, includes, data) so a change invalidates all pages at once.
pub async fn write_manifest(
  cache_dir : String,
  global_hash : String,
  files : Map[String, Json],
) -> Unit {
  let path = @core.join_path(cache_dir, "manifest.json")
  let manifest = @core.jobj([
    ("version", @core.jnum(1.0)),
    ("global", @core.jstr(global_hash)),
    ("files", Json::object(files)),
  ])
  @core.write_if_changed(path, manifest.stringify(indent=2)) |> ignore
}