/* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */

import WebStrategy from './WebStrategy';
import EventAction from '../EventAction';

import { Provider, UserProfile } from '../types';
import { makeTrustworthy } from '../util';
import { onLoginOnboardingReasons, onLoginTransformProfile } from '../transforms/transforms.web';

export default class WebClevertapStrategy extends WebStrategy implements Provider {
  // eslint-disable-next-line no-useless-constructor
  constructor(clevertap: any, debug: boolean) {
    super(clevertap, debug);
  }

  addEvent(eventName: string, payload: Record<string, any> = {}): void {
    if (!payload) {
      // eslint-disable-next-line no-param-reassign
      payload = {};
    }

    // eslint-disable-next-line no-param-reassign
    payload.channel = 'Web';

    if (window && window.location.href) {
      const ref = new URLSearchParams(window.location.href.split('?')[1]).get('ref');
      // eslint-disable-next-line no-param-reassign
      payload.ref = ref;
    }

    this.provider.event.push(eventName, payload);
    if (this.debug) console.log('Event send to Clevertap: ', eventName, payload);
  }

  onUserLogin(payload: any): void {
    const { authUser } = payload;
    const userProfile: UserProfile = onLoginTransformProfile(authUser);
    if (this.debug) console.log('Profile send: ', userProfile);
    if (!userProfile) return;
    // same as clevertap.onUserLogin.push on https://developer.clevertap.com/docs/concepts-user-profiles
    this.provider.onUserLogin.push({ Site: makeTrustworthy(userProfile) });
  }

  onUserSelectReasons(payload: any): void {
    const { user } = payload;
    const userProfile: UserProfile = onLoginOnboardingReasons(user);
    if (this.debug) console.log('Profile send: ', userProfile);
    if (!userProfile) return;
    // same as clevertap.onUserLogin.push on https://developer.clevertap.com/docs/concepts-user-profiles
    this.provider.onUserLogin.push({ Site: makeTrustworthy(userProfile) });
  }
}
