1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Stage = void 0;
|
4 | const session_1 = require("../session");
|
5 | const context_1 = require("./context");
|
6 | const composer_1 = require("../composer");
|
7 | class Stage extends composer_1.Composer {
|
8 | constructor(scenes = [], options) {
|
9 | super();
|
10 | this.options = { ...options };
|
11 | this.scenes = new Map();
|
12 | scenes.forEach((scene) => this.register(scene));
|
13 | }
|
14 | register(...scenes) {
|
15 | scenes.forEach((scene) => {
|
16 | if ((scene === null || scene === void 0 ? void 0 : scene.id) == null || typeof scene.middleware !== 'function') {
|
17 | throw new Error('telegraf: Unsupported scene');
|
18 | }
|
19 | this.scenes.set(scene.id, scene);
|
20 | });
|
21 | return this;
|
22 | }
|
23 | middleware() {
|
24 | const handler = composer_1.Composer.compose([
|
25 | (ctx, next) => {
|
26 | const scenes = this.scenes;
|
27 | const scene = new context_1.default(ctx, scenes, this.options);
|
28 | ctx.scene = scene;
|
29 | return next();
|
30 | },
|
31 | super.middleware(),
|
32 | composer_1.Composer.lazy((ctx) => { var _a; return (_a = ctx.scene.current) !== null && _a !== void 0 ? _a : composer_1.Composer.passThru(); }),
|
33 | ]);
|
34 | return composer_1.Composer.optional(session_1.isSessionContext, handler);
|
35 | }
|
36 | static enter(...args) {
|
37 | return (ctx) => ctx.scene.enter(...args);
|
38 | }
|
39 | static reenter(...args) {
|
40 | return (ctx) => ctx.scene.reenter(...args);
|
41 | }
|
42 | static leave(...args) {
|
43 | return (ctx) => ctx.scene.leave(...args);
|
44 | }
|
45 | }
|
46 | exports.Stage = Stage;
|