use std::collections::BTreeMap;
use std::path::Path;
use std::sync::{Mutex, OnceLock, PoisonError};
use argonauts_core::paths::Paths;
use argonauts_core::prompt_files::PromptDirs;
use crate::engine::host::Host;
use super::{catalog, trust};
pub fn dirs(host: &Host) -> PromptDirs {
let home = Paths::from_env().ok();
let home = home.as_ref().map(|paths| paths.home());
let repo = host.repo();
match repo.run_dir.parent().and_then(Path::parent) {
Some(state_dir) => trust::dirs(&repo.root, state_dir, home),
None => PromptDirs::new(Some(&repo.root), home),
}
}
pub fn waiting(host: &Host) -> Vec<std::path::PathBuf> {
let repo = host.repo();
match repo.run_dir.parent().and_then(Path::parent) {
Some(state_dir) => trust::waiting(&repo.root, state_dir).all().collect(),
None => Vec::new(),
}
}
pub fn text(host: &Host, built_in: &'static str) -> &'static str {
catalog::of(built_in)
.and_then(|template| dirs(host).template(template.id))
.map_or(built_in, |yours| intern(yours.text))
}
pub(crate) fn intern(text: String) -> &'static str {
static TEXTS: OnceLock<Mutex<BTreeMap<String, &'static str>>> = OnceLock::new();
let mut texts = TEXTS
.get_or_init(Mutex::default)
.lock()
.unwrap_or_else(PoisonError::into_inner);
if let Some(kept) = texts.get(&text) {
return kept;
}
let kept: &'static str = Box::leak(text.clone().into_boxed_str());
texts.insert(text, kept);
kept
}