
function setToken (state, token) {
  state.token = token;
  if (token) {
    localStorage?.setItem("jwt", token);
  } else {
    localStorage?.removeItem("jwt");
  }
}

function setUser (state, user) {
  state.user = user;
}

function setPreferences (state, preferences) {
  state.preferences = preferences;
}

function setCookie (state, opts = {}) {
  const name = opts.name ?? "JWT";
  const value = opts.value ?? "";
  const expires = opts.expires ? (new Date(opts.expires)) : (new Date(0));
  const path = opts.path ?? "/";
  const sameSite = opts.sameSite ?? "Lax";

  document.cookie = `${name}=${value};path=${path};SameSite=${sameSite};expires=${expires.toUTCString()}`;
}

function clearCookie (state, name) {
  setCookie(state, {name});
}

export default { setToken, setUser, setPreferences, setCookie, clearCookie };
