export var TurnState;
(function (TurnState) {
    TurnState[TurnState["IDLE"] = 0] = "IDLE";
    TurnState[TurnState["USER_SPEAKING"] = 1] = "USER_SPEAKING";
    TurnState[TurnState["PROCESSING"] = 2] = "PROCESSING";
    TurnState[TurnState["ASSISTANT_SPEAKING"] = 3] = "ASSISTANT_SPEAKING";
})(TurnState || (TurnState = {}));
export class TurnManager {
    state = TurnState.IDLE;
    constructor(bus) {
        bus.subscribe("vad_start", () => this.onVadStart());
        bus.subscribe("vad_stop", () => this.onVadStop());
    }
    onVadStart() {
        // Only update state if TTS is NOT playing. During ASSISTANT_SPEAKING, the
        // barge-in interrupt is triggered by Soniox non-final tokens — which arrive
        // after a network round-trip. If we let VAD immediately flip state to
        // USER_SPEAKING, the state check in handleSonioxMessage fails and the
        // interrupt never fires.
        if (this.state !== TurnState.ASSISTANT_SPEAKING) {
            this.state = TurnState.USER_SPEAKING;
        }
    }
    onVadStop() {
        if (this.state === TurnState.USER_SPEAKING) {
            this.state = TurnState.PROCESSING;
        }
    }
    assistantSpeaking() {
        this.state = TurnState.ASSISTANT_SPEAKING;
    }
    reset() {
        this.state = TurnState.IDLE;
    }
}
//# sourceMappingURL=turnManager.js.map