'use strict';

var jwt = require('jsonwebtoken');
var uuid = require('uuid');
var spark_api = require('@microsoft/spark.api');

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

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var jwt__default = /*#__PURE__*/_interopDefault(jwt);
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);

function create({ port, log, process }) {
  return async (req, res) => {
    const isClient = req.headers["x-teams-devtools"] === "true";
    const id = req.body.channelData?.streamId || uuid__namespace.v4();
    if (!isClient) {
      res.status(201).send({ id });
      return;
    }
    try {
      process(
        new spark_api.JsonWebToken(
          jwt__default.default.sign(
            {
              serviceurl: `http://localhost:${port}`
            },
            "secret"
          )
        ),
        {
          ...req.body,
          id: req.body.id || uuid__namespace.v4(),
          channelId: "msteams",
          from: {
            id: "devtools",
            name: "devtools",
            role: "user"
          },
          conversation: {
            id: req.params.conversationId,
            conversationType: "oneOnOne",
            isGroup: false,
            name: "default"
          }
        }
      );
      res.status(201).send({ id });
    } catch (err) {
      if (err instanceof Error) {
        log.error(err.message, err.stack);
        res.status(500).send({ error: err.message });
        return;
      }
      log.error(err);
      res.status(500).send({ error: err });
    }
  };
}

exports.create = create;
//# sourceMappingURL=create.js.map
//# sourceMappingURL=create.js.map