import BaseScene, { SceneOptions } from '../base' import { Middleware, MiddlewareObj } from '../../middleware' import WizardContextWizard, { WizardSessionData } from './context' import Composer from '../../composer' import Context from '../../context' import SceneContextScene from '../context' export class WizardScene< C extends Context & { scene: SceneContextScene wizard: WizardContextWizard } > extends BaseScene implements MiddlewareObj { steps: Array> constructor(id: string, ...steps: Array>) constructor( id: string, options: SceneOptions, ...steps: Array> ) constructor( id: string, options: SceneOptions | Middleware, ...steps: Array> ) { let opts: SceneOptions | undefined let s: Array> if (typeof options === 'function' || 'middleware' in options) { opts = undefined s = [options, ...steps] } else { opts = options s = steps } super(id, opts) this.steps = s } middleware() { return Composer.compose([ (ctx, next) => { ctx.wizard = new WizardContextWizard(ctx, this.steps) return next() }, super.middleware(), (ctx, next) => { if (ctx.wizard.step === undefined) { ctx.wizard.selectStep(0) return ctx.scene.leave() } return Composer.unwrap(ctx.wizard.step)(ctx, next) }, ]) } enterMiddleware() { return Composer.compose([this.enterHandler, this.middleware()]) } }