text: "use strict";
  var __importStar = (this && this.__importStar) || function (mod) {
      if (mod && mod.__esModule) return mod;
      var result = {};
      if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
      result["default"] = mod;
      return result;
  };
  var __importDefault = (this && this.__importDefault) || function (mod) {
      return (mod && mod.__esModule) ? mod : { "default": mod };
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  const express = __importStar(require("express"));
  const uuid_1 = require("uuid");
  const cors_1 = __importDefault(require("cors"));
  class Router {
      constructor(server) {
          const router = express.Router();
          const users = new Map();
          users[uuid_1.v4()] = { name: "Drácula", age: "7", phone: "21999999999" };
          users[uuid_1.v4()] = { name: "Loki", age: "5", phone: "21999999999" };
          router.get('/', (req, res) => {
              res.json({
                  message: `Nothing to see here, [url]/users instead.`
              });
          });
          //get all users
          router.get('/users', cors_1.default(), (req, res) => {
              res.json({
                  users
              });
          });
          //create new user
          router.post('/users', cors_1.default(), (req, res) => {
              try {
                  let user = {};
                  Object.assign(user, req.body);
                  const newID = uuid_1.v4();
                  users[newID] = user;
                  res.json({
                      id: newID
                  });
              }
              catch (e) {
                  res.status(400).send(JSON.stringify({ "error": "problem with posted data" }));
              }
          });
          //get user by id
          router.get('/users/:id', cors_1.default(), (req, res) => {
              if (!!users[req.params.id]) {
                  res.json({
                      user: users[req.params.id]
                  });
              }
              else {
                  res.status(404).send(JSON.stringify({ "error": "no such user" }));
              }
          });
          //update user
          router.put('/users/:id', cors_1.default(), (req, res) => {
              try {
                  if (!!users[req.params.id]) {
                      let user = {};
                      Object.assign(user, req.body);
                      users[req.params.id] = user;
                      res.json({
                          user: users[req.params.id]
                      });
                  }
                  else {
                      res.status(404).send(JSON.stringify({ "error": "no such user" }));
                  }
              }
              catch (e) {
                  res.status(400).send(JSON.stringify({ "error": "problem with posted data" }));
              }
          });
          //delete user
          router.delete('/users/:id', cors_1.default(), (req, res) => {
              if (!!users[req.params.id]) {
                  delete users[req.params.id];
                  res.json({
                      id: req.params.id
                  });
              }
              else {
                  res.status(404).send(JSON.stringify({ "error": "no such user" }));
              }
          });
          router.options('*', cors_1.default());
          server.use('/', router);
      }
  }
  exports.default = Router;
  