"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ToolbarBIReporter = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _bi = require("@wix/bex-core/bi");
var _mobx = require("mobx");
const withoutDefaults = (0, _bexCore.withoutIrrelevant)();
class ToolbarBIReporter {
  constructor(params) {
    (0, _defineProperty2.default)(this, "collection", void 0);
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "reportBi", void 0);
    (0, _defineProperty2.default)(this, "onAddItemClick", origin => {
      this.reportBi((0, _bi.newItemCreationStart)({
        origin: origin ?? 'Card CTA'
      }));
    });
    (0, _defineProperty2.default)(this, "onPrimaryActionButtonClick", () => {
      this.reportBi((0, _bi.cairoSectionCtaClicked)({
        ctaName: 'Primary action button',
        ctaType: 'Main',
        location: 'Collection Toolbar'
      }));
    });
    (0, _defineProperty2.default)(this, "loadStart", () => {
      var _this$toolbar$collect;
      this.reportBi(withoutDefaults(_bi.loadStart)({
        ...this.toolbar._commonDynamicBiParams(),
        url: typeof window !== 'undefined' ? window.location.href : '',
        featuresAvailability: JSON.stringify({
          isShowingSelection: this.toolbar.selectionProps.value.showSelection ?? false,
          isUsingApplyInFiltersPanel: !!this.toolbar.AddApplyFiltersButton,
          isUsingCustomColumns: !!this.toolbar.customColumnsProps,
          isUsingOptimisticActions: !!this.collection._optimisticActions,
          isUsingViews: !!this.toolbar.viewsProps,
          isUsingSorting: this.toolbar.columns.some(col => col.sortable === true),
          isUsingLoadSelectedColumns: this.collection.query.refreshOnColumnsChange ?? false,
          isUsingDataExtension: !!((_this$toolbar$collect = this.toolbar.collectionDataExtension) != null && _this$toolbar$collect.dataExtension),
          ...this.toolbar.featuresAvailability
        }),
        additionalFeaturesAvailability: JSON.stringify({
          isUsingActionsCell: !!this.toolbar.actionCell.value,
          ...this.toolbar.additionalFeaturesAvailability
        })
      }));
    });
    (0, _defineProperty2.default)(this, "loadEnd", startTime => {
      this.reportBi(withoutDefaults(_bi.loadEnd)({
        ...this.toolbar._commonDynamicBiParams(),
        url: typeof window !== 'undefined' ? window.location.href : '',
        route: typeof window !== 'undefined' ? window.location.pathname : '',
        loadingTime: Math.round(performance.now() - startTime)
      }));
    });
    (0, _defineProperty2.default)(this, "newItemCreationEnd", items => {
      const {
        itemKey
      } = this.collection;
      this.reportBi((0, _bi.newItemCreationEnd)({
        itemId: JSON.stringify(items.map(itemKey)),
        resultType: 'success'
      }));
    });
    this.collection = params.collection;
    this.toolbar = params.toolbar;
    this.reportBi = params.reportBi;
  }
  ctaClicked({
    location,
    ctaIndex,
    ctaName,
    moreActionsIndex,
    numCtas,
    itemId,
    itemIndex,
    componentId,
    appId
  }) {
    this.reportBi(withoutDefaults(_bi.cairoCtaClicked)({
      ctaIndex,
      ctaName,
      location,
      numCtas,
      moreActionsIndex,
      numItems: this.collection.bulkSelect.selectedCountOrTotal,
      isSelectAll: this.collection.bulkSelect.allSelected,
      itemId,
      itemIndex,
      url: typeof window !== 'undefined' ? window.location.href : ''
    }));
    if (componentId) {
      this.reportBi(withoutDefaults(_bi.cairoTpaSCtaClicked)({
        ctaIndex,
        ctaName,
        location,
        numCtas,
        appId,
        tpaComponentId: componentId
      }));
    }
  }
  itemSelectionToggledStarted({
    itemId,
    clickType
  }) {
    const {
      reportBi,
      collection
    } = this;
    const {
      bulkSelect,
      query: {
        search
      }
    } = collection;
    const numItemsBefore = bulkSelect.selectedCountOrTotal;
    return (0, _mobx.action)(() => {
      reportBi(withoutDefaults(_bi.cairoItemSelectionToggled)({
        isSelectAll: false,
        isFromSearch: !search.isEmpty,
        searchQuery: search.value,
        itemId,
        clickType,
        numItemsBefore,
        numItemsAfter: bulkSelect.selectedCountOrTotal
      }));
    });
  }
  selectAllToggledStart({
    location
  }) {
    const {
      reportBi,
      toolbar: {
        bulk
      },
      collection: {
        bulkSelect,
        query: {
          search
        }
      }
    } = this;
    const numItemsBefore = bulkSelect.selectedCountOrTotal;
    return (0, _mobx.action)(() => {
      const {
        hasClickableRows
      } = bulk;
      reportBi(withoutDefaults(_bi.cairoItemSelectionToggled)({
        url: typeof window !== 'undefined' ? window.location.href : '',
        isSelectAll: true,
        isFromSearch: !search.isEmpty,
        searchQuery: search.value,
        itemId: hasClickableRows ? undefined : 'All',
        clickType: hasClickableRows ? 'Deselection' : 'Selection',
        numItemsBefore,
        numItemsAfter: bulkSelect.selectedCountOrTotal,
        location,
        timeFromAction: Math.round(performance.now() - bulk._selectionStartTime)
      }));
    });
  }
}
exports.ToolbarBIReporter = ToolbarBIReporter;
//# sourceMappingURL=ToolbarBIReporter.js.map