use std::fmt::Debug;
use std::vec::IntoIter;
use crate::core::voiceflow::dialog_blocks::enums::VoiceflowButtonsOption;
use crate::core::voiceflow::response_structures::{VoiceflowResponseBlock, VoiceflowResponseBlockProcessor};
use crate::core::voiceflow::VoiceflowBlock;
#[derive(Debug)]
pub struct VoiceflowMessage {
content: Vec<VoiceflowBlock>,
}
impl VoiceflowMessage {
pub fn add_block(&mut self, block: VoiceflowBlock) {
self.content.push(block);
}
pub fn trim_end_block(&mut self) -> bool {
if let Some(VoiceflowBlock::End) = &self.content.last() {
self.content.pop();
true
} else {
false
}
}
pub fn shift_block(&mut self, block: VoiceflowBlock) {
self.content.insert(0, block);
}
pub fn push(&mut self, block: VoiceflowBlock) {
self.content.push(block);
}
pub fn len(&self) -> usize {
self.content.len()
}
}
impl Default for VoiceflowMessage {
fn default() -> Self {
Self {
content: Vec::new(),
}
}
}
impl IntoIterator for VoiceflowMessage {
type Item = VoiceflowBlock;
type IntoIter = IntoIter<VoiceflowBlock>;
fn into_iter(self) -> Self::IntoIter {
self.content.into_iter()
}
}
pub(crate) struct VoiceflowMessageBuilder;
impl VoiceflowMessageBuilder {
pub fn new() -> Self {
Self
}
pub fn build_message(self, blocks: Vec<VoiceflowResponseBlock>) -> VoiceflowMessage {
let block_processor = VoiceflowResponseBlockProcessor::new();
let mut message = VoiceflowMessage {
content: Vec::with_capacity(blocks.len()),
};
let mut buttons_options = VoiceflowButtonsOption::Empty;
for block in blocks {
if let VoiceflowButtonsOption::Empty = buttons_options {
buttons_options = block_processor.process_block(&mut message, block);
} else {
block_processor.process_buttons_options(&mut message, &mut buttons_options, block);
}
}
block_processor.add_buttons_options_to_message(&mut message, buttons_options);
message
}
}