#include <algorithm>
#include <cctype>

#include "target.h"
#include "log.h"
#include "show.h"
//#include "text.h"
#include "util.h"

void Target::add_transform(const std::string& original, const std::string& transformed)
{
    m_transforms.push_back({original, transformed});
}

void Target::add_transforms(const std::string& transforms)
{
    add_transforms(parse_transforms(transforms));
}

void Target::add_transforms(const string_pairs& transforms)
{
    for (const auto& [old_str, new_str] : transforms) {
        add_transform(old_str, new_str);
    }
}

// The typographic transform pass, run by Machine::apply() at final
// processing.  Per katom rather than over the joined result string, so
// that ^'...'^ literal content (katom_t::literal) is never transformed --
// verbatim text must show the characters the writer typed.  A transform
// source therefore cannot match across a katom boundary, which is the
// correct reading: two hyphens separated by a klammer application were
// separated by the writer and are not a dash.
void Target::transform(katom_list& katoms) const
{
    (void)K::log(3);
    std::for_each(
        katoms.begin(), katoms.end(),
        [this] (Katom& k) {
            if (k.m_type != katom_t::literal) {
                for (const auto& [a, b] : this->m_transforms) {
                    k.m_text = string_replace(k.m_text, a, b);
                }
            }
        });
}

std::vector<std::pair<std::string, std::string>>
parse_transforms(const std::string& transform_string)
{
    (void)K::log(3);
    if (trim(transform_string).empty()) return {};
    auto transforms = regex_split(transform_string, std::regex(R"(\s*\|\s*)"), true);
    std::vector<std::pair<std::string, std::string>> result;
    std::transform(transforms.begin(), transforms.end(), std::back_inserter(result),
                   [] (std::string s) {
                       strings_t v = word_split(s);
                       if (v.size() < 2) return std::pair(std::string{}, std::string{});
                       return std::pair(v[0], v[1]);
                   });
    // Remove empty pairs
    result.erase(std::remove_if(result.begin(), result.end(),
                 [](const auto& p) { return p.first.empty(); }), result.end());
    return result;
}

void Target::add_escapes(const std::string& escape_spec)
{
    auto words = word_split(escape_spec);
    for (size_t i = 0; i + 1 < words.size(); i += 2) {
        m_escapes.push_back({words[i], words[i+1]});
    }
}

void Target::add_resolves(const std::string& resolve_spec)
{
    auto words = word_split(resolve_spec);
    for (size_t i = 0; i + 1 < words.size(); i += 2) {
        m_resolves.push_back({words[i], words[i+1]});
    }
}

std::string Target::escape_marker(const std::string& ch)
{
    std::stringstream ss {};
    ss << "KTESC";
    for (unsigned char c : ch)
        ss << std::hex << std::setfill('0') << std::setw(4) << (int)c;
    ss << "KTESC";
    return ss.str();
}

std::string Target::escape_text(std::string text) const
{
    for (const auto& [ch, repl] : m_escapes) {
        text = string_replace(text, ch, escape_marker(ch));
    }
    return text;
}

std::string Target::unescape_text(std::string text) const
{
    // Restore KTESC markers to original characters (for programmatic use)
    return ktesc_resolve(text);
}

std::string Target::resolve_escapes(std::string text) const
{
    // Target-declared escapes first (marker -> declared replacement), then
    // the resolution-only entries (:resolve -- how a QUOTED character
    // renders here), then the generic decode for the remaining markers
    // (marker -> the character itself: quoted punctuation and literal-span
    // content).
    for (const auto& [ch, repl] : m_escapes) {
        text = string_replace(text, escape_marker(ch), repl);
    }
    for (const auto& [ch, repl] : m_resolves) {
        text = string_replace(text, escape_marker(ch), repl);
    }
    return ktesc_resolve(text);
}

std::string ktesc_resolve(std::string text)
{
    // Hand-rolled scan: no std::regex here, this runs over document-sized
    // strings.
    static const std::string tag = "KTESC";
    size_t pos = 0;
    while ((pos = text.find(tag, pos)) != std::string::npos) {
        size_t start = pos + tag.size();
        size_t close = text.find(tag, start);
        if (close == std::string::npos) break;
        size_t len = close - start;
        bool is_hex = len > 0 && len % 4 == 0 &&
            std::all_of(text.begin() + start, text.begin() + close,
                        [](unsigned char c) { return std::isxdigit(c) != 0; });
        if (!is_hex) {
            // Not a marker body; the closing tag may open a real marker.
            pos = start;
            continue;
        }
        std::string chars {};
        for (size_t i = start; i < close; i += 4)
            chars += (char)std::stoi(text.substr(i, 4), nullptr, 16);
        text.replace(pos, close + tag.size() - pos, chars);
        pos += chars.size();
    }
    return text;
}

bool hide_quoted_punctuation(std::string& s)
{
    // ASCII punctuation, EXCEPT "'" -- ^' opens a ^'...'^ literal span, the
    // one documented exception to the rule (a literal apostrophe is ^0027^).
    // Hand-rolled scan: no std::regex, this can run over large text.
    static const std::string punct = R"pct(!"#$%&()*+,-./:;<=>?@[\]^_`{|}~)pct";
    if (s.find('^') == std::string::npos) return false;
    std::string result {};
    bool changed = false;
    for (size_t i = 0; i < s.size(); ++i) {
        if (s[i] == '^' && i + 1 < s.size()
            && punct.find(s[i + 1]) != std::string::npos) {
            result += Target::escape_marker(std::string(1, s[i + 1]));
            ++i;
            changed = true;
        } else {
            result += s[i];
        }
    }
    if (changed) s = result;
    return changed;
}

std::string hide_structural_characters(const std::string& s)
{
    std::string result {};
    for (char c : s) {
        if (c == '@' || c == '|' || c == '#' || c == '^' || c == ':' || c == '*')
            result += Target::escape_marker(std::string(1, c));
        else
            result += c;
    }
    return result;
}

void Target::add_after_apply(const std::string& function_specs)
{
    for (const auto& f : regex_split(function_specs, std::regex(R"(\s+;\s+)"), true)) {
        // A bare spec is ONE Python name (module.function): whitespace
        // inside it means two specs were written without the " ; "
        // separator, and the glued call would otherwise fail only at render
        // time, as a Python SyntaxError located at "phase" rather than at
        // this declaration (found 2026-08-22, the first time a target
        // declared two phases).  Mode-tagged specs (":cpp <library>
        // <function>") are exempt: the library is a filename, and filenames
        // may contain spaces -- which is exactly why the list separator is
        // ";" rather than whitespace.
        if (!f.empty() && f[0] != ':'
            && f.find_first_of(" \t\n") != std::string::npos) {
            throw Definition_error(
                "The :after_apply phase \"" + f + "\" contains whitespace. "
                "A bare phase is a single Python name (module.function), and "
                "several phases are separated by \" ; \":\n"
                "  :after_apply first.phase ; second.phase\n"
                "(A mode-tagged phase -- \":cpp <library> <function>\" -- may "
                "contain spaces; its library is a filename.)",
                m_loc);
        }
        m_after_apply.push_back(f);
    }
}
