"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.SuggestionsState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _http = require("@wix/bex-utils/@wix/ambassador-dealer-v1-serving-offer/http");
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
var _SuggestionsBIReporter = require("./SuggestionsBIReporter");
class SuggestionsState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "placementId", void 0);
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "fetchOffersTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "reportBI", void 0);
    (0, _defineProperty2.default)(this, "bi", void 0);
    (0, _defineProperty2.default)(this, "httpClient", void 0);
    (0, _defineProperty2.default)(this, "init", () => {
      const startTime = performance.now();
      this.bi.loadStart();
      this.fetchOffersTask.runOnce(async () => {
        const offers = await this._fetchOffers();
        this.bi.loadEnd(startTime);
        return offers;
      });
    });
    (0, _defineProperty2.default)(this, "_fetchOffers", async () => {
      const {
        placementId,
        httpClient
      } = this;
      const {
        data
      } = await httpClient.request((0, _http.listOffers)({
        realEstateId: placementId
      }));
      return data.offers ?? [];
    });
    (0, _defineProperty2.default)(this, "onOfferViewed", offerId => {
      this.bi.reportOfferViewed(offerId);
    });
    (0, _defineProperty2.default)(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.SuggestionsBIReporter({
      reportBI: this.reportBI,
      realEstateId: this.placementId,
      httpClient: this.httpClient
    });
    (0, _mobx.makeObservable)(this, {
      offers: _mobx.computed,
      isFetching: _mobx.computed,
      hasOffers: _mobx.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;
  }
}
exports.SuggestionsState = SuggestionsState;
//# sourceMappingURL=SuggestionsState.js.map