text: "use strict";
  
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  
  var fs = require('fs-extra');
  
  var createNodeFromEntity = require('./createNodeFromEntity');
  
  var destroyEntityNode = require('./destroyEntityNode');
  
  var Queue = require('promise-queue');
  
  var _require = require('../../utils'),
      getClient = _require.getClient,
      getLoader = _require.getLoader;
  
  module.exports = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(_ref, _ref2) {
      var actions, getNode, getNodesByType, reporter, parentSpan, schema, store, webhookBody, apiToken, environment, disableLiveReload, previewMode, apiUrl, rawLocaleFallbacks, localeFallbacks, client, loader, program, cacheDir, context, entity_id, entity_type, event_type, changesActivity, payload, linkedEntitiesIdsToFetch, linkedEntitiesPayload, _payload, activity, queue;
  
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              actions = _ref.actions, getNode = _ref.getNode, getNodesByType = _ref.getNodesByType, reporter = _ref.reporter, parentSpan = _ref.parentSpan, schema = _ref.schema, store = _ref.store, webhookBody = _ref.webhookBody;
              apiToken = _ref2.apiToken, environment = _ref2.environment, disableLiveReload = _ref2.disableLiveReload, previewMode = _ref2.previewMode, apiUrl = _ref2.apiUrl, rawLocaleFallbacks = _ref2.localeFallbacks;
              localeFallbacks = rawLocaleFallbacks || {};
              client = getClient({
                apiToken: apiToken,
                previewMode: previewMode,
                environment: environment,
                apiUrl: apiUrl
              });
              loader = getLoader({
                apiToken: apiToken,
                previewMode: previewMode,
                environment: environment,
                apiUrl: apiUrl
              });
              program = store.getState().program;
              cacheDir = "".concat(program.directory, "/.cache/datocms-assets");
  
              if (!fs.existsSync(cacheDir)) {
                fs.mkdirSync(cacheDir);
              }
  
              context = {
                entitiesRepo: loader.entitiesRepo,
                actions: actions,
                getNode: getNode,
                getNodesByType: getNodesByType,
                localeFallbacks: localeFallbacks,
                schema: schema,
                store: store,
                cacheDir: cacheDir
              };
  
              if (!(webhookBody && Object.keys(webhookBody).length)) {
                _context2.next = 45;
                break;
              }
  
              entity_id = webhookBody.entity_id, entity_type = webhookBody.entity_type, event_type = webhookBody.event_type;
              reporter.info("Received ".concat(event_type, " event for ").concat(entity_type, " ").concat(entity_id, " from DatoCMS"));
              changesActivity = reporter.activityTimer("loading DatoCMS content changes", {
                parentSpan: parentSpan
              });
              changesActivity.start();
              _context2.t0 = entity_type;
              _context2.next = _context2.t0 === 'item' ? 17 : _context2.t0 === 'upload' ? 32 : 41;
              break;
  
            case 17:
              if (!(event_type === 'publish')) {
                _context2.next = 30;
                break;
              }
  
              _context2.next = 20;
              return client.items.all({
                'filter[ids]': [entity_id].join(','),
                version: 'published'
              }, {
                deserializeResponse: false,
                allPages: true
              });
  
            case 20:
              payload = _context2.sent;
  
              if (!payload) {
                _context2.next = 28;
                break;
              }
  
              // `rich_text`, `links`, `link` fields link to other entities and we need to
              // fetch them separately to make sure we have all the data
              linkedEntitiesIdsToFetch = payload.data.reduce(function (collectedIds, payload) {
                var item_type_rel = payload.relationships.item_type.data;
                var itemTypeForThis = loader.entitiesRepo.findEntity(item_type_rel.type, item_type_rel.id);
                var fieldsToResolve = itemTypeForThis.fields.filter(function (fieldDef) {
                  return ["rich_text", "links", "link"].includes(fieldDef.fieldType);
                });
  
                function addRawValueToCollectedIds(fieldRawValue) {
                  if (Array.isArray(fieldRawValue)) {
                    fieldRawValue.forEach(collectedIds.add.bind(collectedIds));
                  } else if (fieldRawValue) {
                    collectedIds.add(fieldRawValue);
                  }
                }
  
                fieldsToResolve.forEach(function (fieldInfo) {
                  var fieldRawValue = payload.attributes[fieldInfo.apiKey];
  
                  if (fieldInfo.localized) {
                    // Localized fields raw values are object with lang codes
                    // as keys. We need to iterate over properties to
                    // collect ids from all languages
                    Object.values(fieldRawValue).forEach(function (fieldTranslationRawValue) {
                      addRawValueToCollectedIds(fieldTranslationRawValue);
                    });
                  } else {
                    addRawValueToCollectedIds(fieldRawValue);
                  }
                });
                return collectedIds;
              }, new Set());
              _context2.next = 25;
              return client.items.all({
                'filter[ids]': Array.from(linkedEntitiesIdsToFetch).join(','),
                version: 'published'
              }, {
                deserializeResponse: false,
                allPages: true
              });
  
            case 25:
              linkedEntitiesPayload = _context2.sent;
              // attach included portion of payload
              payload.included = linkedEntitiesPayload.data;
              loader.entitiesRepo.upsertEntities(payload);
  
            case 28:
              _context2.next = 31;
              break;
  
            case 30:
              if (event_type === 'unpublish') {
                loader.entitiesRepo.destroyEntities('item', [entity_id]);
              } else {
                reporter.warn("Invalid event type ".concat(event_type));
              }
  
            case 31:
              return _context2.abrupt("break", 43);
  
            case 32:
              if (!(event_type === 'create')) {
                _context2.next = 39;
                break;
              }
  
              _context2.next = 35;
              return client.uploads.all({
                'filter[ids]': [entity_id].join(','),
                version: 'published'
              }, {
                deserializeResponse: false,
                allPages: true
              });
  
            case 35:
              _payload = _context2.sent;
  
              if (_payload) {
                loader.entitiesRepo.upsertEntities(_payload);
              }
  
              _context2.next = 40;
              break;
  
            case 39:
              if (event_type === 'delete') {
                loader.entitiesRepo.destroyEntities('upload', [entity_id]);
              } else {
                reporter.warn("Invalid event type ".concat(event_type));
              }
  
            case 40:
              return _context2.abrupt("break", 43);
  
            case 41:
              reporter.warn("Invalid entity type ".concat(entity_type));
              return _context2.abrupt("break", 43);
  
            case 43:
              changesActivity.end();
              return _context2.abrupt("return");
  
            case 45:
              activity = reporter.activityTimer("loading DatoCMS content", {
                parentSpan: parentSpan
              });
              activity.start();
              loader.entitiesRepo.addUpsertListener(function (entity) {
                createNodeFromEntity(entity, context);
              });
              loader.entitiesRepo.addDestroyListener(function (entity) {
                destroyEntityNode(entity, context);
              });
              _context2.next = 51;
              return loader.load();
  
            case 51:
              activity.end();
              queue = new Queue(1, Infinity);
  
              if (process.env.NODE_ENV !== "production" && !disableLiveReload) {
                loader.watch(function (loadPromise) {
                  queue.add( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
                    var activity;
                    return regeneratorRuntime.wrap(function _callee$(_context) {
                      while (1) {
                        switch (_context.prev = _context.next) {
                          case 0:
                            activity = reporter.activityTimer("detected change in DatoCMS content, loading new data", {
                              parentSpan: parentSpan
                            });
                            activity.start();
                            _context.next = 4;
                            return loadPromise;
  
                          case 4:
                            activity.end();
  
                          case 5:
                          case "end":
                            return _context.stop();
                        }
                      }
                    }, _callee);
                  })));
                });
              }
  
            case 54:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));
  
    return function (_x, _x2) {
      return _ref3.apply(this, arguments);
    };
  }();