"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionViewsStateBIReporter = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
var _bi = require("@wix/bex-core/bi");
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
class CollectionViewsStateBIReporter {
  constructor(params) {
    (0, _defineProperty2.default)(this, "collectionViewState", void 0);
    (0, _defineProperty2.default)(this, "reportBI", void 0);
    this.collectionViewState = params.collectionViewState;
    this.reportBI = params.reportBI;
  }
  init() {
    const {
      reportBI,
      collectionViewState
    } = this;
    const {
      emitter,
      table,
      views
    } = collectionViewState;
    const disposers = [(0, _bexCore.addEventListener)(views.events, 'beforeSelectView', (0, _mobx.action)(() => {
      var _views$currentView;
      const previousViewName = (_views$currentView = views.currentView) == null ? void 0 : _views$currentView.name;
      const previousFilters = JSON.stringify(table.collection.result.originQuery.filtersKey);
      const isFromSearchView = !!views.localSearchState.bi.events.listeners('afterSelectView').length;
      views.events.once('afterSelectView', ({
        view
      }) => {
        reportBI((0, _bexCore.withoutDefaults)(_bi.cairoViewSelected)({
          currentFilters: JSON.stringify(table.collection.result.originQuery.filtersKey),
          filteredListSize: table.collection.query.activeFiltersCount,
          newViewName: view.name,
          prevViewName: previousViewName,
          prevFilters: previousFilters,
          isPredefined: view.isPreset,
          isFromSearchView,
          isDefault: view.id === views.defaultViewId
        }));
      });
    })), (0, _bexCore.addEventListener)(emitter, 'openManageViewPopoverMenu', (0, _mobx.action)(() => {
      var _collectionViewState$;
      reportBI((0, _bexCore.withoutDefaults)(_bi.cairoFiltersPanelUsed)({
        isOpened: true,
        feature: 'Manage Views',
        origin: 'Manage Views'
      }));
      const isRedesignEnabled = (_collectionViewState$ = collectionViewState.table.container.internalExperiments) == null ? void 0 : _collectionViewState$.enabled('specs.cairo.ViewsRedesign');
      if (isRedesignEnabled) {
        var _views$currentView2;
        const viewId = (_views$currentView2 = views.currentView) == null ? void 0 : _views$currentView2.id;
        const {
          viewsWithoutCategory,
          categories
        } = views.localSearchState.categoriesWithViews;
        const flatViews = [...viewsWithoutCategory, ...categories.flatMap(c => c.views)];
        const viewIndex = viewId ? flatViews.findIndex(v => v.id === viewId) : undefined;
        reportBI((0, _bexCore.withoutDefaults)(_bi.cairoCtaClicked)({
          ctaName: 'more_actions',
          location: 'view_dropdown',
          itemId: viewId,
          itemIndex: viewIndex
        }));
      }
    }))];
    return () => {
      disposers.forEach(disposer => disposer());
    };
  }
  _getViewChanges(view1, view2) {
    var _view1$filters, _view2$filters;
    const {
      queryCompareHelper
    } = this.collectionViewState;
    const changes = [];
    if (!(0, _isEqual.default)(view1.sortDirections, view2.sortDirections)) {
      changes.push('Sorting');
    }
    if (queryCompareHelper.isColumnsChanged(view1.selectedColumns, view2.selectedColumns)) {
      changes.push('Custom Columns');
    }
    if (((_view1$filters = view1.filters) == null || (_view1$filters = _view1$filters.search) == null ? void 0 : _view1$filters.value) !== ((_view2$filters = view2.filters) == null || (_view2$filters = _view2$filters.search) == null ? void 0 : _view2$filters.value)) {
      changes.push('Search');
    }
    const {
      search: s1,
      ...filters1
    } = view1.filters ?? {};
    const {
      search: s2,
      ...filters2
    } = view2.filters ?? {};
    if (!(0, _isEqual.default)(filters1, filters2)) {
      changes.push('Filters');
    }
    return changes.join(',');
  }
  _saveViewBI(view, saveType, oldView) {
    var _table$collection$res;
    const {
      table
    } = this.collectionViewState;
    const selectedColumns = table.selectedColumns.value.filter(col => col.isSelected);
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoViewSaved)({
      ...table._commonDynamicBiParams(),
      saveType,
      prevName: oldView == null ? void 0 : oldView.name,
      newViewName: view.name,
      isPredefined: view.isPreset,
      changedAttributes: this._getViewChanges(view, oldView ?? {}),
      numColumnsInView: selectedColumns.length ?? 0,
      numFiltersInView: (_table$collection$res = table.collection.result.originQuery.search) != null && _table$collection$res.length ? table.collection.query.activeFiltersCount - 1 : table.collection.query.activeFiltersCount,
      searchQuery: table.collection.result.originQuery.search,
      viewFilters: JSON.stringify(table.collection.result.originQuery.nonPersistentFiltersKey),
      origin: 'MainToolbar',
      viewId: view.id,
      visibleColumnsOrder: selectedColumns.map(col => col.id).join(','),
      isDefault: view.id === this.collectionViewState.views.defaultViewId
    }));
  }
  _actionSelectedBI(actionIndex, actionName) {
    var _views$currentView3;
    const {
      table,
      views
    } = this.collectionViewState;
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoManageViewActionSelected)({
      ...table._commonDynamicBiParams(),
      actionName,
      currentView: (_views$currentView3 = views.currentView) == null ? void 0 : _views$currentView3.id,
      actionIndex,
      numActions: 5
    }));
  }
  saveViewChangesSelectedBI() {
    this._actionSelectedBI(1, 'view-save-changes');
  }
  saveViewChangesBI(view, oldView) {
    this._saveViewBI(view, 'save changes', oldView);
  }
  saveNewViewSelectedBI() {
    this._actionSelectedBI(2, 'view-save-new');
  }
  saveNewViewBI(view) {
    this._saveViewBI(view, 'save as new view');
  }
  deleteViewSelectedBI() {
    this._actionSelectedBI(5, 'view-delete');
  }
  deleteViewBI(view) {
    var _table$collection$res2;
    const {
      table,
      views
    } = this.collectionViewState;
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoViewDeleted)({
      numFiltersInView: (_table$collection$res2 = table.collection.result.originQuery.search) != null && _table$collection$res2.length ? table.collection.query.activeFiltersCount - 1 : table.collection.query.activeFiltersCount,
      viewFilters: JSON.stringify(view.filters),
      viewName: view.name,
      viewId: view.id,
      wasSelected: true,
      isDefault: view.id === views.defaultViewId
    }));
  }
  renameViewSelectedBI() {
    this._actionSelectedBI(3, 'view-rename');
  }
  renameViewBI(view, oldName) {
    this._saveViewBI(view, 'rename', {
      ...view,
      name: oldName
    });
  }
  setAsDefaultViewSelectedBI(isDefault) {
    this._actionSelectedBI(4, isDefault ? 'view-set-as-default' : 'view-unset-as-default');
  }
  setAsDefaultViewBI(isDefault, view) {
    this._saveViewBI(view, isDefault ? 'set-as-default' : 'unset-as-default', view);
  }
  undoActionBI({
    actionName,
    viewId,
    newName
  }) {
    const {
      views,
      queryCompareHelper
    } = this.collectionViewState;
    const view = views.getView(viewId);
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoUndoClicked)({
      actionName,
      viewId: view == null ? void 0 : view.id,
      viewName: view == null ? void 0 : view.name,
      viewNameApplied: newName ?? (view == null ? void 0 : view.name),
      changedAttributes: view && actionName === 'view-save-changes' ? this._getViewChanges(view, queryCompareHelper.currentViewBase ?? {}) : undefined
    }));
  }
  tryAgainBI({
    actionName
  }) {
    const {
      table
    } = this.collectionViewState;
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoTryAgainClicked)({
      actionName,
      isFromSearch: !!table.collection.result.originQuery.search,
      numOfWaitingUpdates: 1
    }));
  }
  errorBI({
    actionName,
    createdAt,
    viewId
  }) {
    const {
      table
    } = this.collectionViewState;
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.cairoViewsUnsuccessfulUpdateInServer)({
      actionName,
      errorType: 'Update Failed',
      timeFromEndAction: Date.now() - createdAt,
      numOfWaitingUpdates: 1,
      isFromSearch: !!table.collection.result.originQuery.search,
      viewId
    }));
  }
  clickOnViewDropdownBI() {
    this.reportBI((0, _bexCore.withoutDefaults)(_bi.wixPatternsClickOnViewDropdownSrc144Evid4)({}));
  }
}
exports.CollectionViewsStateBIReporter = CollectionViewsStateBIReporter;
//# sourceMappingURL=CollectionViewsStateBIReporter.js.map