"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "ORS_ACTIONS_DISCOVERY_ENDPOINT", {
  enumerable: true,
  get: function get() {
    return _actions.ORS_ACTIONS_DISCOVERY_ENDPOINT;
  }
});
Object.defineProperty(exports, "assetsDefaultInitialVisibleColumnKeys", {
  enumerable: true,
  get: function get() {
    return _data.assetsDefaultInitialVisibleColumnKeys;
  }
});
Object.defineProperty(exports, "defaultInitialVisibleConfluenceColumnKeys", {
  enumerable: true,
  get: function get() {
    return _data2.defaultInitialVisibleColumnKeys;
  }
});
Object.defineProperty(exports, "defaultInitialVisibleJiraColumnKeys", {
  enumerable: true,
  get: function get() {
    return _data3.defaultInitialVisibleColumnKeys;
  }
});
Object.defineProperty(exports, "failedRecommendationAPIResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.failedRecommendationAPIResponse;
  }
});
Object.defineProperty(exports, "failedUserQueryResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.failedUserQueryResponse;
  }
});
Object.defineProperty(exports, "fieldValuesEmptyResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesEmptyResponse;
  }
});
Object.defineProperty(exports, "fieldValuesEmptyResponseMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesEmptyResponseMapped;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForAssignees", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForAssignees;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForAssigneesMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForAssigneesMapped;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForProjects", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForProjects;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForProjectsMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForProjectsMapped;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForProjectsMoreData", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForProjectsMoreData;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForStatuses", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForStatuses;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForStatusesMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForStatusesMapped;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForStatusesSearched", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForStatusesSearched;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForTypes", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForTypes;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForTypesMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForTypesMapped;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForTypesWithRelativeUrls", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForTypesWithRelativeUrls;
  }
});
Object.defineProperty(exports, "fieldValuesResponseForTypesWithRelativeUrlsMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.fieldValuesResponseForTypesWithRelativeUrlsMapped;
  }
});
exports.forceBaseUrl = void 0;
Object.defineProperty(exports, "hydrateJqlEmptyResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.hydrateJqlEmptyResponse;
  }
});
Object.defineProperty(exports, "hydrateJqlEmptyResponseMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.hydrateJqlEmptyResponseMapped;
  }
});
Object.defineProperty(exports, "hydrateJqlStandardResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.hydrateJqlStandardResponse;
  }
});
Object.defineProperty(exports, "hydrateJqlStandardResponseForVRTesting", {
  enumerable: true,
  get: function get() {
    return _mocks.hydrateJqlStandardResponseForVRTesting;
  }
});
Object.defineProperty(exports, "hydrateJqlStandardResponseMapped", {
  enumerable: true,
  get: function get() {
    return _mocks.hydrateJqlStandardResponseMapped;
  }
});
Object.defineProperty(exports, "mockActionsDiscovery", {
  enumerable: true,
  get: function get() {
    return _actions.mockActionsDiscovery;
  }
});
Object.defineProperty(exports, "mockAssetsClientFetchRequests", {
  enumerable: true,
  get: function get() {
    return _assets.mockAssetsClientFetchRequests;
  }
});
Object.defineProperty(exports, "mockAutoCompleteData", {
  enumerable: true,
  get: function get() {
    return _data3.mockAutoCompleteData;
  }
});
Object.defineProperty(exports, "mockBasicFilterAGGFetchRequests", {
  enumerable: true,
  get: function get() {
    return _index.mockBasicFilterAGGFetchRequests;
  }
});
exports.mockDatasourceFetchRequests = void 0;
Object.defineProperty(exports, "mockJiraData", {
  enumerable: true,
  get: function get() {
    return _data3.mockJiraData;
  }
});
Object.defineProperty(exports, "mockProductsData", {
  enumerable: true,
  get: function get() {
    return _data3.mockProductsData;
  }
});
Object.defineProperty(exports, "mockSite", {
  enumerable: true,
  get: function get() {
    return _data3.mockSite;
  }
});
Object.defineProperty(exports, "mockSiteData", {
  enumerable: true,
  get: function get() {
    return _data3.mockSiteData;
  }
});
Object.defineProperty(exports, "mockSuggestionData", {
  enumerable: true,
  get: function get() {
    return _data3.mockSuggestionData;
  }
});
Object.defineProperty(exports, "successfulRecommendationAPIResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.successfulRecommendationAPIResponse;
  }
});
Object.defineProperty(exports, "successfulUserQueryResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.successfulUserQueryResponse;
  }
});
Object.defineProperty(exports, "successfuluserHydrationResponse", {
  enumerable: true,
  get: function get() {
    return _mocks.successfuluserHydrationResponse;
  }
});
Object.defineProperty(exports, "transformedRecommendationMockFilterOptions", {
  enumerable: true,
  get: function get() {
    return _mocks.transformedRecommendationMockFilterOptions;
  }
});
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _client = _interopRequireDefault(require("fetch-mock/cjs/client"));
var _actions = require("./actions");
var _assets = require("./assets");
var _data = require("./assets/data");
var _mocks = require("./basic-filters/mocks");
var _data2 = require("./confluence/data");
var confluenceMocks = _interopRequireWildcard(require("./confluence/mocks"));
var _data3 = require("./jira/data");
var jiraMocks = _interopRequireWildcard(require("./jira/mocks"));
var _index = require("./basic-filters/index");
var _excluded = ["type", "datasourceId", "shouldMockORSBatch", "delayedResponse", "availableSitesOverride", "mockExecutionDelay", "accessibleProductsOverride"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
_client.default.config.fallbackToNetwork = true;
var numberOfLoads = 0;
var mockDatasourceFetchRequests = exports.mockDatasourceFetchRequests = function mockDatasourceFetchRequests() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'jira' : _ref$type,
    string = _ref.datasourceId,
    _ref$shouldMockORSBat = _ref.shouldMockORSBatch,
    shouldMockORSBatch = _ref$shouldMockORSBat === void 0 ? false : _ref$shouldMockORSBat,
    _ref$delayedResponse = _ref.delayedResponse,
    delayedResponse = _ref$delayedResponse === void 0 ? true : _ref$delayedResponse,
    availableSitesOverride = _ref.availableSitesOverride,
    _ref$mockExecutionDel = _ref.mockExecutionDelay,
    mockExecutionDelay = _ref$mockExecutionDel === void 0 ? 600 : _ref$mockExecutionDel,
    accessibleProductsOverride = _ref.accessibleProductsOverride,
    rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var datasourceMatcher = '[^/]+';

  // Playwright VR tests do not like setTimeout
  // eslint-disable-next-line no-unused-vars
  var setTimeoutConfigured = delayedResponse ? setTimeout : function (cb, _) {
    return cb();
  };
  var initialVisibleColumnKeys = function () {
    if (rest.initialVisibleColumnKeys) {
      return rest.initialVisibleColumnKeys;
    }
    if (type === 'jira') {
      return jiraMocks.defaultInitialVisibleColumnKeys;
    }
    if (type === 'confluence') {
      return confluenceMocks.defaultInitialVisibleColumnKeys;
    }
    return [];
  }();
  _client.default.post(new RegExp("object-resolver/datasource/".concat(datasourceMatcher, "/fetch/details")), /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          return _context.abrupt("return", new Promise(function (resolve, reject) {
            if (type === 'jira') {
              var response = jiraMocks.generateDetailsResponse(initialVisibleColumnKeys);
              return resolve(response);
            }
            if (type === 'confluence') {
              var _response = confluenceMocks.generateDetailsResponse(initialVisibleColumnKeys);
              return resolve(_response);
            }
            return reject(new Error("Unhandled type ".concat(type, " when mocking fetc/details")));
          }));
        case 1:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })));

  // Mock this for the editor's testing examples.
  if (shouldMockORSBatch) {
    _client.default.post(new RegExp("object-resolver/resolve/batch"), /*#__PURE__*/function () {
      var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(url, request) {
        var getMock, requestJson, resourceUrl, mock;
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              getMock = function _getMock(resourceUrl) {
                if (type === 'jira') {
                  return jiraMocks.generateResolveResponse(resourceUrl);
                }
                if (type === 'confluence') {
                  return confluenceMocks.generateResolveResponse(resourceUrl);
                }
              };
              requestJson = JSON.parse(request.body);
              if (!(requestJson.length === 1)) {
                _context2.next = 7;
                break;
              }
              resourceUrl = requestJson[0].resourceUrl;
              mock = getMock(resourceUrl);
              if (!mock) {
                _context2.next = 7;
                break;
              }
              return _context2.abrupt("return", Promise.resolve([mock]));
            case 7:
              return _context2.abrupt("return", _client.default.realFetch(url, {
                method: 'POST',
                headers: request.headers,
                body: request.body
              }));
            case 8:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x, _x2) {
        return _ref3.apply(this, arguments);
      };
    }());
  }
  _client.default.post(new RegExp("/gateway/api/object-resolver/datasource/".concat(datasourceMatcher, "/fetch/data")), /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(url, details) {
      var requestBody, cloudId, includeSchema, fields, requestedColumnKeys, getMock;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            getMock = function _getMock2() {
              if (type === 'jira') {
                return jiraMocks.generateDataResponse({
                  cloudId: cloudId,
                  numberOfLoads: numberOfLoads,
                  includeSchema: includeSchema,
                  initialVisibleColumnKeys: requestedColumnKeys
                });
              }
              if (type === 'confluence') {
                return confluenceMocks.generateDataResponse({
                  cloudId: cloudId,
                  numberOfLoads: numberOfLoads,
                  includeSchema: includeSchema,
                  initialVisibleColumnKeys: requestedColumnKeys
                });
              }
            };
            requestBody = JSON.parse(details.body);
            cloudId = requestBody.parameters.cloudId, includeSchema = requestBody.includeSchema, fields = requestBody.fields; // Mirror production: when the client requests specific `fields`, return data
            // (and schema, if requested) for those fields. Otherwise, fall back to the
            // mock's configured default visible columns.
            requestedColumnKeys = Array.isArray(fields) && fields.length > 0 ? fields : initialVisibleColumnKeys;
            return _context3.abrupt("return", new Promise(function (resolve, reject) {
              var delay = numberOfLoads * 1000;
              setTimeoutConfigured(function () {
                try {
                  var mockResponse = getMock();
                  resolve(mockResponse);
                } catch (err) {
                  reject(err);
                }
                numberOfLoads += 1;
              }, delay);
            }));
          case 5:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    return function (_x3, _x4) {
      return _ref4.apply(this, arguments);
    };
  }());
  _client.default.post(/api\/available-sites/, /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
    return _regenerator.default.wrap(function _callee4$(_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          return _context4.abrupt("return", new Promise(function (resolve) {
            resolve({
              sites: availableSitesOverride || _data3.mockSiteData
            });
          }));
        case 1:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  })));
  _client.default.post(/api\/v2\/accessible-products/, /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5() {
    return _regenerator.default.wrap(function _callee5$(_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          return _context5.abrupt("return", new Promise(function (resolve) {
            resolve({
              data: {
                products: accessibleProductsOverride || _data3.mockProductsData
              }
            });
          }));
        case 1:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  })));
  _client.default.get(/\/api\/ex\/jira\/.+\/\/rest\/api\/latest\/jql\/autocompletedata\/suggestions\?.+/, /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6() {
    return _regenerator.default.wrap(function _callee6$(_context6) {
      while (1) switch (_context6.prev = _context6.next) {
        case 0:
          return _context6.abrupt("return", new Promise(function (resolve) {
            var delay = 150;
            setTimeoutConfigured(function () {
              resolve(_data3.mockSuggestionData);
            }, delay);
          }));
        case 1:
        case "end":
          return _context6.stop();
      }
    }, _callee6);
  })));
  _client.default.post(new RegExp("/gateway/api/v1/recommendations"), /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7() {
    return _regenerator.default.wrap(function _callee7$(_context7) {
      while (1) switch (_context7.prev = _context7.next) {
        case 0:
          return _context7.abrupt("return", Promise.resolve(_mocks.successfulRecommendationAPIResponse));
        case 1:
        case "end":
          return _context7.stop();
      }
    }, _callee7);
  })));
  _client.default.post(/\/api\/ex\/jira\/.+\/rest\/api\/latest\/jql\/autocompletedata/, /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8() {
    return _regenerator.default.wrap(function _callee8$(_context8) {
      while (1) switch (_context8.prev = _context8.next) {
        case 0:
          return _context8.abrupt("return", new Promise(function (resolve) {
            var delay = 150;
            setTimeoutConfigured(function () {
              resolve(_data3.mockAutoCompleteData);
            }, delay);
          }));
        case 1:
        case "end":
          return _context8.stop();
      }
    }, _callee8);
  })));
  (0, _actions.mockActionsDiscovery)();
  (0, _actions.mockActionsExecution)(mockExecutionDelay);
};
var forceBaseUrl = exports.forceBaseUrl = function forceBaseUrl(baseUrl) {
  _client.default.post(/^\//, function (url, init) {
    return fetch("".concat(baseUrl).concat(url), init);
  });
};