#pragma once
// Spec: INV-posts 2.3 (ReplyAnchor, activeReply, replyAttachments, editingCommentId, pendingEditQuote), 1.4 (quote
// insertion), 3.1 (feed cards share the same rules). The @State that SwiftUI keeps per screen and per CommentView:
// which composer is open, the unsubmitted attachments, per comment edit state and the open editor objects.
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include "screens/comments/CommentEditor.hpp"
#include "model/UploadResponse.hpp"
namespace dp {
class DevPlaceApi;
class CommentsSession {
public:
static constexpr const char* kPostAnchor = "@post";
static constexpr const char* kBottomAnchor = "@bottom";
struct RowState {
bool isEditing = false;
std::string editedText;
float contentHeight = 0;
};
CommentsSession();
~CommentsSession();
// ---- reply composer (one at a time) ----
const std::optional<std::string>& activeReply() const { return activeReply_; }
bool isReplying(const std::string& anchor) const { return activeReply_ && *activeReply_ == anchor; }
// beginReply(anchor): drop unsubmitted attachments, open the composer at `anchor`.
void beginReply(const std::string& anchor);
// cancelReply(): drop unsubmitted attachments, close.
void cancelReply();
// Submit path: attachments are handed to the caller, the composer closes. Returns the attachments.
std::vector<UploadResponse> takeAttachmentsAndClose();
const std::vector<UploadResponse>& attachments() const { return attachments_; }
void setAttachments(std::vector<UploadResponse> list);
// DELETE uploads/delete/<uid> for each unsubmitted attachment, failures only logged.
void discardAttachments();
// Increments on every beginReply so the composer fade-in restarts.
int epoch() const { return epoch_; }
// ---- comment editing ----
const std::optional<std::string>& editingCommentId() const { return editingCommentId_; }
RowState& row(const std::string& commentId);
void beginEditing(const std::string& commentId, const std::string& content);
void endEditing(const std::string& commentId);
// Takes the quote waiting for the edit editor (pendingEditQuote consumption).
std::optional<std::string> takePendingEditQuote();
// ---- quote composer (FeedView.insertQuote, PostView.insertQuote) ----
bool isComposing() const { return activeReply_.has_value() || editingCommentId_.has_value(); }
void insertQuote(const std::string& quote);
// ---- editors: created on demand, dropped when a frame did not ask for them ----
CommentEditor& editor(const std::string& key);
// Largest mention suggestion list height among the open editors.
float mentionListHeight() const;
void beginFrame();
void endFrame();
// Tests and shutdown.
void reset();
private:
std::optional<std::string> activeReply_;
std::optional<std::string> editingCommentId_;
std::optional<std::string> pendingEditQuote_;
std::vector<UploadResponse> attachments_;
std::map<std::string, RowState> rows_;
std::map<std::string, std::unique_ptr<CommentEditor>> editors_;
std::vector<std::unique_ptr<CommentEditor>> retired_;
std::set<std::string> touched_;
int epoch_ = 0;
};
} // namespace dp