#ifndef HUSH_LAYOUT_CACHE_H
#define HUSH_LAYOUT_CACHE_H

/* A rendered visual line, produced fresh every frame while building the Clay tree.
   Used the *following* frame to resolve clicks and up/down cursor movement against
   real, already-computed Clay element positions (queried via Clay_GetElementData). */
typedef struct {
    int blockIndex;
    int lineInBlock;
    int srcStart, srcEnd; /* byte range in the block's raw text covered by this line */
    int globalLineIndex;  /* identifies this line's Clay element: CLAY_IDI("Line", globalLineIndex) */
    int segStart, segEnd; /* range into LayoutCache.segs covering this line's segments */
} CachedLine;

typedef struct {
    int lineIndex; /* index into LayoutCache.lines */
    int srcStart, srcEnd;
    int globalSegIndex; /* identifies this segment's Clay element: CLAY_IDI("Seg", globalSegIndex) */
    int fontId;
    int fontSize;
} CachedSegment;

typedef struct {
    CachedLine *lines;
    int lineCount, lineCap;
    CachedSegment *segs;
    int segCount, segCap;
} LayoutCache;

void layout_cache_init(LayoutCache *c);
void layout_cache_reset(LayoutCache *c); /* clears contents but keeps capacity, for reuse each frame */
void layout_cache_free(LayoutCache *c);

int layout_cache_add_line(LayoutCache *c, int blockIndex, int lineInBlock, int srcStart, int srcEnd);
int layout_cache_add_seg(LayoutCache *c, int lineIndex, int srcStart, int srcEnd, int fontId, int fontSize);
void layout_cache_close_line(LayoutCache *c, int lineIndex); /* call after adding all segs for a line, to set segEnd */

#endif
