use tokenizers::{Encoding, Tokenizer};

use crate::ChunkSizer;

/// Compute the number of tokens that exist within an entire [`Encoding`] object.
///
/// Take into account [`Encoding::get_overflowing`] for cases where the [`Tokenizer`] producing the
/// [`Encoding`] has truncation parameters set.
fn num_tokens_with_overflow(encoding: &Encoding, pad_id: Option<u32>) -> usize {
    let base = encoding
        .get_ids()
        .iter()
        // Skip padding tokens at beginning and end so they don't count towards the chunk size
        .skip_while(|&id| pad_id.is_some_and(|pad_id| id == &pad_id))
        .take_while(|&id| pad_id.is_none_or(|pad_id| id != &pad_id))
        .count();

    // If the [`Tokenizer`] has truncation, need to check overflow encodings to determine overall size.
    let overflow: usize = encoding
        .get_overflowing()
        .iter()
        .map(|enc| num_tokens_with_overflow(enc, pad_id))
        .sum();

    base + overflow
}

impl ChunkSizer for Tokenizer {
    /// Returns the number of tokens in a given text after tokenization.
    ///
    /// If the tokenizer has truncation enabled, the returned size may be truncated by the
    /// tokenizer. Disable truncation on the tokenizer before constructing a splitter if you need
    /// chunk sizes to reflect the full input text.
    ///
    /// # Panics
    ///
    /// Will panic if you don't have a byte-level tokenizer and the splitter
    /// encounters text it can't tokenize.
    fn size(&self, chunk: &str) -> usize {
        let encoding = self
            .encode_fast(chunk, false)
            .expect("Unable to tokenize the following string {chunk}");

        let pad_id = self.get_padding().map(|params| params.pad_id);
        num_tokens_with_overflow(&encoding, pad_id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn returns_size() {
        let tokenizer = Tokenizer::from_pretrained("bert-base-cased", None).unwrap();
        let size = tokenizer.size(" An apple a");
        assert_eq!(size, 3);
    }

    #[test]
    fn returns_size_handles_prefix() {
        let tokenizer =
            tokenizers::Tokenizer::from_file("./tests/tokenizers/huggingface.json").unwrap();

        let size = tokenizer.size("An apple a");
        assert_eq!(size, 3);
    }

    #[test]
    fn handles_padding() {
        let tokenizer = Tokenizer::from_pretrained("thenlper/gte-small", None).unwrap();
        let size = tokenizer.size("An apple a");
        assert_eq!(size, 3);
    }

    #[test]
    fn returns_truncated_size_when_tokenizer_has_truncation() {
        let tokenizer = Tokenizer::from_pretrained("sentence-transformers/all-MiniLM-L6-v2", None)
            .expect("Could not load tokenizer 'sentence-transformers/all-MiniLM-L6-v2'");

        assert_eq!(
            tokenizer.size("An apple a day keeps the doctor away.".repeat(100).as_str()),
            128
        );
    }

    #[test]
    fn returns_full_size_when_truncation_disabled() {
        let mut tokenizer =
            Tokenizer::from_pretrained("sentence-transformers/all-MiniLM-L6-v2", None)
                .expect("Could not load tokenizer 'sentence-transformers/all-MiniLM-L6-v2'");
        tokenizer
            .with_truncation(None)
            .expect("Could not disable tokenizer truncation");

        assert_eq!(
            tokenizer.size("An apple a day keeps the doctor away.".repeat(100).as_str()),
            900
        );
    }
}
