import readline from 'readline';
import express from 'express';
import { ConsoleLogger, EventEmitter } from '@microsoft/spark.common';

class ConsolePlugin {
  constructor(options = {}) {
    this.options = options;
    this.log = new ConsoleLogger("@spark/app/http");
    this.express = express();
    this.reader = readline.createInterface({
      input: this.options.stream || process.stdin,
      terminal: false
    });
    this.express.get("/auth/redirect", this.onAuthRedirect.bind(this));
    this.events = new 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();
  }
}

export { ConsolePlugin };
//# sourceMappingURL=plugin.mjs.map
//# sourceMappingURL=plugin.mjs.map