text: "use strict";
  /**
   * Copyright (c) Microsoft Corporation. All rights reserved.
   * Licensed under the MIT License.
   */
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  const activityHandlerBase_1 = require("./activityHandlerBase");
  const signInConstants_1 = require("./signInConstants");
  const statusCodes_1 = require("./statusCodes");
  /**
   * Event-emitting activity handler for bots. Extends [ActivityHandlerBase](xref:botbuilder-core.ActivityHandlerBase).
   *
   * @remarks
   * This provides an extensible class for handling incoming activities in an event-driven way.
   * You can register an arbitrary set of handlers for each event type.
   *
   * To register a handler for an event, use the corresponding _on event_ method. If multiple handlers are
   * registered for an event, they are run in the order in which they were registered.
   *
   * This object emits a series of _events_ as it processes an incoming activity.
   * A handler can stop the propagation of the event by not calling the continuation function.
   *
   * | Event type | Description |
   * | :--- | :--- |
   * | Turn | Emitted first for every activity. |
   * | Type-specific | Emitted for the specific activity type, before emitting an event for any sub-type. |
   * | Sub-type | Emitted for certain specialized events, based on activity content. |
   * | Dialog | Emitted as the final activity processing event. |
   *
   * For example:
   *
   * ```typescript
   * const bot = new ActivityHandler();
   *
   * server.post('/api/messages', (req, res) => {
   *     adapter.processActivity(req, res, async (context) => {
   *         // Route to bot's activity logic.
   *         await bot.run(context);
   *     });
   * });
   *
   * bot.onTurn(async (context, next) => {
   *         // Handle a "turn" event.
   *         await context.sendActivity(`${ context.activity.type } activity received.`);
   *         // Continue with further processing.
   *         await next();
   *     })
   *     .onMessage(async (context, next) => {
   *         // Handle a message activity.
   *         await context.sendActivity(`Echo: ${ context.activity.text }`);
   *         // Continue with further processing.
   *         await next();
   *     });
   * ```
   *
   * **See also**
   * - The [Bot Framework Activity schema](https://aka.ms/botSpecs-activitySchema)
   */
  class ActivityHandler extends activityHandlerBase_1.ActivityHandlerBase {
      constructor() {
          super(...arguments);
          this.handlers = {};
      }
      /**
       * Registers an activity event handler for the _turn_ event, emitted for every incoming activity, regardless of type.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       */
      onTurn(handler) {
          return this.on('Turn', handler);
      }
      /**
       * Registers an activity event handler for the _message_ event, emitted for every incoming message activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * Message activities represent content intended to be shown within a conversational interface
       * and can contain text, speech, interactive cards, and binary or unknown attachments.
       * Not all message activities contain text, the activity's [text](xref:botframework-schema.Activity.text)
       * property can be `null` or `undefined`.
       */
      onMessage(handler) {
          return this.on('Message', handler);
      }
      /**
       * Registers an activity event handler for the _conversation update_ event, emitted for every incoming
       * conversation update activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * Conversation update activities describe a changes to a conversation's metadata, such as title, participants,
       * or other channel-specific information.
       *
       * To handle when members are added to or removed from the conversation, use the
       * [onMembersAdded](xref:botbuilder-core.ActivityHandler.onMembersAdded) and
       * [onMembersRemoved](xref:botbuilder-core.ActivityHandler.onMembersRemoved) sub-type event handlers.
       */
      onConversationUpdate(handler) {
          return this.on('ConversationUpdate', handler);
      }
      /**
       * Registers an activity event handler for the _members added_ event, emitted for any incoming
       * conversation update activity that includes members added to the conversation.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * The activity's [membersAdded](xref:botframework-schema.Activity.membersAdded) property
       * contains the members added to the conversation, which can include the bot.
       *
       * To handle conversation update events in general, use the
       * [onConversationUpdate](xref:botbuilder-core.ActivityHandler.onConversationUpdate) type-specific event handler.
       */
      onMembersAdded(handler) {
          return this.on('MembersAdded', handler);
      }
      /**
       * Registers an activity event handler for the _members removed_ event, emitted for any incoming
       * conversation update activity that includes members removed from the conversation.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * The activity's [membersRemoved](xref:botframework-schema.Activity.membersRemoved) property
       * contains the members removed from the conversation, which can include the bot.
       *
       * To handle conversation update events in general, use the
       * [onConversationUpdate](xref:botbuilder-core.ActivityHandler.onConversationUpdate) type-specific event handler.
       */
      onMembersRemoved(handler) {
          return this.on('MembersRemoved', handler);
      }
      /**
       * Registers an activity event handler for the _message reaction_ event, emitted for every incoming
       * message reaction activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * Message reaction activities represent a social interaction on an existing message activity
       * within a conversation. The original activity is referred to by the message reaction activity's
       * [replyToId](xref:botframework-schema.Activity.replyToId) property. The
       * [from](xref:botframework-schema.Activity.from) property represents the source of the reaction,
       * such as the user that reacted to the message.
       *
       * To handle when reactions are added to or removed from messages in the conversation, use the
       * [onReactionsAdded](xref:botbuilder-core.ActivityHandler.onReactionsAdded) and
       * [onReactionsRemoved](xref:botbuilder-core.ActivityHandler.onReactionsRemoved) sub-type event handlers.
       */
      onMessageReaction(handler) {
          return this.on('MessageReaction', handler);
      }
      /**
       * Registers an activity event handler for the _reactions added_ event, emitted for any incoming
       * message reaction activity that describes reactions added to a message.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * The activity's [reactionsAdded](xref:botframework-schema.Activity.reactionsAdded) property
       * includes one or more reactions that were added.
       *
       * To handle message reaction events in general, use the
       * [onMessageReaction](xref:botbuilder-core.ActivityHandler.onMessageReaction) type-specific event handler.
       */
      onReactionsAdded(handler) {
          return this.on('ReactionsAdded', handler);
      }
      /**
       * Registers an activity event handler for the _reactions removed_ event, emitted for any incoming
       * message reaction activity that describes reactions removed from a message.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * The activity's [reactionsRemoved](xref:botframework-schema.Activity.reactionsRemoved) property
       * includes one or more reactions that were removed.
       *
       * To handle message reaction events in general, use the
       * [onMessageReaction](xref:botbuilder-core.ActivityHandler.onMessageReaction) type-specific event handler.
       */
      onReactionsRemoved(handler) {
          return this.on('ReactionsRemoved', handler);
      }
      /**
       * Registers an activity event handler for the _event_ event, emitted for every incoming event activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * Event activities communicate programmatic information from a client or channel to a bot.
       * The meaning of an event activity is defined by the activity's
       * [name](xref:botframework-schema.Activity.name) property, which is meaningful within the scope
       * of a channel. Event activities are designed to carry both interactive information (such as
       * button clicks) and non-interactive information (such as a notification of a client
       * automatically updating an embedded speech model).
       *
       * To handle a `tokens/response` event event, use the
       * [onTokenResponseEvent](xref:botbuilder-core.ActivityHandler.onTokenResponseEvent) sub-type
       * event handler. To handle other named events, add logic to this handler.
       */
      onEvent(handler) {
          return this.on('Event', handler);
      }
      /**
       * Registers an activity event handler for the _end of conversation_ activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * This activity is typically send from a Skill to a Skill caller indicating the end of that particular child conversation.
       *
       * To handle an End of Conversation, use the
       * [onEndOfConversation](xref:botbuilder-core.ActivityHandler.onEndOfConversation) type-specific event handler.
       */
      onEndOfConversation(handler) {
          return this.on('EndOfConversation', handler);
      }
      /**
       * Registers an activity event handler for the _typing_ activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * To handle a Typing event, use the
       * [onTyping](xref:botbuilder-core.ActivityHandler.onTyping) type-specific event handler.
       */
      onTyping(handler) {
          return this.on('Typing', handler);
      }
      /**
       * Registers an activity event handler for the _installationupdate_ activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * To handle a InstallationUpdate event, use the
       * [onInstallationUpdate](xref:botbuilder-core.ActivityHandler.onInstallationUpdate) type-specific event handler.
       */
      onInstallationUpdate(handler) {
          return this.on('InstallationUpdate', handler);
      }
      /**
       * Registers an activity event handler for the _installationupdate add_ activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * To handle a InstallationUpdateAdd event, use the
       * [onInstallationUpdateAdd](xref:botbuilder-core.ActivityHandler.onInstallationUpdateAdd) type-specific event handler.
       */
      onInstallationUpdateAdd(handler) {
          return this.on('InstallationUpdateAdd', handler);
      }
      /**
       * Registers an activity event handler for the _installationupdate remove_ activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * To handle a InstallationUpdateRemove event, use the
       * [onInstallationUpdateRemove](xref:botbuilder-core.ActivityHandler.onInstallationUpdateRemove) type-specific event handler.
       */
      onInstallationUpdateRemove(handler) {
          return this.on('InstallationUpdateRemove', handler);
      }
      /**
       * Registers an activity event handler for the _tokens-response_ event, emitted for any incoming
       * `tokens/response` event activity. These are generated as part of the OAuth authentication flow.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * The activity's [value](xref:botframework-schema.Activity.value) property contains the user token.
       *
       * If your bot handles authentication using an [OAuthPrompt](xref:botbuilder-dialogs.OAuthPrompt)
       * within a dialog, then the dialog will need to receive this activity to complete the authentication flow.
       *
       * To handle other named events and event events in general, use the
       * [onEvent](xref:botbuilder-core.ActivityHandler.onEvent) type-specific event handler.
       */
      onTokenResponseEvent(handler) {
          return this.on('TokenResponseEvent', handler);
      }
      /**
       * Registers an activity event handler for the _unrecognized activity type_ event, emitted for an
       * incoming activity with a type for which the [ActivityHandler](xref:botbuilder-core.ActivityHandler)
       * doesn't provide an event handler.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       * The `ActivityHandler` does not define events for all activity types defined in the
       * [Bot Framework Activity schema](http://aka.ms/botSpecs-activitySchema). In addition,
       * channels and custom adapters can create [Activities](xref:botframework-schema.Activity) with
       * types not in the schema. When the activity handler receives such an event, it emits an unrecognized activity type event.
       *
       * The activity's [type](xref:botframework-schema.Activity.type) property contains the activity type.
       */
      onUnrecognizedActivityType(handler) {
          return this.on('UnrecognizedActivityType', handler);
      }
      /**
       * Registers an activity event handler for the _dialog_ event, emitted as the last event for an incoming activity.
       *
       * @param handler The event handler.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       *
       */
      onDialog(handler) {
          return this.on('Dialog', handler);
      }
      /**
       * Called to initiate the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Typically, you would provide this method as the function handler that the adapter calls
       * to perform the bot's logic after the received activity has been pre-processed by the adapter
       * and routed through any middleware.
       *
       * For example:
       * ```javascript
       *  server.post('/api/messages', (req, res) => {
       *      adapter.processActivity(req, res, async (context) => {
       *          // Route to bot's activity logic.
       *          await bot.run(context);
       *      });
       * });
       * ```
       *
       * **See also**
       * - [BotFrameworkAdapter.processActivity](xref:botbuilder.BotFrameworkAdapter.processActivity)
       */
      run(context) {
          const _super = Object.create(null, {
              run: { get: () => super.run }
          });
          return __awaiter(this, void 0, void 0, function* () {
              yield _super.run.call(this, context);
          });
      }
      /**
       * Called at the start of the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to use custom logic for emitting events.
       *
       * The default logic is to call any handlers registered via [onTurn](xref:botbuilder-core.ActivityHandler.onTurn),
       * and then continue by calling [ActivityHandlerBase.onTurnActivity](xref:botbuilder-core.ActivityHandlerBase.onTurnActivity).
       */
      onTurnActivity(context) {
          const _super = Object.create(null, {
              onTurnActivity: { get: () => super.onTurnActivity }
          });
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'Turn', () => __awaiter(this, void 0, void 0, function* () {
                  yield _super.onTurnActivity.call(this, context);
              }));
          });
      }
      /**
       * Runs all registered _message_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onMessage](xref:botbuilder-core.ActivityHandler.onMessage),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onMessageActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'Message', this.defaultNextEvent(context));
          });
      }
      /*
       * Provides default behavior for invoke activities.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       * The default logic is to check for a signIn invoke and handle that
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
      */
      onInvokeActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  switch (context.activity.name) {
                      case signInConstants_1.verifyStateOperationName:
                      case signInConstants_1.tokenExchangeOperationName: {
                          yield this.onSignInInvoke(context);
                          return { status: statusCodes_1.StatusCodes.OK };
                      }
                      case "healthCheck":
                          return yield ActivityHandler.createInvokeResponse(yield this.onHealthCheck(context));
                      default:
                          throw new Error('NotImplemented');
                  }
              }
              catch (err) {
                  if (err.message === 'NotImplemented') {
                      return { status: statusCodes_1.StatusCodes.NOT_IMPLEMENTED };
                  }
                  throw err;
              }
              finally {
                  this.defaultNextEvent(context)();
              }
          });
      }
      /*
       * Handle _signin invoke activity type_.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
      */
      onSignInInvoke(context) {
          return __awaiter(this, void 0, void 0, function* () {
              throw new Error('NotImplemented');
          });
      }
      /*
       * Handle _healthCheck invoke activity type_.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to customize or extended the built in healthCheck behavior.
      */
      onHealthCheck(context) {
          return __awaiter(this, void 0, void 0, function* () {
              const adapter = context.adapter;
              if (typeof adapter.healthCheck === 'function') {
                  return yield adapter.healthCheck(context);
              }
              else {
                  return { healthResults: { success: true, messages: ['Health check succeeded.'] } };
              }
          });
      }
      /**
       * Runs all registered _endOfConversation_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onEndOfConversationActivity](xref:botbuilder-core.ActivityHandler.onMessage),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onEndOfConversationActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'EndOfConversation', this.defaultNextEvent(context));
          });
      }
      /**
       * Runs all registered _typing_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onTypingActivity](xref:botbuilder-core.ActivityHandler.onTypingActivity),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onTypingActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'Typing', this.defaultNextEvent(context));
          });
      }
      /**
       * Runs all registered _instllationupdate_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onInstallationUpdateActivity](xref:botbuilder-core.ActivityHandler.onInstallationUpdateActivity),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onInstallationUpdateActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'InstallationUpdate', () => __awaiter(this, void 0, void 0, function* () {
                  yield this.dispatchInstallationUpdateActivity(context);
              }));
          });
      }
      /**
       * Runs the _installation update_ sub-type handlers, as appropriate, and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels or to add
       * custom conversation update sub-type events.
       *
       * The default logic is:
       * - If any members were added, call handlers registered via [onMembersAdded](xref:botbuilder-core.ActivityHandler.onMembersAdded).
       * - If any members were removed, call handlers registered via [onMembersRemoved](xref:botbuilder-core.ActivityHandler.onMembersRemoved).
       * - Continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      dispatchInstallationUpdateActivity(context) {
          const _super = Object.create(null, {
              onInstallationUpdateActivity: { get: () => super.onInstallationUpdateActivity }
          });
          return __awaiter(this, void 0, void 0, function* () {
              if (context.activity.action == 'add' || context.activity.action == 'remove') {
                  yield _super.onInstallationUpdateActivity.call(this, context);
              }
              else {
                  yield this.defaultNextEvent(context)();
              }
          });
      }
      /**
       * Runs all registered _installation update add_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onInstallationUpdateAdd](xref:botbuilder-core.ActivityHandler.onInstallationUpdateAdd),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onInstallationUpdateAddActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'InstallationUpdateAdd', this.defaultNextEvent(context));
          });
      }
      /**
       * Runs all registered _installation update remove_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onInstallationUpdateRemove](xref:botbuilder-core.ActivityHandler.onInstallationUpdateRemove),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onInstallationUpdateRemoveActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'InstallationUpdateRemove', this.defaultNextEvent(context));
          });
      }
      /**
       * Runs all registered _unrecognized activity type_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onUnrecognizedActivityType](xref:botbuilder-core.ActivityHandler.onUnrecognizedActivityType),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onUnrecognizedActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'UnrecognizedActivityType', this.defaultNextEvent(context));
          });
      }
      /**
       * Runs all registered _conversation update_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onConversationUpdate](xref:botbuilder-core.ActivityHandler.onConversationUpdate),
       * and then continue by calling
       * [dispatchConversationUpdateActivity](xref:botbuilder-core.ActivityHandler.dispatchConversationUpdateActivity).
       */
      onConversationUpdateActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'ConversationUpdate', () => __awaiter(this, void 0, void 0, function* () {
                  yield this.dispatchConversationUpdateActivity(context);
              }));
          });
      }
      /**
       * Runs the _conversation update_ sub-type handlers, as appropriate, and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels or to add
       * custom conversation update sub-type events.
       *
       * The default logic is:
       * - If any members were added, call handlers registered via [onMembersAdded](xref:botbuilder-core.ActivityHandler.onMembersAdded).
       * - If any members were removed, call handlers registered via [onMembersRemoved](xref:botbuilder-core.ActivityHandler.onMembersRemoved).
       * - Continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      dispatchConversationUpdateActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              if (context.activity.membersAdded && context.activity.membersAdded.length > 0) {
                  yield this.handle(context, 'MembersAdded', this.defaultNextEvent(context));
              }
              else if (context.activity.membersRemoved && context.activity.membersRemoved.length > 0) {
                  yield this.handle(context, 'MembersRemoved', this.defaultNextEvent(context));
              }
              else {
                  yield this.defaultNextEvent(context)();
              }
          });
      }
      /**
       * Runs all registered _message reaction_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onMessageReaction](xref:botbuilder-core.ActivityHandler.onMessageReaction),
       * and then continue by calling
       * [dispatchMessageReactionActivity](xref:botbuilder-core.ActivityHandler.dispatchMessageReactionActivity).
       */
      onMessageReactionActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'MessageReaction', () => __awaiter(this, void 0, void 0, function* () {
                  yield this.dispatchMessageReactionActivity(context);
              }));
          });
      }
      /**
       * Runs all registered _reactions added_ handlers and then continues the event emission process.
       *
       * @param reactionsAdded The list of reactions added.
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onReactionsAdded](xref:botbuilder-core.ActivityHandler.onReactionsAdded),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onReactionsAddedActivity(reactionsAdded, context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'ReactionsAdded', this.defaultNextEvent(context));
          });
      }
      /**
       * Runs all registered _reactions removed_ handlers and then continues the event emission process.
       *
       * @param reactionsRemoved The list of reactions removed.
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onReactionsRemoved](xref:botbuilder-core.ActivityHandler.onReactionsRemoved),
       * and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      onReactionsRemovedActivity(reactionsRemoved, context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'ReactionsRemoved', this.defaultNextEvent(context));
          });
      }
      /**
       * Runs the _message reaction_ sub-type handlers, as appropriate, and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels or to add
       * custom message reaction sub-type events.
       *
       * The default logic is:
       * - If reactions were added, call handlers registered via [onReactionsAdded](xref:botbuilder-core.ActivityHandler.onReactionsAdded).
       * - If reactions were removed, call handlers registered via [onMembersRemoved](xref:botbuilder-core.ActivityHandler.onMembersRemoved).
       * - Continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      dispatchMessageReactionActivity(context) {
          const _super = Object.create(null, {
              onMessageReactionActivity: { get: () => super.onMessageReactionActivity }
          });
          return __awaiter(this, void 0, void 0, function* () {
              if (context.activity.reactionsAdded || context.activity.reactionsRemoved) {
                  yield _super.onMessageReactionActivity.call(this, context);
              }
              else {
                  yield this.defaultNextEvent(context)();
              }
          });
      }
      /**
       * Runs all registered event_ handlers and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels.
       *
       * The default logic is to call any handlers registered via
       * [onEvent](xref:botbuilder-core.ActivityHandler.onEvent),
       * and then continue by calling
       * [dispatchEventActivity](xref:botbuilder-core.ActivityHandler.dispatchEventActivity).
       */
      onEventActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'Event', () => __awaiter(this, void 0, void 0, function* () {
                  yield this.dispatchEventActivity(context);
              }));
          });
      }
      /**
       * Runs the _event_ sub-type handlers, as appropriate, and then continues the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to support channel-specific behavior across multiple channels or to add custom event sub-type events.
       * For certain channels, such as  Web Chat and custom Direct Line clients, developers can emit custom event activities from the client.
       *
       * The default logic is:
       * - If the activity is a 'tokens/response' event, call handlers registered via
       *   [onTokenResponseEvent](xref:botbuilder-core.ActivityHandler.onTokenResponseEvent).
       * - Continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
       */
      dispatchEventActivity(context) {
          return __awaiter(this, void 0, void 0, function* () {
              if (context.activity.name === signInConstants_1.tokenResponseEventName) {
                  yield this.handle(context, 'TokenResponseEvent', this.defaultNextEvent(context));
              }
              else {
                  yield this.defaultNextEvent(context)();
              }
          });
      }
      /**
       * Called at the end of the event emission process.
       *
       * @param context The context object for the current turn.
       *
       * @remarks
       * Overwrite this method to use custom logic for emitting events.
       *
       * The default logic is to call any handlers registered via [onDialog](xref:botbuilder-core.ActivityHandler.onDialog),
       * and then complete the event emission process.
       */
      defaultNextEvent(context) {
          const runDialogs = () => __awaiter(this, void 0, void 0, function* () {
              yield this.handle(context, 'Dialog', () => __awaiter(this, void 0, void 0, function* () {
                  // noop
              }));
          });
          return runDialogs;
      }
      /**
       * Registers a bot event handler to receive a specific event.
       *
       * @param type The identifier for the event type.
       * @param handler The event handler to register.
       *
       * @remarks
       * Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
       */
      on(type, handler) {
          if (!this.handlers[type]) {
              this.handlers[type] = [handler];
          }
          else {
              this.handlers[type].push(handler);
          }
          return this;
      }
      /**
       * Emits an event and executes any registered handlers.
       *
       * @param context The context object for the current turn.
       * @param type The identifier for the event type.
       * @param onNext The continuation function to call after all registered handlers for this event complete.
       *
       * @remarks
       * Runs any registered handlers for this event type and then calls the continuation function.
       *
       * This optionally produces a return value, to support _invoke_ activities. If multiple handlers
       * produce a return value, the first one produced is returned.
       */
      handle(context, type, onNext) {
          return __awaiter(this, void 0, void 0, function* () {
              let returnValue = null;
              function runHandler(index) {
                  return __awaiter(this, void 0, void 0, function* () {
                      if (index < handlers.length) {
                          const val = yield handlers[index](context, () => runHandler(index + 1));
                          // if a value is returned, and we have not yet set the return value,
                          // capture it.  This is used to allow InvokeResponses to be returned.
                          if (typeof (val) !== 'undefined' && returnValue === null) {
                              returnValue = val;
                          }
                      }
                      else {
                          const val = yield onNext();
                          if (typeof (val) !== 'undefined') {
                              returnValue = val;
                          }
                      }
                  });
              }
              const handlers = this.handlers[type] || [];
              yield runHandler(0);
              return returnValue;
          });
      }
      static createInvokeResponse(body) {
          return { status: 200, body };
      }
  }
  exports.ActivityHandler = ActivityHandler;
  //# sourceMappingURL=activityHandler.js.map