+// WhatsApp is the link of the daemon to the WhatsApp account of the user, as a
+// linked device. The session database of the library in dir holds the keys of
+// the device. Run keeps a linked device connected, and the library connects
+// again by itself after a network failure.
+//
+// The commands pair and unlink go to Run as requests, and Run runs one at a
+// time. A pairing runs in its own goroutine until its code expires, and a new
+// pairing ends the one before. Its phone number and code go only to the answer
+// of the client that asked. A logout from the phone comes as an event of the
+// library, and Run deletes the session. The mutex protects the session, the
+// client, and the state of the connection, which the event handler of the
+// library changes from its own goroutines. The sessionMutex serializes session
+// creation, close, removal, and snapshots. It is held before mutex, and stays
+// held when removal releases mutex for client callbacks.
+//
+// ctx is the caller context until Run sets its context for library callbacks.
+// The mutex protects it, so callbacks follow daemon cancellation.
+//
+// The inbox puts messages into batches and logs them, and groups holds the
+// names of the groups. Both get a WhatsAppLink from link for each call, so they
+// use the current device and client.
+//
+// The library logs nothing, because its log lines hold the ID of the own
+// account, which is the phone number of the user. The own number and the own
+// LID never go to the log of the agent or to the log of the daemon: redact
+// removes them from the errors, and a batch shows the messages of the user as
+// You and a mention of the user as @you. The IDs of the other chats go to the
+// log, and those of private chats hold phone numbers, because the agent needs
+// them to send.
+type WhatsApp struct {
+ ctx context.Context
+ dir string
+ clock func() time.Time
+ logger *slog.Logger
+ newClient func(device *store.Device, handle func(event any) bool) WhatsAppClient
+ requests chan WhatsAppRequest
+ logouts chan struct{}
+ state *WhatsAppState
+ groups *WhatsAppGroups
+ inbox *WhatsAppInbox
+ routes *Routes
+
+ sessionMutex sync.Mutex
+ mutex sync.Mutex
+ container *sqlstore.Container
+ device *store.Device
+ client WhatsAppClient
+ linked bool
+ own []string
+ notify func(Event)
+ connected bool
+ retrying bool
+ connectedAt time.Time
+ disconnectedAt time.Time
+ disconnections int
+ noticed bool
+ timer *time.Timer
+ closed bool
+}
+
+// WhatsAppLink is a copy of the link of the device at one time, for the parts
+// of WhatsApp that do not own it: the groups, the batches, find, and send.
+type WhatsAppLink struct {
+ linked bool
+ device *store.Device
+ client WhatsAppClient
+ connected bool
+ own []string
+}
+
+// WhatsAppRequest is a pair or unlink command for Run, with the function that
+// gives its answers to the client that asked. A pairing has the phone number.
+type WhatsAppRequest struct {
+ phone string
+ unlink bool
+ answer func(text string)
+}
+