'use strict';

var readline = require('readline');
var express = require('express');
var spark_common = require('@microsoft/spark.common');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var readline__default = /*#__PURE__*/_interopDefault(readline);
var express__default = /*#__PURE__*/_interopDefault(express);

class ConsolePlugin {
  constructor(options = {}) {
    this.options = options;
    this.log = new spark_common.ConsoleLogger("@spark/app/http");
    this.express = express__default.default();
    this.reader = readline__default.default.createInterface({
      input: this.options.stream || process.stdin,
      terminal: false
    });
    this.express.get("/auth/redirect", this.onAuthRedirect.bind(this));
    this.events = new spark_common.EventEmitter();
  }
  name = "console";
  log;
  reader;
  express;
  events;
  on(name, callback) {
    this.events.on(name, callback);
  }
  onInit(app) {
    this.log = app.log.child("console");
  }
  async onStart(port = 3e3) {
    this.express.listen(port + 1, () => {
      this.reader.on("line", async (text) => {
        const activity = {
          id: "1",
          type: "message",
          text,
          channelId: "msteams",
          conversation: {
            id: "1",
            conversationType: "oneOnOne",
            isGroup: false,
            name: ""
          },
          from: {
            id: "1",
            name: "user",
            role: "user"
          },
          recipient: {
            id: "2",
            name: "bot",
            role: "bot"
          }
        };
        const token = {
          appId: "1",
          from: "azure",
          fromId: "azure",
          serviceUrl: ""
        };
        this.events.emit("activity.received", {
          token,
          activity
        });
      });
    });
  }
  async onSend(activity, ref) {
    if (typeof activity === "string") {
      activity = {
        type: "message",
        text: activity
      };
    }
    this.events.emit("activity.before.sent", {
      activity,
      ref
    });
    if (activity.type === "message" && activity.text) {
      this.log.info(activity.text);
    }
    this.events.emit("activity.sent", {
      activity: { id: "1", ...activity },
      ref
    });
    return { id: "1", ...activity };
  }
  onAuthRedirect(req, res, _next) {
    console.log(req.url);
    console.log(req.query);
    res.status(200).send();
  }
}

exports.ConsolePlugin = ConsolePlugin;
//# sourceMappingURL=plugin.js.map
//# sourceMappingURL=plugin.js.map