#include "MarkdownDocument.h"

#include "MarkdownElement.h"
#include "MarkdownComponents.h"


void MarkdownDocument::addElement(std::unique_ptr<MarkdownElement> element)
{
    mElements.push_back(std::move(element));
}

std::size_t MarkdownDocument::getNumElements() const
{
    return mElements.size();
}

MarkdownElement* MarkdownDocument::getElement(std::size_t idx) const
{
    return mElements[idx].get();
}

void MarkdownDocument::doLinkTargetSubstitution(const std::string& targetString, const std::string& replacementString)
{
    for(auto& element : mElements)
    {
        element->doFieldSubstitution(MarkdownElement::Type::LINK, targetString, replacementString);
    }
}

std::vector<MarkdownLink*> MarkdownDocument::getAllLinks() const
{
    std::vector<MarkdownLink*> links;
    for(auto& element : mElements)
    {
        if (element->getType() == MarkdownElement::Type::PARAGRAPH)
        {
            auto para_links = dynamic_cast<MarkdownParagraph*>(element.get())->getAllLinks();
            links.insert(links.end(), para_links.begin(), para_links.end());
        }
    }
    return links;
}
