#pragma once
#include <string>
#include <vector>
#include "../base/interface.hpp"

extern "C" {
#include <tree_sitter/api.h>
}

namespace lsp::provider::text_document
{
    class Rename : public IRequestProvider
    {
    public:
        Rename() = default;

        std::string GetMethod() const override;
        std::string GetProviderName() const override;
        std::string ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context) override;

    private:
        // 构建重命名响应 - 返回 WorkspaceEdit | null
        std::optional<protocol::WorkspaceEdit> BuildRenameResponse(const protocol::RenameParams& params, ExecutionContext& context);

        // 查找所有需要重命名的位置
        std::vector<protocol::Location> FindRenameLocations(const protocol::DocumentUri& uri, const std::string& old_name, ExecutionContext& context);

        // 获取位置处的标识符和范围
        std::string GetIdentifierAtPosition(const protocol::DocumentUri& uri, const protocol::Position& position, ExecutionContext& context);
        bool IsDefinitionNode(TSNode node, const std::string& identifier, const std::string& content);

        // 验证是否可以重命名
        bool CanRename(const std::string& identifier);

        // 验证新名称是否有效
        bool IsValidIdentifier(const std::string& new_name);

        // 在节点中查找所有引用
        void FindIdentifiersInNode(TSNode node, const std::string& identifier, const std::string& content, std::vector<protocol::Range>& ranges);
    };

    namespace
    {
        // 节点类型常量
        constexpr const char* kIdentifier = "identifier";
        constexpr const char* kFunctionDefinition = "function_definition_statement";
        constexpr const char* kFunctionDeclaration = "function_declaration_statement";
        constexpr const char* kClassDefinition = "class_definition_statement";
        constexpr const char* kVarStatement = "var_statement";
        constexpr const char* kStaticStatement = "static_statement";
        constexpr const char* kGlobalStatement = "global_statement";
        constexpr const char* kConstStatement = "const_statement";
        constexpr const char* kMethodDeclaration = "method_declaration";
        constexpr const char* kMethodDeclarationOnly = "method_declaration_only";
        constexpr const char* kMethodWithModifier = "method_with_modifier";
        constexpr const char* kMethodWithImplementation = "method_with_implementation";
        constexpr const char* kPropertyDeclaration = "property_declaration";
        constexpr const char* kAssignmentExpression = "assignment_expression";
        constexpr const char* kVarDeclaration = "var_declaration";
        constexpr const char* kStaticDeclaration = "static_declaration";
        constexpr const char* kGlobalDeclaration = "global_declaration";

        // TSF语言关键字（不能用作标识符）
        const std::set<std::string> kReservedKeywords = {
            "and",
            "array",
            "begin",
            "case",
            "class",
            "const",
            "constructor",
            "destructor",
            "div",
            "do",
            "downto",
            "else",
            "end",
            "except",
            "file",
            "finally",
            "for",
            "function",
            "global",
            "goto",
            "if",
            "implementation",
            "in",
            "inherited",
            "initialization",
            "interface",
            "is",
            "label",
            "mod",
            "nil",
            "not",
            "object",
            "of",
            "or",
            "packed",
            "procedure",
            "program",
            "property",
            "raise",
            "record",
            "repeat",
            "set",
            "shl",
            "shr",
            "static",
            "string",
            "then",
            "to",
            "try",
            "type",
            "unit",
            "until",
            "uses",
            "var",
            "while",
            "with",
            "true",
            "false",
            "self",
            "virtual",
            "override",
            "overload",
            "public",
            "private",
            "protected",
            "published",
            "finalization",
            "break",
            "continue",
            "exit",
            "return",
            "new",
            "free",
            "echo"
        };
    }

}
