#pragma once

#include <regex>

#include "deftype.h"
#include "argument_set.h"
#include "target_set.h"
#include "locator.h"

class Klammer
{
public:
    Klammer() = default;
    Klammer(const std::string& name)
        : m_name(name)
      {};

    using variable_map_t = std::map<std::string, std::vector<int>>;
    using target_variable_map_t = std::map<std::string, variable_map_t>;
    static std::regex name_re; // = std::regex(R"((\w+)(?:\.(\w+))?)");

    struct components {
        std::string target;
        katom_t deftype;
        Parameter_set parameters;
        std::vector<Katom> body;
        variable_map_t varmap;
        Locator loc;
    };

    // target-name -> [variable -> index]

    void add_target_definition(
        std::string target_name, Argtype_set argtypes,
        std::vector<Katom>::iterator begin, std::vector<Katom>::iterator end);
    void remove_target_definition(const std::string& target_name);

    auto target_defs(std::vector<std::string> target_names);
    auto instance_defs();
    void disallow_instances(); //Klammer::components declaration);
    bool copy_to_instances(Target_set targets);
    void copy_components(
        Parameter_set parameters, std::vector<Klammer::components> cs, Target_set targets);
    void check_for_multiple_general_klammers();
    void check_for_declaration_and_definitions();
    void copy_general_klammer_to_undefined(Target_set targets);
    void no_declarations(Target_set targets);
    void one_declaration(Target_set targets, Klammer::components declare);
    void many_declarations(std::vector<Klammer::components> declares);
    void rationalize(Target_set target);
    /*
    void add_description(const std::string& desc, Katom definition_type);

    auto user_defs();
    auto klammer_defines_parameters();
    auto klammer_uses_parameters();
    void check_for_target_errors(Target_set targets);
    bool explicit_parameters_match();
    void copy_components(Klammer::components cs, Target_set targets);
    */

    std::string signature_text();
    std::string description_text();
    std::string describe(int margin=0);

    bool has_literal_param() const {
        for (const auto& p : m_parameters.m_positional)
            if (p.m_argtype.m_name == "literal") return true;
        for (const auto& p : m_parameters.m_rest)
            if (p.m_argtype.m_name == "literal") return true;
        return false;
    }
    strings_t get_target_names() const;
    strings_t get_locations();

    // Initial instantiation:
    std::string m_name {};

    // Collection: target, deftype, parameters, body, locator
    std::vector<Klammer::components>  m_defs {};

    // After rationalization:
    Parameter_set m_parameters {};
    std::map<std::string, std::vector<Katom>> m_body {};
    std::string m_desc {};
    std::map<std::string, Locator> m_defloc {}; // target -> Locator
    std::map<std::string, defmode_t> m_defmode {}; // target -> defmode
    target_variable_map_t m_varmap {}; // target -> map: variable -> index
};

std::string klammer_name_from_katom(std::string s, Locator loc);

std::tuple<std::string,std::string>
parse_name(Target_set targets, Katom name_katom);

std::tuple<Katom, Parameter_set, std::vector<Katom>, Locator>
parse_definition_katoms(Argtype_set argtypes,  //Target_set targets,
                        std::vector<Katom>::iterator& begin, std::vector<Katom>::iterator& end);

/*
klammer_definition_args parse_klammer_definition_katoms(
    katom_list& katoms, Argtype_set& argtypes);

void check_for_undefined_arguments(
    std::string name, Parameters parameters, katom_list body_katoms, Locator loc);
*/
