import { withoutIrrelevant, } from '@wix/bex-core';
import { cairoCtaClicked, cairoItemSelectionToggled, cairoTpaSCtaClicked, newItemCreationStart, cairoSectionCtaClicked, loadStart, loadEnd, newItemCreationEnd, } from '@wix/bex-core/bi';
import { action } from 'mobx';
const withoutDefaults = withoutIrrelevant();
export class ToolbarBIReporter {
    constructor(params) {
        this.onAddItemClick = (origin) => {
            this.reportBi(newItemCreationStart({
                origin: origin ?? 'Card CTA',
            }));
        };
        this.onPrimaryActionButtonClick = () => {
            this.reportBi(cairoSectionCtaClicked({
                ctaName: 'Primary action button',
                ctaType: 'Main',
                location: 'Collection Toolbar',
            }));
        };
        this.loadStart = () => {
            this.reportBi(withoutDefaults(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.collectionDataExtension?.dataExtension,
                    ...this.toolbar.featuresAvailability,
                }),
                additionalFeaturesAvailability: JSON.stringify({
                    isUsingActionsCell: !!this.toolbar.actionCell.value,
                    ...this.toolbar.additionalFeaturesAvailability,
                }),
            }));
        };
        this.loadEnd = (startTime) => {
            this.reportBi(withoutDefaults(loadEnd)({
                ...this.toolbar._commonDynamicBiParams(),
                url: typeof window !== 'undefined' ? window.location.href : '',
                route: typeof window !== 'undefined' ? window.location.pathname : '',
                loadingTime: Math.round(performance.now() - startTime),
            }));
        };
        this.newItemCreationEnd = (items) => {
            const { itemKey } = this.collection;
            this.reportBi(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(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(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 action(() => {
            reportBi(withoutDefaults(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 action(() => {
            const { hasClickableRows } = bulk;
            reportBi(withoutDefaults(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),
            }));
        });
    }
}
//# sourceMappingURL=ToolbarBIReporter.js.map