import { isSessionContext, SessionContext } from '../session' import SceneContextScene, { SceneContextSceneOptions, SceneSession, SceneSessionData, } from './context' import { BaseScene } from './base' import { Composer } from '../composer' import { Context } from '../context' export class Stage< C extends SessionContext> & { scene: SceneContextScene }, D extends SceneSessionData = SceneSessionData > extends Composer { options: Partial> scenes: Map> constructor( scenes: ReadonlyArray> = [], options?: Partial> ) { super() this.options = { ...options } this.scenes = new Map>() scenes.forEach((scene) => this.register(scene)) } register(...scenes: ReadonlyArray>) { scenes.forEach((scene) => { if (scene?.id == null || typeof scene.middleware !== 'function') { throw new Error('telegraf: Unsupported scene') } this.scenes.set(scene.id, scene) }) return this } middleware() { const handler = Composer.compose([ (ctx, next) => { const scenes: Map> = this.scenes const scene = new SceneContextScene(ctx, scenes, this.options) ctx.scene = scene return next() }, super.middleware(), Composer.lazy((ctx) => ctx.scene.current ?? Composer.passThru()), ]) return Composer.optional(isSessionContext, handler) } static enter }>( ...args: Parameters['enter']> ) { return (ctx: C) => ctx.scene.enter(...args) } static reenter }>( ...args: Parameters['reenter']> ) { return (ctx: C) => ctx.scene.reenter(...args) } static leave }>( ...args: Parameters['leave']> ) { return (ctx: C) => ctx.scene.leave(...args) } }