use std::path::Path;
use zenith_session::adapter::OsFs;
use zenith_session::{StorePaths, bundle, resolve_data_dir, unbundle};
use crate::commands::workspace::scratch::open_store;
use crate::history::read_doc_id;
pub fn bundle_doc(doc_path: &Path, out_path: &Path) -> Result<String, String> {
let paths = open_store()?;
bundle_doc_in(&paths, doc_path, out_path)
}
pub fn bundle_doc_in(
paths: &StorePaths,
doc_path: &Path,
out_path: &Path,
) -> Result<String, String> {
let doc_id = read_doc_id(doc_path)?;
let fs = OsFs;
let bytes = bundle(&fs, paths, &doc_id).map_err(|e| e.message)?;
std::fs::write(out_path, &bytes)
.map_err(|e| format!("cannot write '{}': {e}", out_path.display()))?;
Ok(format!("bundled {} → {}", doc_id, out_path.display()))
}
pub fn unbundle_doc(bundle_path: &Path) -> Result<String, String> {
let data_dir = resolve_data_dir().map_err(|e| e.message)?;
let paths = StorePaths::new(data_dir);
unbundle_doc_in(&paths, bundle_path)
}
pub fn unbundle_doc_in(paths: &StorePaths, bundle_path: &Path) -> Result<String, String> {
let bytes = std::fs::read(bundle_path)
.map_err(|e| format!("cannot read '{}': {e}", bundle_path.display()))?;
let fs = OsFs;
let doc_id = unbundle(&fs, paths, &bytes).map_err(|e| e.message)?;
Ok(doc_id)
}