import {
  DEFAULT_AUTHORIZATION_TOKEN_EXPIRATION,
  StorageKeys,
} from '../globalConstants';
import CryptoJS from "crypto-js";
const secretKey = import.meta.env.VITE_SECRET_KEY || 'prims@2024';

let tokenExpiration = DEFAULT_AUTHORIZATION_TOKEN_EXPIRATION;

export function setTokenExpiration(expires) {
  tokenExpiration = expires;
}

export function getToken() {
  //return Cookies.get(StorageKeys.AuthorizationToken);
  return localStorage.getItem(StorageKeys.AuthorizationToken)
}

export function setToken(token) {
  localStorage.setItem(StorageKeys.AuthorizationToken, token);
  // Cookies.set(StorageKeys.AuthorizationToken, token, {
  //   expires: new Date(new Date().getTime() + tokenExpiration),
  // });
}

export function removeToken() {
  localStorage.removeItem(StorageKeys.AuthorizationToken);
}

export function encryptPassword(password){
  if (!password) {
    return password;
  }
  const key = CryptoJS.enc.Utf8.parse(secretKey);
  const srcs = CryptoJS.enc.Utf8.parse(password);
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
  return encrypted.toString();
}

