/* eslint-disable @typescript-eslint/no-explicit-any */
import EventAction from '../EventAction';
import { onLoginTransformProfile } from '../transforms/transforms.web';

import { Provider, UserProfile } from '../types';
import { makeTrustworthy } from '../util';
import WebStrategy from './WebStrategy';

export default class WebMixpanelStrategy extends WebStrategy implements Provider {
  // eslint-disable-next-line no-useless-constructor
  constructor(mixpanel: any, debug: boolean) {
    super(mixpanel, 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.track(eventName, payload);
    if (this.debug) console.log('Event send to MixPanel: ', 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;
    console.log('this.provider', this.provider);
    // same as clevertap.onUserLogin.push on https://developer.clevertap.com/docs/concepts-user-profiles
    this.provider.identify(userProfile.Uid);
    this.provider.people.set_once(userProfile);
  }
}
