define('@cardstack/test-support/fixtures', ['exports', '@cardstack/test-support/jsonapi-factory', '@cardstack/plugin-utils/environment', '@cardstack/test-support/environment'], function (exports, _jsonapiFactory, _environment, _environment2) {
  'use strict';

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

  var _slicedToArray = function () {
    function sliceIterator(arr, i) {
      var _arr = [];
      var _n = true;
      var _d = false;
      var _e = undefined;

      try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);

          if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;
        _e = err;
      } finally {
        try {
          if (!_n && _i["return"]) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }

      return _arr;
    }

    return function (arr, i) {
      if (Array.isArray(arr)) {
        return arr;
      } else if (Symbol.iterator in Object(arr)) {
        return sliceIterator(arr, i);
      } else {
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
      }
    };
  }();

  function _asyncToGenerator(fn) {
    return function () {
      var gen = fn.apply(this, arguments);
      return new Promise(function (resolve, reject) {
        function step(key, arg) {
          try {
            var info = gen[key](arg);
            var value = info.value;
          } catch (error) {
            reject(error);
            return;
          }

          if (info.done) {
            resolve(value);
          } else {
            return Promise.resolve(value).then(function (value) {
              step("next", value);
            }, function (err) {
              step("throw", err);
            });
          }
        }

        return step("next");
      });
    };
  }

  function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  }

  var _createClass = function () {
    function defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    return function (Constructor, protoProps, staticProps) {
      if (protoProps) defineProperties(Constructor.prototype, protoProps);
      if (staticProps) defineProperties(Constructor, staticProps);
      return Constructor;
    };
  }();

  var Fixtures = function () {
    function Fixtures(_ref) {
      var create = _ref.create,
          destroy = _ref.destroy;

      _classCallCheck(this, Fixtures);

      this._factory = new _jsonapiFactory.default();
      this._create = create;
      this._destroy = destroy;
    }

    _createClass(Fixtures, [{
      key: 'setupTest',
      value: function setupTest(hooks) {
        var _this = this;

        hooks.beforeEach(_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return _this.teardown();

                case 2:
                  _context.next = 4;
                  return _this.setup();

                case 4:
                case 'end':
                  return _context.stop();
              }
            }
          }, _callee, _this);
        })));
        hooks.afterEach(_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
          return regeneratorRuntime.wrap(function _callee2$(_context2) {
            while (1) {
              switch (_context2.prev = _context2.next) {
                case 0:
                  _context2.next = 2;
                  return _this.teardown();

                case 2:
                  return _context2.abrupt('return', _context2.sent);

                case 3:
                case 'end':
                  return _context2.stop();
              }
            }
          }, _callee2, _this);
        })));
      }
    }, {
      key: 'setup',
      value: function () {
        var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
          var models, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _ref5, _ref6, index, model, url, response;

          return regeneratorRuntime.wrap(function _callee3$(_context3) {
            while (1) {
              switch (_context3.prev = _context3.next) {
                case 0:
                  if (!(typeof this._create !== 'function')) {
                    _context3.next = 2;
                    break;
                  }

                  return _context3.abrupt('return');

                case 2:
                  _context3.next = 4;
                  return this._create(this._factory);

                case 4:
                  models = this._factory.getModels();
                  _iteratorNormalCompletion = true;
                  _didIteratorError = false;
                  _iteratorError = undefined;
                  _context3.prev = 8;
                  _iterator = models.entries()[Symbol.iterator]();

                case 10:
                  if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
                    _context3.next = 31;
                    break;
                  }

                  _ref5 = _step.value;
                  _ref6 = _slicedToArray(_ref5, 2);
                  index = _ref6[0];
                  model = _ref6[1];
                  url = _environment.hubURL + '/api/' + model.type;

                  if (index < models.length - 1) {
                    // On all but the last api request, we opt in to not waiting
                    // for the content to be indexed. This lets us move faster
                    // and then wait for indexing to happen once at the end.
                    url += '?nowait';
                  }
                  _context3.next = 19;
                  return fetch(url, {
                    method: 'POST',
                    headers: {
                      authorization: 'Bearer ' + _environment2.ciSessionId
                    },
                    body: JSON.stringify({
                      data: {
                        id: model.id,
                        type: model.type,
                        attributes: model.attributes,
                        relationships: model.relationships
                      }
                    })
                  });

                case 19:
                  response = _context3.sent;

                  if (!(response.status !== 201)) {
                    _context3.next = 28;
                    break;
                  }

                  _context3.t0 = Error;
                  _context3.t1 = 'Unexpected response ' + response.status + ' while trying to define fixtures: ';
                  _context3.next = 25;
                  return response.text();

                case 25:
                  _context3.t2 = _context3.sent;
                  _context3.t3 = _context3.t1 + _context3.t2;
                  throw new _context3.t0(_context3.t3);

                case 28:
                  _iteratorNormalCompletion = true;
                  _context3.next = 10;
                  break;

                case 31:
                  _context3.next = 37;
                  break;

                case 33:
                  _context3.prev = 33;
                  _context3.t4 = _context3['catch'](8);
                  _didIteratorError = true;
                  _iteratorError = _context3.t4;

                case 37:
                  _context3.prev = 37;
                  _context3.prev = 38;

                  if (!_iteratorNormalCompletion && _iterator.return) {
                    _iterator.return();
                  }

                case 40:
                  _context3.prev = 40;

                  if (!_didIteratorError) {
                    _context3.next = 43;
                    break;
                  }

                  throw _iteratorError;

                case 43:
                  return _context3.finish(40);

                case 44:
                  return _context3.finish(37);

                case 45:
                case 'end':
                  return _context3.stop();
              }
            }
          }, _callee3, this, [[8, 33, 37, 45], [38,, 40, 44]]);
        }));

        function setup() {
          return _ref4.apply(this, arguments);
        }

        return setup;
      }()
    }, {
      key: 'teardown',
      value: function () {
        var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
          var destructionList, createdModels, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, item, response, _ref8, model, _response, _ref9, models, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _iterator3, _step3, _model;

          return regeneratorRuntime.wrap(function _callee4$(_context4) {
            while (1) {
              switch (_context4.prev = _context4.next) {
                case 0:
                  if (!(typeof this._create !== 'function')) {
                    _context4.next = 2;
                    break;
                  }

                  return _context4.abrupt('return');

                case 2:
                  destructionList = [];

                  if (typeof this._destroy === 'function') {
                    destructionList = destructionList.concat(this._destroy());
                  }

                  createdModels = this._factory.getModels().map(function (model) {
                    return { id: model.id, type: model.type };
                  });

                  destructionList = destructionList.concat(createdModels.reverse());

                  _iteratorNormalCompletion2 = true;
                  _didIteratorError2 = false;
                  _iteratorError2 = undefined;
                  _context4.prev = 9;
                  _iterator2 = destructionList[Symbol.iterator]();

                case 11:
                  if (_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done) {
                    _context4.next = 67;
                    break;
                  }

                  item = _step2.value;

                  if (item.type) {
                    _context4.next = 15;
                    break;
                  }

                  return _context4.abrupt('continue', 64);

                case 15:
                  if (!item.id) {
                    _context4.next = 29;
                    break;
                  }

                  _context4.next = 18;
                  return fetch(_environment.hubURL + '/api/' + item.type + '/' + item.id, {
                    method: 'GET',
                    headers: { authorization: 'Bearer ' + _environment2.ciSessionId }
                  });

                case 18:
                  response = _context4.sent;

                  if (!(response.status !== 200)) {
                    _context4.next = 21;
                    break;
                  }

                  return _context4.abrupt('continue', 64);

                case 21:
                  _context4.next = 23;
                  return response.json();

                case 23:
                  _ref8 = _context4.sent;
                  model = _ref8.data;
                  _context4.next = 27;
                  return this._deleteModel(model);

                case 27:
                  _context4.next = 64;
                  break;

                case 29:
                  _context4.next = 31;
                  return fetch(_environment.hubURL + '/api/' + item.type, {
                    method: 'GET',
                    headers: { authorization: 'Bearer ' + _environment2.ciSessionId }
                  });

                case 31:
                  _response = _context4.sent;

                  if (!(_response.status !== 200)) {
                    _context4.next = 34;
                    break;
                  }

                  return _context4.abrupt('continue', 64);

                case 34:
                  _context4.next = 36;
                  return _response.json();

                case 36:
                  _ref9 = _context4.sent;
                  models = _ref9.data;
                  _iteratorNormalCompletion3 = true;
                  _didIteratorError3 = false;
                  _iteratorError3 = undefined;
                  _context4.prev = 41;
                  _iterator3 = models[Symbol.iterator]();

                case 43:
                  if (_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done) {
                    _context4.next = 50;
                    break;
                  }

                  _model = _step3.value;
                  _context4.next = 47;
                  return this._deleteModel(_model);

                case 47:
                  _iteratorNormalCompletion3 = true;
                  _context4.next = 43;
                  break;

                case 50:
                  _context4.next = 56;
                  break;

                case 52:
                  _context4.prev = 52;
                  _context4.t0 = _context4['catch'](41);
                  _didIteratorError3 = true;
                  _iteratorError3 = _context4.t0;

                case 56:
                  _context4.prev = 56;
                  _context4.prev = 57;

                  if (!_iteratorNormalCompletion3 && _iterator3.return) {
                    _iterator3.return();
                  }

                case 59:
                  _context4.prev = 59;

                  if (!_didIteratorError3) {
                    _context4.next = 62;
                    break;
                  }

                  throw _iteratorError3;

                case 62:
                  return _context4.finish(59);

                case 63:
                  return _context4.finish(56);

                case 64:
                  _iteratorNormalCompletion2 = true;
                  _context4.next = 11;
                  break;

                case 67:
                  _context4.next = 73;
                  break;

                case 69:
                  _context4.prev = 69;
                  _context4.t1 = _context4['catch'](9);
                  _didIteratorError2 = true;
                  _iteratorError2 = _context4.t1;

                case 73:
                  _context4.prev = 73;
                  _context4.prev = 74;

                  if (!_iteratorNormalCompletion2 && _iterator2.return) {
                    _iterator2.return();
                  }

                case 76:
                  _context4.prev = 76;

                  if (!_didIteratorError2) {
                    _context4.next = 79;
                    break;
                  }

                  throw _iteratorError2;

                case 79:
                  return _context4.finish(76);

                case 80:
                  return _context4.finish(73);

                case 81:
                case 'end':
                  return _context4.stop();
              }
            }
          }, _callee4, this, [[9, 69, 73, 81], [41, 52, 56, 64], [57,, 59, 63], [74,, 76, 80]]);
        }));

        function teardown() {
          return _ref7.apply(this, arguments);
        }

        return teardown;
      }()
    }, {
      key: '_deleteModel',
      value: function () {
        var _ref10 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(model) {
          var version, headers;
          return regeneratorRuntime.wrap(function _callee5$(_context5) {
            while (1) {
              switch (_context5.prev = _context5.next) {
                case 0:
                  version = model.meta && model.meta.version;
                  headers = { authorization: 'Bearer ' + _environment2.ciSessionId };

                  if (version) {
                    headers["If-Match"] = version;
                  }
                  _context5.next = 5;
                  return fetch(_environment.hubURL + '/api/' + model.type + '/' + model.id, {
                    method: 'DELETE',
                    headers: headers
                  });

                case 5:
                case 'end':
                  return _context5.stop();
              }
            }
          }, _callee5, this);
        }));

        function _deleteModel(_x) {
          return _ref10.apply(this, arguments);
        }

        return _deleteModel;
      }()
    }]);

    return Fixtures;
  }();

  exports.default = Fixtures;
});