/*
 * Author:          Patrick-Christopher Mattulat
 * Co-Author:       Claude Sonnet 4.6 (LLM)
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2020-09-30
 * Changed:         2026-06-23
 *
 * */

#include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
#include <ls-std/io/xml/XmlDocument.hpp>

using ls::standard::core::Class;
using ls::standard::core::NullPointerArgumentEvaluator;
using ls::standard::io::XmlDeclaration;
using ls::standard::io::XmlDocument;
using ls::standard::io::XmlNode;
using std::shared_ptr;
using std::string;

XmlDocument::XmlDocument() : Class("XmlDocument")
{}

XmlDocument::~XmlDocument() noexcept = default;

shared_ptr<XmlDeclaration> XmlDocument::getDeclaration() const
{
  return this->declaration;
}

shared_ptr<XmlNode> XmlDocument::getRootElement() const
{
  return this->rootElement;
}

void XmlDocument::setDeclaration(const shared_ptr<XmlDeclaration> &_declaration)
{
  this->_assignDeclaration(_declaration);
}

void XmlDocument::setRootElement(const shared_ptr<XmlNode> &_rootElement)
{
  this->_assignRootElement(_rootElement);
}

string XmlDocument::toXml() const
{
  string xmlString{};

  if (this->declaration != nullptr)
  {
    xmlString = this->declaration->toXml();

    if (!xmlString.empty())
    {
      xmlString += "\n";
    }
  }

  return xmlString + this->rootElement->toXml();
}

void XmlDocument::_assignDeclaration(const shared_ptr<XmlDeclaration> &_declaration)
{
  NullPointerArgumentEvaluator{_declaration, "xml declaration reference is null!"}.evaluate();
  this->declaration = _declaration;
}

void XmlDocument::_assignRootElement(const shared_ptr<XmlNode> &_rootElement)
{
  NullPointerArgumentEvaluator{_rootElement, "xml root node reference is null!"}.evaluate();
  this->rootElement = _rootElement;
}
