import jwt from 'jsonwebtoken';
import * as uuid from 'uuid';
import { JsonWebToken } from '@microsoft/spark.api';

function create({ port, log, process }) {
  return async (req, res) => {
    const isClient = req.headers["x-teams-devtools"] === "true";
    const id = req.body.channelData?.streamId || uuid.v4();
    if (!isClient) {
      res.status(201).send({ id });
      return;
    }
    try {
      process(
        new JsonWebToken(
          jwt.sign(
            {
              serviceurl: `http://localhost:${port}`
            },
            "secret"
          )
        ),
        {
          ...req.body,
          id: req.body.id || uuid.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 });
    }
  };
}

export { create };
//# sourceMappingURL=create.mjs.map
//# sourceMappingURL=create.mjs.map