import * as Sentry from "@sentry/nextjs";
import { environment } from "../environment";

export enum Key {
  LOGOUT = "supabase-logout",
  WEBSOCKET_DISCONNECT_INTENT = "websocket-disconnect-intent",
  COPIED_FLOW_DATA = "copied-flow-data",
  SHEPHERD_TOUR = "shepherd-tour",
  WALLET_LAST_SEEN_CREDITS = "wallet-last-seen-credits",
  LIBRARY_AGENTS_CACHE = "library-agents-cache",
  CHAT_SESSION_ID = "chat_session_id",
  COOKIE_CONSENT = "autogpt_cookie_consent",
  AI_AGENT_SAFETY_POPUP_SHOWN = "ai-agent-safety-popup-shown",
  COPILOT_SOUND_ENABLED = "copilot-sound-enabled",
  COPILOT_NOTIFICATIONS_ENABLED = "copilot-notifications-enabled",
  COPILOT_NOTIFICATION_BANNER_DISMISSED = "copilot-notification-banner-dismissed",
  COPILOT_NOTIFICATION_DIALOG_DISMISSED = "copilot-notification-dialog-dismissed",
  COPILOT_ARTIFACT_PANEL_WIDTH = "copilot-artifact-panel-width",
  COPILOT_MODE = "copilot-mode",
  COPILOT_MODEL = "copilot-model",
  COPILOT_COMPLETED_SESSIONS = "copilot-completed-sessions",
  PUSH_SUBSCRIPTION_REGISTERED = "push-subscription-registered",
  COPILOT_DRY_RUN = "copilot-dry-run",
  TOP_UP_MODAL_LAST_SHOWN = "top-up-modal-last-shown",
  LOW_CREDIT_BANNER_DISMISSED = "low-credit-banner-dismissed",
  BUILDER_MOBILE_WARNING_SUPPRESSED = "builder-mobile-warning-suppressed",
}

function get(key: Key) {
  if (environment.isServerSide()) {
    Sentry.captureException(new Error("Local storage is not available"));
    return;
  }
  try {
    return window.localStorage.getItem(key);
  } catch {
    // Fine, just return undefined not always items will be set on local storage
    return;
  }
}

function set(key: Key, value: string) {
  if (environment.isServerSide()) {
    Sentry.captureException(new Error("Local storage is not available"));
    return;
  }
  return window.localStorage.setItem(key, value);
}

function clean(key: Key) {
  if (environment.isServerSide()) {
    Sentry.captureException(new Error("Local storage is not available"));
    return;
  }
  return window.localStorage.removeItem(key);
}

export const storage = {
  clean,
  get,
  set,
};
