#pragma once

#include <memory>
#include <unordered_map>
#include "./base/event_bus.hpp"
#include "./detail/document/text_document.hpp"
#include "../protocol/protocol.hpp"

namespace lsp::service
{
    class Document
    {
    public:
        explicit Document(std::shared_ptr<EventBus> event_bus);
        ~Document();

        void OpenDocument(const protocol::DidOpenTextDocumentParams& params);
        void UpdateDocument(const protocol::DidChangeTextDocumentParams& params);
        void CloseDocument(const protocol::DidCloseTextDocumentParams& params);

        std::optional<protocol::string> GetContent(const protocol::DocumentUri& uri) const;
        std::optional<protocol::integer> GetVersion(const protocol::DocumentUri& uri) const;
        std::vector<protocol::DocumentUri> GetAllDocumentUris() const;

    private:
        mutable std::shared_mutex mutex_;
        std::unordered_map<protocol::DocumentUri, document::TextDocument> documents_;
        std::shared_ptr<EventBus> event_bus_;
    };
}
