import { STORAGE_KEY_KIMLIK_TOKEN, STORAGE_KEY_KIMLIK_USER_ID } from '@dvn/kimlik/api/kimlikApi';
import Cookies from 'js-cookie';
export class CookieStorage {
  constructor() {
    this.cookie = Cookies;
  }
  /**
   * Generates date pointed to given day addition. Uses
   * COOKIE_EXPIRATION_DAY_COUNT if no day is provided.
   * @param dayCount
   * @returns {Date}
   */
  static getExpires(dayCount = CookieStorage.COOKIE_EXPIRATION_DAY_COUNT) {
    const date = new Date();
    date.setTime(date.getTime() + (dayCount * 24 * 60 * 60 * 1000));
    return date;
  }
  /**
   * Saves cookie value.
   * @param key of the cookie.
   * @param value of the cookie.
   */
  save(key, value) {
    this.cookie.set(key, value, {
      path: CookieStorage.COOKIE_PATH,
      expires: CookieStorage.getExpires(),
    });
  }
  /**
   * Gets cookie value.
   * @param key of the cookie.
   */
  get(key) {
    return this.cookie.get(key);
  }
  /**
   * Removes the cookie value.
   * @param key
   */
  remove(key) {
    this.cookie.remove(key, { path: CookieStorage.COOKIE_PATH });
  }
  /**
   * Clears session related cookies.
   */
  clear() {
    this.remove(STORAGE_KEY_KIMLIK_TOKEN);
    this.remove(STORAGE_KEY_KIMLIK_USER_ID);
  }
  /**
   * Returns all values.
   */
  getAll() {
    return {
      jwt: this.get(STORAGE_KEY_KIMLIK_TOKEN),
      userId: this.get(STORAGE_KEY_KIMLIK_USER_ID),
    };
  }
}
CookieStorage.COOKIE_PATH = '/';
CookieStorage.COOKIE_EXPIRATION_DAY_COUNT = 7;
