use blake2::{Blake2b512, Digest};
use prost::Message;
use crate::chunk_dictionary::ChunkDictionary;
pub const ARCHIVE_MAGIC: &[u8; 6] = b"BITA1\0";
pub const PRE_HEADER_SIZE: usize = 6 + std::mem::size_of::<u64>();
pub fn build(
dictionary: &ChunkDictionary,
chunk_data_offset: Option<u64>,
) -> Result<Vec<u8>, std::io::Error> {
let mut header: Vec<u8> = vec![];
let mut hasher = Blake2b512::new();
let mut dictionary_buf: Vec<u8> = Vec::new();
dictionary.encode(&mut dictionary_buf)?;
header.extend(ARCHIVE_MAGIC);
header.extend((dictionary_buf.len() as u64).to_le_bytes());
header.extend(dictionary_buf);
let offset = match chunk_data_offset {
Some(o) => o,
None => header.len() as u64 + 8 + 64,
};
header.extend(offset.to_le_bytes());
hasher.update(&header);
header.extend(hasher.finalize());
Ok(header)
}