// Browser Compatibility Layer
// Provides unified API access across Chrome and Firefox
// In Firefox, chrome.* APIs are aliased from browser.* but some features differ

export const IS_FIREFOX = typeof browser !== 'undefined' && typeof InstallTrigger !== 'undefined';
export const IS_CHROME = !IS_FIREFOX && typeof chrome !== 'undefined';

/**
 * Check if an API is available before using it
 */
export function hasAPI(path) {
  try {
    const parts = path.split('.');
    let obj = chrome;
    for (const part of parts) {
      obj = obj[part];
      if (obj === undefined) return false;
    }
    return true;
  } catch {
    return false;
  }
}

// Feature flags based on browser capabilities
export const FEATURES = {
  enterpriseDeviceAttributes: hasAPI('enterprise.deviceAttributes'),
  storageManaged: hasAPI('storage.managed'),
  storageSync: hasAPI('storage.sync'),
  webRequest: hasAPI('webRequest.onBeforeRequest'),
  downloadsOnDeterminingFilename: hasAPI('downloads.onDeterminingFilename'),
  declarativeNetRequest: hasAPI('declarativeNetRequest.updateDynamicRules'),
  externallyConnectable: IS_CHROME, // Firefox doesn't support externally_connectable
};
