// Autostart adapter — replaces Tauri's `tauri-plugin-autostart`. Uses
// Electron's built-in `app.setLoginItemSettings()` / `app.getLoginItemSettings()`,
// which manages the OS login-items mechanism on Windows (HKCU registry
// Run key), macOS (LaunchAgent), and Linux (.desktop entry).

import { app, ipcMain } from 'electron';

import { CHANNELS } from '../ipc-types';

export function register(): void {
  ipcMain.handle(CHANNELS.AUTOSTART_IS_ENABLED, () => {
    try {
      const settings = app.getLoginItemSettings();
      return settings.openAtLogin;
    } catch (err: unknown) {
      console.warn('autostart get failed', err);
      return false;
    }
  });

  ipcMain.handle(CHANNELS.AUTOSTART_SET, (_evt, enabled: boolean) => {
    try {
      app.setLoginItemSettings({ openAtLogin: !!enabled });
    } catch (err: unknown) {
      console.warn('autostart set failed', err);
      throw err;
    }
  });
}
