use std::{cell::RefCell, rc::{Rc, Weak}};

use crate::configotron::fsm::{FSMState, FSMStateAction, FSM};

pub fn run_fsm(input: &str, fsm: &FSM) -> FSMStateAction {
    let mut state: Weak<RefCell<FSMState>> = Rc::downgrade(fsm.start.as_ref().unwrap());

    for c in input.chars() {
        let n = c as u8;
        if c.is_ascii() && n <= 0x7F {
            let rc_state = state.upgrade().unwrap();
            let s = rc_state.borrow();
            state = s.transition.as_ref().unwrap().get_next(n);
        }
    };

    state.upgrade().unwrap().borrow().action.clone()
}
