"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.websocketMockSetup = websocketMockSetup;

var _ws = _interopRequireDefault(require("ws"));

var _logger = require("../../../logger");

var _constants = require("../../constants");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function websocketMockSetup(app, httpsServer) {
  app.use('/wms/*', (req, res) => {
    res.sendFile((0, _constants.getTemplatePath)('wms', 'index.html'));
  });
  const wss = new _ws.default.Server({
    server: httpsServer
  });
  let wsPool = [];
  wss.on('connection', ws => {
    wsPool.push(ws);
    ws.on('close', () => {
      wsPool = wsPool.filter(ws1 => ws1 !== ws);
    });
    ws.on('message', message => {
      (0, _logger.messageLogger)('received: %s', message);
    });
    ws.send('something');
  });
  app.post('/wmsmockapi', (req, res) => {
    wsPool.forEach(ws => {
      const {
        body
      } = req;

      try {
        ws.send(JSON.stringify(body));
      } catch (e) {
        (0, _logger.messageLogger)(e, body);
      }
    });
    res.send('success');
  });
}