#pragma once

#include <cstdint>
#include <string>

namespace torlinkc {

// "12.3 MB" / "0 B" style formatting. Ported from util/format.ts::formatBytes
// (reused here for both byte counts and byte/sec rates, same as the console
// harness already did).
std::string formatBytes(double bytes);

// "9981" / "12k" / "1.4m" style abbreviation for seeder/leecher counts.
// Ported from util/format.ts::formatCount.
std::string formatCount(double n);

// Parses a human-readable size like "1.2 GiB" or "700 MB" (as scraped from
// x1337/nyaa's HTML/RSS) back into bytes. Ported from
// util/format.ts::parseSize.
std::int64_t parseSize(const std::string& s);

// Strips C0/DEL/C1 control and escape-capable code points from a UTF-8
// string. UTF-8-sequence-aware: a multi-byte character's continuation bytes
// (0x80-0xBF) are never inspected on their own, only whichever single byte
// starts a sequence -- otherwise a continuation byte landing in 0x80-0x9F
// (common; that's most of the range) gets deleted as a "C1 control",
// corrupting the character. Ported from util/format.ts::stripControl:
// torrent names and other fields
// pulled from scraped, untrusted network responses get rendered verbatim in
// the TUI, so a hijacked or malicious source must not be able to smuggle an
// OSC/CSI escape sequence (e.g. an OSC 52 clipboard write) to the terminal
// through them.
std::string stripControl(const std::string& s);

// Truncates to at most `maxWidth` UTF-8 codepoints ("glyphs" -- never splits
// a multi-byte character), replacing the last one with an ellipsis when the
// string is longer. Ported from util/format.ts::truncate, counting UTF-8
// codepoints instead of UTF-16 code units (torrent names routinely contain
// non-ASCII characters).
std::string truncate(const std::string& s, int maxWidth);

}  // namespace torlinkc
