/**
 * Web platform idle detection using Page Visibility API.
 * Provides a simplified version of the Electron idle API.
 */
let idleCallback = null;
let lastActiveTime = Date.now();

function handleVisibilityChange() {
  if (!idleCallback) return;
  if (document.hidden) {
    idleCallback({ isIdle: true });
  } else {
    lastActiveTime = Date.now();
    idleCallback({ isIdle: false });
  }
}

export default {
  getSystemIdleTime() {
    // Return seconds since last activity (approximation using visibility)
    if (document.hidden) {
      return Math.floor((Date.now() - lastActiveTime) / 1000);
    }
    return 0;
  },

  onIdleStateChanged(callback) {
    idleCallback = callback;
    document.addEventListener('visibilitychange', handleVisibilityChange);
  },

  removeIdleStateListener() {
    idleCallback = null;
    document.removeEventListener('visibilitychange', handleVisibilityChange);
  },
};
