use crate::EpochProcessingError;
use safe_arith::SafeArith;
use std::sync::Arc;
use types::core::{ChainSpec, EthSpec};
use types::state::BeaconState;

pub fn process_sync_committee_updates<E: EthSpec>(
    state: &mut BeaconState<E>,
    spec: &ChainSpec,
) -> Result<(), EpochProcessingError> {
    let next_epoch = state.next_epoch()?;
    if next_epoch.safe_rem(spec.epochs_per_sync_committee_period)? == 0 {
        *state.current_sync_committee_mut()? = state.next_sync_committee()?.clone();

        *state.next_sync_committee_mut()? = Arc::new(state.get_next_sync_committee(spec)?);
    }
    Ok(())
}
