use std::path::Path;
use crate::error::{CwError, Result};
pub fn resolve_prompt(
inline: Option<String>,
file: Option<&Path>,
stdin: bool,
stdin_reader: impl FnOnce() -> std::io::Result<String>,
) -> Result<Option<String>> {
let raw: Option<String> = if let Some(s) = inline {
Some(s)
} else if let Some(p) = file {
Some(std::fs::read_to_string(p).map_err(|e| {
CwError::Other(format!(
"failed to read --prompt-file '{}': {e}",
p.display()
))
})?)
} else if stdin {
Some(
stdin_reader()
.map_err(|e| CwError::Other(format!("failed to read --prompt-stdin: {e}")))?,
)
} else {
None
};
Ok(raw.and_then(|s| {
let trimmed = s.trim_end_matches(['\n', '\r']);
if trimmed.trim().is_empty() {
None
} else {
Some(trimmed.to_string())
}
}))
}