use crate::AfterburnerError;
use anyhow::{Context, Result};
use serde_json::Value;
use std::fs;
use std::io::Read;
use std::path::PathBuf;
use super::args::Cli;
use super::build::build_afterburner;
pub fn thrust_from_stdin(cli: &Cli, path: &PathBuf) -> Result<()> {
let source = fs::read_to_string(path).with_context(|| format!("reading {path:?}"))?;
let abs = std::fs::canonicalize(path).unwrap_or_else(|_| path.clone());
let cwd = abs
.parent()
.map_or_else(|| "/".to_string(), |d| d.to_string_lossy().into_owned());
let source = format!(
"globalThis.__host_cwd = {cwd_json}; globalThis.__ab_argv = ['burn', {path_json}]; \
if (typeof globalThis.__plenum_refresh_entry_require === 'function') globalThis.__plenum_refresh_entry_require();
{source}",
cwd_json = serde_json::to_string(&cwd)?,
path_json = serde_json::to_string(&abs.to_string_lossy())?,
);
let mut stdin_bytes = Vec::new();
std::io::stdin()
.read_to_end(&mut stdin_bytes)
.context("reading stdin")?;
let input: Value = if stdin_bytes.is_empty() {
Value::Null
} else {
serde_json::from_slice(&stdin_bytes).context("parse stdin as JSON")?
};
let ab = build_afterburner(cli)?;
let id = ab.register(&source).context("compile")?;
let out = ab
.run(&id, &input)
.map_err(|e: AfterburnerError| anyhow::anyhow!("{e}"))?;
println!("{}", serde_json::to_string(&out).unwrap_or_default());
Ok(())
}