import { UserType } from '@/modules/@common/user/types';

import { setUserDataAction } from '../redux/slices';
import { LanguageType, setVideoLangs } from '@/modules/@common/helpers/videoLangs';
import { setIAB } from '@/modules/@common/iab/helpers';
import { setImpersonated, setToken } from '@/modules/@common/token/helpers';

import store from '@/modules/@common/store';

type AuthDataType = {
  user: {
    iab: {
      data: string;
    };
    videoLangs: LanguageType[];
    user: UserType;
  };
  token: string;
  impersonated: boolean;
};

async function initUser(authData: AuthDataType) {
  const {
    iab: { data: iab },
    videoLangs,
  } = authData.user;

  setIAB(iab);
  setVideoLangs(videoLangs);

  store.current.dispatch(setUserDataAction(authData.user.user));

  setToken(authData.token);
  setImpersonated(authData.impersonated ? '1' : '');
}

export default initUser;
