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

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

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

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

void Target::transform(katom_list& katoms)
{
    (void)K::log(3);
    std::for_each(
        katoms.begin(), katoms.end(), 
        [this] (Katom& k) {       
            // std::cout << "transform: " << k << "\n";
            if (k.m_type != katom_t::literal) {
                for (auto [a, b] : this->m_transforms) {
                    // std::cout << "  " << a << right_arrow << b << "\n";
                    k.m_text = string_replace(k.m_text, a, b);
                }
            }
        });
}

std::vector<std::pair<std::string, std::string>>
parse_transforms(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(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]});
    }
}

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)
    for (const auto& [ch, repl] : m_escapes) {
        text = string_replace(text, escape_marker(ch), ch);
    }
    return text;
}

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

void Target::add_after_apply(std::string function_specs)
{
    for (auto f : regex_split(function_specs, std::regex(R"(\s+;\s+)"), true)) {
        // msg() << "Add " << m_name << " after-apply: " << f << "\n";
        m_after_apply.push_back(f);
    }
}
