import ServiceManagement

/// Registers / unregisters the app as a login item using the
/// modern SMAppService API (macOS 13+).
enum LaunchAtLoginHelper {

    static func setEnabled(_ enabled: Bool) {
        let service = SMAppService.mainApp
        do {
            if enabled {
                try service.register()
            } else {
                try service.unregister()
            }
        } catch {
            print("[LaunchAtLogin] Failed to \(enabled ? "register" : "unregister"): \(error)")
        }
    }

    /// Reads the current registration state from the system.
    static var isEnabled: Bool {
        SMAppService.mainApp.status == .enabled
    }
}
