import { browser } from 'wxt/browser';

// Cookie
export const browserCookieGetAll = browser.cookies?.getAll ?? null;
export const browserCookieGet = browser.cookies?.get ?? null;
export const browserCookieRemove = browser.cookies?.remove ?? null;
export const browserCookieSet = browser.cookies?.set ?? null;

// Storage
export const browserLocalStorage = browser.storage?.local ?? null;
export const browserSyncStorage = browser.storage?.sync ?? null;

// Tab
export const browseTabCreate = browser.tabs?.create ?? null;
export const browseTabReload = browser.tabs?.reload ?? null;
export const browseTabQuery = browser.tabs?.query ?? null;
export const browseTabGet = browser.tabs?.get ?? null;
export const browseTabSendMessage = browser.tabs?.sendMessage ?? null;
export const browseTabRemove = browser.tabs?.remove ?? null;
export const browseTabOnUpdate = browser.tabs?.onUpdated ?? null;

// Runtime
export const browserOnMessage = browser.runtime?.onMessage ?? null;
export const browserSendMessage = browser.runtime?.sendMessage ?? null;
export const browserExtensionId = browser.runtime?.id ?? null;

// WebRequest
export const browserWebrequest = browser.webRequest ?? null;

// Notifications
export const browseNotificationCreate = browser.notifications?.create ?? null;

// Alarms
export const browserAlarms = browser.alarms ?? null;

/**
 * @returns {boolean}
 */
export const useOpenPopup = () => {
  if (!browser.extension?.getViews) {
    return false;
  }
  return browser.extension.getViews({ type: 'popup' }).some((view) => view === window);
};

export const isPopupEnv = useOpenPopup();
