//
// Utility functions for processing tree indentation with UTF-8 box drawing characters
//

#include "TreeIndentUtil.hpp"

namespace mcts::util {

namespace {
// Box drawing characters for tree visualization
constexpr const char* kBranch = "\xE2\x94\x9C";      // ├
constexpr const char* kCorner = "\xE2\x94\x94";      // └
constexpr const char* kVertical = "\xE2\x94\x82";    // │
constexpr const char* kHorizontal = "\xE2\x94\x80";  // ─
}  // namespace

std::string BuildTreeIndent(int indentLevel, bool isLastChild) {
    std::string indent;

    for (int i = 0; i < indentLevel; ++i) {
        if (i == indentLevel - 1) {
            indent += isLastChild ? kCorner : kBranch;
            indent += kHorizontal;
            indent += " ";
        } else {
            indent += "   ";
        }
    }

    return indent;
}

std::string ConvertBranchToContinuation(const std::string& indent) {
    std::string result = indent;

    const std::string replacement = std::string(kVertical) + "  ";

    // Replace ├ and └ with │
    size_t pos = 0;
    while ((pos = result.find(kBranch, pos)) != std::string::npos) {
        result.replace(pos, 3, replacement);  // UTF-8 chars are 3 bytes
        pos += replacement.size();
    }
    pos = 0;
    while ((pos = result.find(kCorner, pos)) != std::string::npos) {
        result.replace(pos, 3, replacement);
        pos += replacement.size();
    }

    return result;
}

}  // namespace mcts::util
