import { listOffers } from '@wix/bex-utils/@wix/ambassador-dealer-v1-serving-offer/http';
import { computed, makeObservable } from 'mobx';
import { TaskState, } from '@wix/bex-core';
import { SuggestionsBIReporter } from './SuggestionsBIReporter';
export class SuggestionsState {
    constructor(params) {
        this.fetchOffersTask = new TaskState();
        this.init = () => {
            const startTime = performance.now();
            this.bi.loadStart();
            this.fetchOffersTask.runOnce(async () => {
                const offers = await this._fetchOffers();
                this.bi.loadEnd(startTime);
                return offers;
            });
        };
        this._fetchOffers = async () => {
            const { placementId, httpClient } = this;
            const { data } = await httpClient.request(listOffers({ realEstateId: placementId }));
            return data.offers ?? [];
        };
        this.onOfferViewed = (offerId) => {
            this.bi.reportOfferViewed(offerId);
        };
        this.onOfferClicked = (offerId, targetUrl) => {
            this.bi.reportOfferClicked(offerId, targetUrl);
        };
        this.placementId = params.placementId;
        this.container = params.container;
        this.httpClient = this.container.httpClient;
        this.reportBI = this.container.createBILogger({
            componentType: 'suggestionsWidget',
            route: window.location.pathname,
        });
        this.bi = new SuggestionsBIReporter({
            reportBI: this.reportBI,
            realEstateId: this.placementId,
            httpClient: this.httpClient,
        });
        makeObservable(this, {
            offers: computed,
            isFetching: computed,
            hasOffers: computed,
        });
    }
    get offers() {
        return this.fetchOffersTask.status.isSuccess
            ? this.fetchOffersTask.status.data
            : [];
    }
    get isFetching() {
        return this.fetchOffersTask.status.isLoading;
    }
    get hasOffers() {
        return this.offers.length > 0;
    }
}
//# sourceMappingURL=SuggestionsState.js.map