use tokio::sync::mpsc::{Receiver, Sender};

use crate::chopper::types::Row;

pub struct Splitter {
    rx: Receiver<Box<Row>>,
    txs: Vec<Sender<Box<Row>>>,
}

impl Splitter {
    pub fn new(rx: Receiver<Box<Row>>, txs: Vec<Sender<Box<Row>>>) -> Splitter {
        Splitter { rx, txs }
    }

    pub async fn run(&mut self) {
        //TODO send to all in parallel?
        while let Some(row) = self.rx.recv().await {
            let last_idx = self.txs.len() - 1;
            for i in 0..last_idx {
                self.txs[i].send(row.clone()).await.unwrap();
            }
            self.txs[last_idx].send(row).await.unwrap();
        }
    }
}
