use std::fs;

use csv::ReaderBuilder;
use anyhow::{ Ok, Result};
use log::info;

pub fn read_dict(path: &str) -> Result<Vec<(String, u32)>> {
    info!("Parsing {}", path);

    let mut reader = ReaderBuilder::new()
        .delimiter(b'\t')
        .flexible(true)
        .has_headers(false)
        .from_reader(fs::File::open(path)?);

    let mut res = Vec::new();
    for line in reader.records() {
        let line = line.unwrap();
        if line.get(0).unwrap() == "@SQ" {
            res.push((
                line.get(1).unwrap().replace("SN:", ""),
                line.get(2).unwrap().replace("LN:", "").parse().unwrap(),
            ));
        }
    }
    Ok(res)
}

