import { ReportBI, WixPatternsContainerParams } from '@wix/bex-core';
import { loadEnd, loadStart } from '@wix/bex-core/bi';
import { reportEvent } from '@wix/bex-utils/@wix/ambassador-dealer-v1-offer-event/http';
import { EventType } from '@wix/bex-utils/@wix/ambassador-dealer-v1-offer-event/types';

export interface SuggestionsBIReporterParams {
  reportBI: ReportBI;
  realEstateId: string;
  httpClient: WixPatternsContainerParams['httpClient'];
}

export class SuggestionsBIReporter {
  readonly reportBI: ReportBI;
  readonly httpClient: WixPatternsContainerParams['httpClient'];
  readonly realEstateId: string;

  constructor(params: SuggestionsBIReporterParams) {
    this.reportBI = params.reportBI;
    this.realEstateId = params.realEstateId;
    this.httpClient = params.httpClient;
  }

  loadStart() {
    this.reportBI(loadStart({}));
  }

  loadEnd(startTime: DOMHighResTimeStamp) {
    this.reportBI(
      loadEnd({ loadingTime: Math.round(performance.now() - startTime) }),
    );
  }

  reportOfferViewed(offerId: string) {
    this.httpClient.request(
      reportEvent({
        event: {
          type: EventType.VIEW,
          offerId,
          realEstateId: this.realEstateId,
        },
      }),
    );
  }

  reportOfferClicked(offerId: string, targetUrl?: string) {
    this.httpClient.request(
      reportEvent({
        event: {
          type: EventType.MAIN_CTA_CLICK,
          offerId,
          realEstateId: this.realEstateId,
          mainCtaClick: {
            targetUrl: targetUrl ?? '',
          },
        },
      }),
    );
  }
}
