"use strict";

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

var _path = _interopRequireDefault(require("path"));

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

var _utils = require("../../../src/utils");

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

function websocketMockSetup(app, httpsServer) {
  app.use('/wms/*', (req, res) => {
    res.sendFile(_path.default.join(__dirname, '..', '..', 'templates', '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, _utils.log)('received: %s', message);
    });
    ws.send('something');
  });
  app.post('/wmsmockapi', (req, res) => {
    wsPool.forEach(ws => {
      let {
        body
      } = req;

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