#pragma once

#include <map>
#include <iomanip>
#include <sstream>

#include "katom.h"
#include "locator.h"

class Target
{
public:
    Target() = default;
//    Target(std::vector<Katom>::iterator begin, std::vector<Katom>::iterator end, Argtype_registry argtypes);
    Target(const std::string& name, const std::string& desc, const Locator& loc)
        : m_name(name)
        , m_desc(desc)
        , m_loc(loc)
        {};

    void add_transform(const std::string& original, const std::string& transformed);
    void add_transforms(const std::string& transforms);
    void add_transforms(const std::vector<std::pair<std::string, std::string>>& transforms);
    void transform(std::vector<Katom>& katoms) const;

    void add_escapes(const std::string& escape_spec);
    void add_resolves(const std::string& resolve_spec);
    std::string escape_text(std::string text) const;
    std::string unescape_text(std::string text) const;
    std::string resolve_escapes(std::string text) const;
    static std::string escape_marker(const std::string& ch);

    void add_after_apply(const std::string& function_specs);

    std::string m_name {};
    std::string m_desc {};
    std::vector<std::string> m_includes {};
    std::vector<std::string> m_provides {};
    std::vector<std::string> m_after_apply {};

    Locator m_loc {};
    std::vector<std::pair<std::string, std::string>> m_transforms {};
    std::vector<std::pair<std::string, std::string>> m_escapes {};
    // Resolution-only entries (:resolve): how a QUOTED character renders in
    // this target.  The resolve half of :escape without the escape half --
    // needed where the raw character must stay untouched in writer text
    // (tex cannot escape "-" without destroying the --- convention) but the
    // quoted character must not decode to its raw self (a decoded -- would
    // re-form TeX's dash ligature).
    std::vector<std::pair<std::string, std::string>> m_resolves {};
//    Argtype_registry m_argtypes {};


};

std::vector<std::pair<std::string, std::string>>
parse_transforms(const std::string& transform_string);

// Decode every KTESC<hex>KTESC marker in text back to its original
// characters.  Used for the final output (after target-declared escapes have
// been resolved to their replacements) and for programmatic use of argument
// values.  The Python counterpart is unescape_ktesc() in klammer_base.py.
std::string ktesc_resolve(std::string text);

// Replace each Klammertext structural character (@ | # ^ : *) in s with its
// KTESC marker, so text that has already been interpreted once (quoted
// specials, ^'...'^ literal content) survives re-katomization by
// sub-Machines and the @eval result read-back.  Resolved by ktesc_resolve()
// at final processing.
std::string hide_structural_characters(const std::string& s);

// Replace each ^P pair in s -- "^" before any ASCII punctuation character
// except "'" (which opens a ^'...'^ literal span) -- with the KTESC marker
// of P: "^" before a punctuation character quotes it, uniformly, not only
// the six Klammertext specials.  A letter or digit is never special, so
// ^<letter-or-digit> keeps its existing meanings (diacritics, mnemonics,
// ^UUUU^ code points).  Returns whether anything was replaced.  Called from
// hide_special_katoms() on writer-text katoms OUTSIDE definition and
// @eval/@read/@cond spans -- code keeps its carets (grep '^-' must reach
// the shell intact), the same skip set as the quoted-special hiding.
bool hide_quoted_punctuation(std::string& s);
