/* eslint-disable @typescript-eslint/no-explicit-any */
import MobileStrategy from './MobileStrategy';
import { makeTrustworthy } from '../util';

import { immutableCustomerIntoUserProfile } from '../transforms/transforms.mobile';

import { Provider, UserProfile, Location } from '../types';

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

  private profileSet(user: UserProfile): void {
    if (!user) return;
    this.provider.profileSet(makeTrustworthy(user));
  }

  private profileSetMultiValue(user: UserProfile): void {
    Object.keys(user).forEach((key) => {
      const value = user[key as keyof UserProfile];
      if (Array.isArray(value) && value.length > 0) {
        this.provider.profileSetMultiValuesForKey(value, key);
      }
    });
  }

  private setLocation(location: Location): void {
    this.provider.setLocation(location.latitude, location.longitude);
  }

  private setFCMtoken(token: string): void {
    this.provider.setPushToken(token, 'FCM');
  }

  onUserLogin(payload: any): void {
    const { customer } = payload;
    const userProfile = immutableCustomerIntoUserProfile(customer);
    if (!userProfile) return;
    this.provider.onUserLogin(makeTrustworthy(userProfile));
  }

  setUserLocation(payload: any): void {
    const { location } = payload;
    this.setLocation(location);
  }

  updateFCMtoken(payload: any): void {
    const { token } = payload;
    if (!token) return;
    this.setFCMtoken(token);
  }
}
