export const EmbeddedTurnPhase = {
    Idle: 'idle',
    Submitting: 'submitting',
    Running: 'running',
    Recovering: 'recovering',
    Refreshing: 'refreshing',
    Cancelling: 'cancelling',
    Disposed: 'disposed'
};
export const EmbeddedSubmitOverlapPolicy = {
    Reject: 'reject',
    Steer: 'steer'
};
export class EmbeddedTurnLifecycle {
    _generation = 0;
    _phase = EmbeddedTurnPhase.Idle;
    get generation() {
        return this._generation;
    }
    get phase() {
        return this._phase;
    }
    get isActive() {
        return this._phase !== EmbeddedTurnPhase.Idle && this._phase !== EmbeddedTurnPhase.Disposed;
    }
    beginSubmit() {
        return this._begin(EmbeddedTurnPhase.Submitting);
    }
    beginCancelling() {
        return this._begin(EmbeddedTurnPhase.Cancelling);
    }
    markRunning(token) {
        this._transition(token, EmbeddedTurnPhase.Running);
    }
    markRecovering() {
        if (this._phase === EmbeddedTurnPhase.Submitting ||
            this._phase === EmbeddedTurnPhase.Running ||
            this._phase === EmbeddedTurnPhase.Refreshing) {
            this._phase = EmbeddedTurnPhase.Recovering;
        }
    }
    markRefreshing(token) {
        this._transition(token, EmbeddedTurnPhase.Refreshing);
    }
    finish(token) {
        if (this._generation === token.generation) {
            this._phase = EmbeddedTurnPhase.Idle;
        }
    }
    dispose() {
        this._generation += 1;
        this._phase = EmbeddedTurnPhase.Disposed;
    }
    isTurnActive(agentIsStreaming) {
        return this.isActive || agentIsStreaming;
    }
    isCurrent(token) {
        return this._generation === token.generation;
    }
    snapshotShouldStayRunning(pendingContinuation) {
        return (pendingContinuation ||
            this._phase === EmbeddedTurnPhase.Submitting ||
            this._phase === EmbeddedTurnPhase.Running ||
            this._phase === EmbeddedTurnPhase.Recovering ||
            this._phase === EmbeddedTurnPhase.Refreshing);
    }
    _begin(phase) {
        this._generation += 1;
        this._phase = phase;
        return { generation: this._generation };
    }
    _transition(token, phase) {
        if (this._generation === token.generation) {
            this._phase = phase;
        }
    }
}
export function getSubmitOverlapPolicy(submitOptions) {
    if (submitOptions?.editFromMessageId)
        return EmbeddedSubmitOverlapPolicy.Reject;
    if (submitOptions?.contextReset)
        return EmbeddedSubmitOverlapPolicy.Reject;
    return EmbeddedSubmitOverlapPolicy.Steer;
}
//# sourceMappingURL=turn-lifecycle.js.map