"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mockAssetsClientFetchRequests = exports.forceCmdbBaseUrl = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _client = _interopRequireDefault(require("fetch-mock/cjs/client"));
var _data = require("./data");
_client.default.config.fallbackToNetwork = true;
var ASSETS_LIST_OF_LINKS_DATASOURCE_ID = '361d618a-3c04-40ad-9b27-3c8ea6927020';
var delay = 150;
var mockAssetsClientFetchRequests = exports.mockAssetsClientFetchRequests = function mockAssetsClientFetchRequests() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    _ref$delayedResponse = _ref.delayedResponse,
    delayedResponse = _ref$delayedResponse === void 0 ? true : _ref$delayedResponse;
  // Playwright VR tests do not like setTimeout
  var setTimeoutConfigured = delayedResponse ? setTimeout : function (cb, _) {
    return cb();
  };
  var workspaceId = '123';
  _client.default.get('/rest/servicedesk/cmdb/latest/workspace', /*#__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) {
            setTimeoutConfigured(function () {
              resolve({
                results: [{
                  id: workspaceId
                }]
              });
            }, delay);
          }));
        case 1:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })));
  _client.default.get("begin:/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/objectschema/list"), /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(url) {
      var urlParams, query;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            urlParams = new URLSearchParams(url);
            query = urlParams.get('query');
            return _context2.abrupt("return", new Promise(function (resolve) {
              setTimeoutConfigured(function () {
                if (query) {
                  var filteredValues = _data.objectSchemaListResponse.values.filter(function (objectSchema) {
                    return objectSchema.name.toLowerCase().includes(query.toLowerCase());
                  });
                  resolve({
                    values: filteredValues
                  });
                }
                resolve(_data.objectSchemaListResponse);
              }, delay);
            }));
          case 3:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    return function (_x) {
      return _ref3.apply(this, arguments);
    };
  }());
  _client.default.get(new RegExp("/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/objectschema/\\d+")), /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(url) {
      var id;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            id = url.split('/').pop();
            return _context3.abrupt("return", new Promise(function (resolve, reject) {
              setTimeoutConfigured(function () {
                var objectSchema = _data.objectSchemaListResponse.values.find(function (objectSchema) {
                  return objectSchema.id === id;
                });
                if (objectSchema) {
                  resolve(objectSchema);
                } else {
                  reject();
                }
              }, delay);
            }));
          case 2:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    return function (_x2) {
      return _ref4.apply(this, arguments);
    };
  }());
  _client.default.post("/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/aql/validate"), /*#__PURE__*/function () {
    var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(_, request) {
      var requestJson;
      return _regenerator.default.wrap(function _callee4$(_context4) {
        while (1) switch (_context4.prev = _context4.next) {
          case 0:
            requestJson = JSON.parse(request.body);
            return _context4.abrupt("return", new Promise(function (resolve) {
              setTimeoutConfigured(function () {
                var isValid = requestJson.qlQuery.includes('invalid') ? false : true;
                resolve({
                  isValid: isValid
                });
              }, delay);
            }));
          case 2:
          case "end":
            return _context4.stop();
        }
      }, _callee4);
    }));
    return function (_x3, _x4) {
      return _ref5.apply(this, arguments);
    };
  }());
  _client.default.post(new RegExp("/object-resolver/datasource/".concat(ASSETS_LIST_OF_LINKS_DATASOURCE_ID, "/fetch/details")), /*#__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) {
            return resolve(_data.assetsDefaultDetails);
          }));
        case 1:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  })));
  _client.default.post(new RegExp("/gateway/api/object-resolver/datasource/".concat(ASSETS_LIST_OF_LINKS_DATASOURCE_ID, "/fetch/data")), /*#__PURE__*/function () {
    var _ref7 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(url) {
      return _regenerator.default.wrap(function _callee6$(_context6) {
        while (1) switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", new Promise(function (resolve) {
              return resolve((0, _data.generateDataResponse)({
                initialVisibleColumnKeys: _data.assetsDefaultInitialVisibleColumnKeys
              }));
            }));
          case 1:
          case "end":
            return _context6.stop();
        }
      }, _callee6);
    }));
    return function (_x5) {
      return _ref7.apply(this, arguments);
    };
  }());
};
var forceCmdbBaseUrl = exports.forceCmdbBaseUrl = function forceCmdbBaseUrl(baseUrl) {
  _client.default.mock(/^\//, function (url, init) {
    return fetch("".concat(baseUrl).concat(url), init);
  });
};