use core::iter::{ExactSizeIterator, FusedIterator};

use crate::{Arena, Key};

/// Iterator typed used when an [`Arena`] is converted to an [`IntoIterator`].
pub struct IntoIter<K, V> {
    pub(crate) arena: Arena<K, V>,
    pub(crate) slot: u32,
}

impl<K, V> Iterator for IntoIter<K, V> {
    type Item = (Key<K>, V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            // If there are no entries remaining in the arena, we should always
            // return None. Using this check instead of comparing with the
            // arena's size allows us to skip any trailing empty entries.
            if self.arena.is_empty() {
                return None;
            }

            // slot may overflow if the arena's underlying storage contains more
            // than 2^32 elements, but its internal length value was not
            // changed, as it overflowing would panic before reaching this code.
            let slot = self.slot;
            self.slot = self
                .slot
                .checked_add(1)
                .unwrap_or_else(|| panic!("Overflowed u32 trying to into_iter Arena"));

            // If this entry is occupied, this method will mark it as an empty.
            // Otherwise, we'll continue looping until we've removed all
            // occupied entries from the arena.
            if let Some((index, value)) = self.arena.remove_by_slot(slot) {
                return Some((index, value));
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.arena.len(), Some(self.arena.len()))
    }
}

impl<K, V> FusedIterator for IntoIter<K, V> {}
impl<K, V> ExactSizeIterator for IntoIter<K, V> {}

#[cfg(test)]
mod test {
    use crate::Arena;

    #[cfg(not(feature = "std"))]
    use alloc::vec::Vec;

    #[test]
    fn into_iter() {
        let mut arena = Arena::<(), u32>::with_capacity(2);
        let one = arena.insert(1);
        let two = arena.insert(2);

        let mut pairs = Vec::new();
        let mut into_iter = arena.into_iter();
        assert_eq!(into_iter.size_hint(), (2, Some(2)));

        let next = into_iter.next().unwrap();
        assert!(!pairs.contains(&next));
        pairs.push(next);
        assert_eq!(into_iter.size_hint(), (1, Some(1)));

        let next = into_iter.next().unwrap();
        assert!(!pairs.contains(&next));
        pairs.push(next);
        assert_eq!(into_iter.size_hint(), (0, Some(0)));

        assert_eq!(into_iter.next(), None);
        assert_eq!(into_iter.next(), None);
        assert_eq!(into_iter.size_hint(), (0, Some(0)));

        assert_eq!(pairs.len(), 2);
        assert!(pairs.contains(&(one, 1)));
        assert!(pairs.contains(&(two, 2)));
    }
}
