define('@cardstack/models/adapter', ['exports', 'ember-data', 'ember-resource-metadata/adapter-mixin', '@cardstack/plugin-utils/environment', 'ember-inject-optional'], function (exports, _emberData, _adapterMixin, _environment, _emberInjectOptional) {
  'use strict';

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

  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");
      });
    };
  }

  exports.default = _emberData.default.JSONAPIAdapter.extend(_adapterMixin.default, {
    host: _environment.hubURL,
    namespace: 'api',
    cardstackSession: _emberInjectOptional.default.service(),
    session: _emberInjectOptional.default.service(),

    // queryRecord can use the hub's page.size control to just do a
    // query of with a limit of 1.
    queryRecord: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(store, type, query) {
        var upstreamQuery, response, returnValue;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                upstreamQuery = Ember.assign({}, query);

                upstreamQuery.page = { size: 1 };
                _context.next = 4;
                return this._super(store, type, upstreamQuery);

              case 4:
                response = _context.sent;

                if (!(!response.data || !Array.isArray(response.data) || response.data.length < 1)) {
                  _context.next = 7;
                  break;
                }

                throw new _emberData.default.AdapterError([{ code: 404, title: 'Not Found', detail: 'branch-adapter queryRecord got less than one record back' }]);

              case 7:
                returnValue = {
                  data: response.data[0]
                };

                if (response.meta) {
                  returnValue.meta = response.meta;
                }
                return _context.abrupt('return', returnValue);

              case 10:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function queryRecord(_x, _x2, _x3) {
        return _ref.apply(this, arguments);
      }

      return queryRecord;
    }(),
    deleteRecord: function deleteRecord(store, type, snapshot) {
      var id = snapshot.id;

      var options = {};
      var metaService = this.get('_resourceMetadata');
      var meta = metaService.peek(snapshot.record);
      var version = Ember.get(meta, 'version');

      if (version) {
        options.headers = { "If-Match": version };
      }

      // Note: this bypasses the `ds-improved-ajax` feature
      return this.ajax(this.buildURL(type.modelName, id, snapshot, 'deleteRecord'), "DELETE", options);
    },
    ajaxOptions: function ajaxOptions() {
      var hash = this._super.apply(this, arguments);

      if (this.get('cardstackSession')) {
        // @cardstack/authentication is available. If it has a valid
        // session, apply the token to our request

        var beforeSend = hash.beforeSend;

        var _get = this.get('session.data.authenticated'),
            access_token = _get.access_token;

        hash.beforeSend = function (xhr) {
          xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
          if (beforeSend) {
            beforeSend(xhr);
          }
        };
      }
      return hash;
    }
  });
});