import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import test from "ava";
import Handler from "./handler.js";
import makeImmutable from "seamless-immutable";
var setIn = makeImmutable.setIn;
test("collaboration session reconciliation - single user", /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(t) {
    var handler, s1, latestState, latestPatch, s2;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            handler = new Handler({
              serverUrl: "https://udt-collaboration.workaround.now.sh",
              userName: "user1"
            });
            s1 = makeImmutable({
              interface: {
                type: "image_classification"
              },
              samples: [{
                imageUrl: "https://example.com/image1.jpg"
              }, {
                imageUrl: "https://example.com/image2.jpg"
              }, {
                imageUrl: "https://example.com/image3.jpg"
              }]
            });
            _context.next = 4;
            return handler.createSession(s1);

          case 4:
            _context.t0 = t;
            _context.next = 7;
            return handler.sendPatchIfChanged(s1);

          case 7:
            _context.t1 = _context.sent;
            _context.t2 = _context.t1 === null;

            _context.t0.assert.call(_context.t0, _context.t2);

            _context.next = 12;
            return handler.getLatestState();

          case 12:
            latestState = _context.sent;
            t.deepEqual(latestState.state, s1);
            _context.next = 16;
            return handler.applyLatestPatches();

          case 16:
            latestPatch = _context.sent;
            t.assert(latestPatch === null);
            s2 = setIn(s1, ["taskOutput"], [null, null, null]);
            _context.t3 = t;
            _context.next = 22;
            return handler.sendPatchIfChanged(s2);

          case 22:
            _context.t4 = _context.sent;
            _context.t5 = _context.t4 !== null;

            _context.t3.assert.call(_context.t3, _context.t5);

            _context.next = 27;
            return handler.applyLatestPatches();

          case 27:
            latestPatch = _context.sent;
            t.assert(latestPatch === null);

          case 29:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function (_x) {
    return _ref.apply(this, arguments);
  };
}());
test("collaboration session reconciliation - two users", /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(t) {
    var h1, h2, s1, s2, latestH2Patch;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            h1 = new Handler({
              serverUrl: "https://udt-collaboration.workaround.now.sh",
              userName: "user1"
            });
            h2 = new Handler({
              serverUrl: "https://udt-collaboration.workaround.now.sh",
              userName: "user2"
            });
            s1 = makeImmutable({
              interface: {
                type: "image_classification"
              },
              samples: [{
                imageUrl: "https://example.com/image1.jpg"
              }, {
                imageUrl: "https://example.com/image2.jpg"
              }, {
                imageUrl: "https://example.com/image3.jpg"
              }]
            });
            _context2.next = 5;
            return h1.createSession(s1);

          case 5:
            _context2.next = 7;
            return h2.joinSession(h1.sessionId);

          case 7:
            s2 = setIn(s1, ["taskOutput"], [null, null, null]);
            _context2.t0 = t;
            _context2.next = 11;
            return h1.sendPatchIfChanged(s2);

          case 11:
            _context2.t1 = _context2.sent;
            _context2.t2 = _context2.t1 !== null;

            _context2.t0.assert.call(_context2.t0, _context2.t2);

            _context2.next = 16;
            return h2.applyLatestPatches();

          case 16:
            latestH2Patch = _context2.sent;
            t.assert(latestH2Patch !== null);

          case 18:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));

  return function (_x2) {
    return _ref2.apply(this, arguments);
  };
}());