pub mod puz;
pub mod ipuz;

pub fn detect_and_parse(data: &[u8]) -> Result<crate::puzzle::Puzzle, String> {
    // try puz first (check for ACROSS&DOWN magic at offset 2)
    if data.len() > 14 && &data[2..14] == b"ACROSS&DOWN\0" {
        return puz::from_puz(data);
    }
    // try ipuz (valid JSON with crossword markers)
    if let Ok(text) = std::str::from_utf8(data) {
        if let Ok(json) = serde_json::from_str::<serde_json::Value>(text) {
            if json.get("kind").is_some() || json.get("puzzle").is_some() || json.get("dimensions").is_some() {
                return ipuz::from_ipuz(&json);
            }
        }
    }
    Err("unrecognized file format".into())
}
