"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ViewsLocalSearchBIReporter = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
var _bi = require("@wix/bex-core/bi");
var _events = require("events");
var _util = require("@wix/bex-core/util");
class ViewsLocalSearchBIReporter {
  get views() {
    return this.state.views;
  }
  get reportBI() {
    return this.views.reportBI;
  }
  constructor(params) {
    (0, _defineProperty2.default)(this, "state", void 0);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    this.state = params.state;
    (0, _mobx.makeObservable)(this, {
      totalViews: _mobx.computed,
      totalFilteredViewsAndCategories: _mobx.computed,
      totalFilteredCategories: _mobx.computed
    });
  }
  _clearAllEvents() {
    const {
      events
    } = this;
    events.removeAllListeners('afterSelectView');
    events.removeAllListeners('blur');
  }
  onSearch() {
    const {
      events,
      state
    } = this;
    const afterSearchParams = {
      numViews: this.totalViews,
      searchResultsCnt: this.totalFilteredViewsAndCategories,
      numCategoriesInResults: this.totalFilteredCategories,
      searchTerm: state.localSearchValue
    };
    const reportViewSelectedSearchBI = () => {
      this._clearAllEvents();
      this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoSearchViewInViews)({
        ...afterSearchParams,
        endType: 'View selected'
      }));
    };
    const reportSearchDismissedSearchBI = () => {
      this._clearAllEvents();
      this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoSearchViewInViews)({
        ...afterSearchParams,
        endType: 'Search dismissed'
      }));
    };
    this._clearAllEvents();
    events.once('afterSelectView', reportViewSelectedSearchBI);
    events.once('blur', reportSearchDismissedSearchBI);
  }
  get totalViews() {
    const {
      categoriesWithViews: {
        categories,
        viewsWithoutCategory
      }
    } = this.state;
    return viewsWithoutCategory.length + categories.reduce((sum, category) => sum + category.views.length, 0);
  }
  get totalFilteredViewsAndCategories() {
    const {
      filteredCategoriesWithViews: {
        categories,
        viewsWithoutCategory
      }
    } = this.state;
    return viewsWithoutCategory.length + categories.length + categories.reduce((sum, category) => sum + category.views.length, 0);
  }
  get totalFilteredCategories() {
    const {
      filteredCategoriesWithViews: {
        categories
      }
    } = this.state;
    return categories.length;
  }
  init() {
    const {
      views,
      events
    } = this;
    const {
      autoCompleteReadonlyState
    } = views;
    const disposers = [(0, _util.addEventListener)(views.events, 'afterSelectView', () => {
      events.emit('afterSelectView');
    }), (0, _util.addEventListener)(autoCompleteReadonlyState.events, 'blur', () => {
      events.emit('blur');
    })];
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.ViewsLocalSearchBIReporter = ViewsLocalSearchBIReporter;
//# sourceMappingURL=ViewsLocalSearchBIReporter.js.map