"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = create;
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const uuid = __importStar(require("uuid"));
const spark_api_1 = require("@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 spark_api_1.JsonWebToken(jsonwebtoken_1.default.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: 'personal',
                    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 });
        }
    };
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3JlYXRlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vc3JjL3JvdXRlcy92My9jb252ZXJzYXRpb25zL2FjdGl2aXRpZXMvY3JlYXRlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBWUEsd0JBcURDO0FBaEVELGdFQUErQjtBQUMvQiwyQ0FBNkI7QUFFN0Isb0RBQThEO0FBUTlELFNBQWdCLE1BQU0sQ0FBQyxFQUFFLElBQUksRUFBRSxHQUFHLEVBQUUsT0FBTyxFQUFnQjtJQUN6RCxPQUFPLEtBQUssRUFDVixHQUF5RCxFQUN6RCxHQUFxQixFQUNyQixFQUFFO1FBQ0YsTUFBTSxRQUFRLEdBQUcsR0FBRyxDQUFDLE9BQU8sQ0FBQyxrQkFBa0IsQ0FBQyxLQUFLLE1BQU0sQ0FBQztRQUM1RCxNQUFNLEVBQUUsR0FBRyxHQUFHLENBQUMsSUFBSSxDQUFDLFdBQVcsRUFBRSxRQUFRLElBQUksSUFBSSxDQUFDLEVBQUUsRUFBRSxDQUFDO1FBRXZELElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQztZQUNkLEdBQUcsQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUMsSUFBSSxDQUFDLEVBQUUsRUFBRSxFQUFFLENBQUMsQ0FBQztZQUM3QixPQUFPO1FBQ1QsQ0FBQztRQUVELElBQUksQ0FBQztZQUNILE9BQU8sQ0FDTC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