"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ViewsServiceTestkit = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _client = require("@wix/fe-essentials/http-client/testkit/client");
var _types = require("@wix/bex-utils/@wix/ambassador-app-settings-v1-settings/types");
var _http = require("@wix/bex-utils/@wix/ambassador-app-settings-v1-settings/http");
const aGetGlobalSettingsResponse = (overrides = {}) => ({
  ...overrides
});
const aSetGlobalSettingsResponse = (overrides = {}) => ({
  ...overrides
});
class ViewsServiceTestkit {
  constructor({
    namespace
  }) {
    (0, _defineProperty2.default)(this, "namespace", void 0);
    (0, _defineProperty2.default)(this, "mocks", []);
    (0, _defineProperty2.default)(this, "lastRequest", {
      views: [],
      hiddenPresetIds: []
    });
    this.namespace = namespace;
  }
  getNamespace() {
    return `${this.namespace}.views`;
  }
  shouldPersist(repeat) {
    return repeat === 0;
  }
  addMock(repeat = 0, cb) {
    this.shouldPersist(repeat) ? this.mocks.push(cb().persist()) : this.mocks.push(...Array.from(Array(repeat)).map(cb));
  }
  getMocks() {
    return this.mocks;
  }
  mockFetchViews({
    payload,
    options = {}
  }) {
    const {
      views = this.lastRequest.views || [],
      hiddenPresetIds = this.lastRequest.hiddenPresetIds || []
    } = payload;
    this.lastRequest = {
      views,
      hiddenPresetIds
    };
    const {
      repeat = 0
    } = options;
    const shouldPersist = this.shouldPersist(repeat);
    this.addMock(options == null ? void 0 : options.repeat, () => (0, _client.whenRequest)(_http.getGlobalSettings).withData(data => data.__CAIRO_VIEWS_TEST === 'true').reply(() => ({
      status: 200,
      data: aGetGlobalSettingsResponse({
        settings: {
          [this.getNamespace()]: {
            views: shouldPersist ? this.lastRequest.views : views,
            hiddenPresetIds: shouldPersist ? this.lastRequest.hiddenPresetIds : hiddenPresetIds
          }
        }
      })
    })));
  }
  mockUpdateViews({
    payload,
    options
  }) {
    const {
      views = []
    } = payload;
    this.addMock(options == null ? void 0 : options.repeat, () => (0, _client.whenRequest)(_http.setGlobalSettings).withData(data => {
      var _data$settings;
      const viewsData = data == null || (_data$settings = data.settings) == null ? void 0 : _data$settings[this.getNamespace()];
      this.mockFetchViews({
        payload: viewsData,
        options
      });
      options == null || options.stub == null || options.stub(viewsData);
      const noViews = !viewsData.views.length && !views.length;
      return data.host === _types.Host.BUSINESS_MANAGER && data.__CAIRO_VIEWS_TEST === 'true' && (noViews || viewsData.views.some(view1 => views.some(view2 => view1.name === view2.name)));
    }).reply(() => ({
      status: 200,
      data: aSetGlobalSettingsResponse({
        settings: {
          [this.getNamespace()]: {
            views
          }
        }
      })
    })));
  }
  mockUpdateHiddenPreset({
    payload,
    options
  }) {
    const {
      hiddenPresetIds = []
    } = payload;
    this.addMock(options == null ? void 0 : options.repeat, () => (0, _client.whenRequest)(_http.setGlobalSettings).withData(data => {
      var _data$settings2;
      this.mockFetchViews({
        payload: {
          views: [],
          hiddenPresetIds
        },
        options
      });
      options == null || options.stub == null || options.stub(data == null || (_data$settings2 = data.settings) == null ? void 0 : _data$settings2[this.getNamespace()]);
      return data.__CAIRO_VIEWS_TEST === 'true' && (data == null ? void 0 : data.settings[`${this.namespace}.views`].hiddenPresetIds.some(hiddenPresetId1 => hiddenPresetIds.some(hiddenPresetId2 => hiddenPresetId1 === hiddenPresetId2)));
    }).reply(() => ({
      status: 200,
      data: aSetGlobalSettingsResponse({
        settings: {
          [this.getNamespace()]: {
            hiddenPresetIds
          }
        }
      })
    })));
  }
}
exports.ViewsServiceTestkit = ViewsServiceTestkit;
//# sourceMappingURL=viewsServiceTestkit.js.map