function storageKey(userEmail) {
  return `email-unlimit-mailbox-tokens:${userEmail || "anonymous"}`;
}

function readTokens(userEmail) {
  try {
    return JSON.parse(localStorage.getItem(storageKey(userEmail))) || {};
  } catch {
    return {};
  }
}

export function getMailboxToken(userEmail, mailboxId) {
  return readTokens(userEmail)[mailboxId] || null;
}

export function setMailboxToken(userEmail, mailboxId, token) {
  const tokens = readTokens(userEmail);
  tokens[mailboxId] = token;
  localStorage.setItem(storageKey(userEmail), JSON.stringify(tokens));
}

export function removeMailboxToken(userEmail, mailboxId) {
  const tokens = readTokens(userEmail);
  delete tokens[mailboxId];
  localStorage.setItem(storageKey(userEmail), JSON.stringify(tokens));
}
