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

export const setTokens = (authResponse: any) => {
  localStorage.setItem('user', JSON.stringify(authResponse.user))
  localStorage.setItem('token', JSON.stringify(authResponse.user.token))
}

export const removeTokens = () => {
  localStorage.removeItem('user')
  localStorage.removeItem('token')
}

export const checkExpToken = () => {
  if (localStorage.getItem('token')) {
    return true
  } else {
    return false
  }
}

export const checkPermissionAdmin = (permission:string) => {
  const user = localStorage.getItem('user')
  if (user) {
    const parsedUser = JSON.parse(user)
    let permissions = parsedUser.user.permission.split(',')
    
    if (permissions.find((p:string)=>permission.includes(p))) {
      return true
    } else {
      return false
    }
  } else {
    return false
  }
}

export const getUser = () => localStorage.getItem('user') as string
export const getAccessToken = () =>
  localStorage.getItem('token')?.slice(1, -1) as string
