"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ProductsCollectionStateDriver = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _WixPatternsContainer = require("./WixPatternsContainer.driver");
var _state = require("../state");
var _createProductsBackend = require("./createProductsBackend");
var _util = require("../util");
class ProductsCollectionStateDriver {
  constructor(overrides = {}) {
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "backend", void 0);
    (0, _defineProperty2.default)(this, "overrides", void 0);
    (0, _defineProperty2.default)(this, "collection", void 0);
    (0, _defineProperty2.default)(this, "fetchDataStartSpy", () => {
      const spy = jest.fn();
      this.backend.events.on('fetchDataStart', spy);
      return spy;
    });
    this.overrides = overrides;
    this.container = new _WixPatternsContainer.WixPatternsContainerDriver(overrides);
    this.backend = (0, _createProductsBackend.createProductsBackend)(overrides.backend);
    const queryName = 'wix.cairo.products.v1.product';
    const fqdn = overrides.fqdn ? (0, _util.parseFqdn)(overrides.fqdn) : (0, _util.parseFqdn)(queryName);
    this.collection = new _state.CollectionState({
      queryName,
      fqdn,
      toExtendedFields: item => item.extendedFields,
      ...(overrides.withFetchTotal && {
        fetchTotal: query => {
          return this.backend.fetchTotal(query);
        }
      }),
      fetchData: query => this.backend.fetchData(query),
      // not passing collection.fetch directly to be able to spy on it
      itemKey: item => item.id,
      itemName: item => item.name,
      filters: {},
      limit: 50,
      onlineState: this.container.onlineState,
      translate: this.container.i18n.t.bind(this.container.i18n),
      queryCache: this.container.queryCache,
      history: this.container.history,
      errorMonitor: this.container.errorMonitor,
      errorHandler: this.container.errorHandler,
      showToast: this.container.showToast,
      initDependencies: () => this.container.container.initialFetch(),
      lodash: this.container.container.lodash,
      ...overrides.collection
    });
  }
  reCreateCollection() {
    const {
      queryName,
      query: {
        limit
      },
      filters
    } = this.collection;
    this.collection = new _state.CollectionState({
      queryName,
      fqdn: (0, _util.parseFqdn)(queryName),
      fetchData: query => this.backend.fetchData(query),
      // not passing collection.fetch directly to be able to spy on it
      itemKey: item => item.id,
      itemName: item => item.name,
      filters,
      limit,
      onlineState: this.container.onlineState,
      translate: this.container.i18n.t.bind(this.container.i18n),
      queryCache: this.container.queryCache,
      history: this.container.history,
      errorMonitor: this.container.errorMonitor,
      errorHandler: this.container.errorHandler,
      showToast: this.container.showToast,
      initDependencies: () => this.container.container.initialFetch(),
      lodash: this.container.container.lodash,
      ...this.overrides.collection
    });
  }
  getRandomItem() {
    const {
      items
    } = this;
    const index = this.backend.chance.integer({
      min: 0,
      max: items.length - 1
    });
    return {
      index,
      item: items[index]
    };
  }
  get items() {
    const {
      collection: {
        query
      }
    } = this;
    const {
      items
    } = this.backend.fetchDataSync(query.asComputed);
    return items;
  }
  get chance() {
    return this.backend.chance;
  }
}
exports.ProductsCollectionStateDriver = ProductsCollectionStateDriver;
//# sourceMappingURL=ProductsCollectionState.driver.js.map