<!-- source-hash: 418321f518535eef27f0004848e1e1b3 -->
A browser/Tauri-only NATS client factory for WebSocket-based NATS connections (`nats.ws`), providing typed pub/sub, request/reply, JetStream ordered consumer subscriptions, and connection lifecycle management.

## Key Components

### Interfaces & Types
| Export | Description |
|--------|-------------|
| `NatsClient` | Core client interface with all pub/sub/request/JetStream methods |
| `NatsClientOptions` | Connection config: servers, auth, reconnect, exponential backoff, ping |
| `NatsSubscribeOptions` | Per-subscription options: queue group, max messages, abort signal |
| `NatsPublishOptions` / `NatsRequestOptions` | Optional headers and timeout per operation |
| `JetStreamOrderedSubscribeOptions` | JetStream ephemeral consumer config: stream, filter, deliver policy, start sequence |
| `NatsStatus` / `NatsStatusEvent` | Connection state machine: `connecting`, `connected`, `disconnected`, `reconnecting`, `closed`, `error` |

### Factory Function
- **`createNatsClient(options)`** — Returns a `NatsClient` instance. Lazily imports `nats.ws` (browser-safe). All methods are lazy-connected and browser-only (throws if `window` is undefined).

### Internal Helpers
- **`createExponentialBackoffHandler`** — Configurable exponential backoff with optional crypto jitter for reconnect delays.
- **`toNatsHeaders`** — Converts `Record<string, string>` or native `NatsHeaders` into a `nats.ws` headers object.
- **`mapNatsTypeToStatus`** — Normalizes raw `nats.ws` status events to the typed `NatsStatus` union.

## Usage Example

```typescript
import { createNatsClient } from './nats'

const client = createNatsClient({
  servers: 'wss://nats.example.com:443',
  name: 'flamingo-ui',
  token: 'my-token',
  exponentialBackoff: {
    initialDelayMs: 500,
    maxDelayMs: 15_000,
    multiplier: 2,
    jitter: true,
  },
})

// Listen for connection status changes
const off = client.onStatus((event) => {
  console.log('NATS status:', event.status)
})

await client.connect()

// Typed JSON publish
client.publishJson('telemetry.events', { deviceId: 'abc', value: 42 })

// Typed JSON request/reply
const response = await client.requestJson<{ ok: boolean }>(
  'devices.query',
  { id: 'abc' },
  { timeoutMs: 3000 },
)

// JetStream ordered consumer (live-tail)
const handle = await client.subscribeJetStreamOrdered(
  (msg) => console.log('JetStream msg:', msg.seq),
  {
    streamName: 'EVENTS',
    filterSubject: 'events.>',
    deliverPolicy: 'new',
  },
)

// Cleanup
handle.unsubscribe()
off()
await client.close()
```