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

#include <ls-std/core/ConditionalFunctionExecutor.hpp>
#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
#include <ls-std/io/NewLine.hpp>
#include <ls-std/io/section-pair/evaluator/SectionPairValueArgumentEvaluator.hpp>
#include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
#include <ls-std/io/section-pair/serialization/SerializableSectionPairRowSingleValue.hpp>

using ls::standard::core::Class;
using ls::standard::core::ConditionalFunctionExecutor;
using ls::standard::core::EmptyStringArgumentEvaluator;
using ls::standard::core::type::byte_field;
using ls::standard::io::NewLine;
using ls::standard::io::section_pair_row_value;
using ls::standard::io::SectionPairRowEnumType;
using ls::standard::io::SectionPairRowSingleValue;
using ls::standard::io::SectionPairRowValue;
using ls::standard::io::SectionPairValueArgumentEvaluator;
using ls::standard::io::SerializableSectionPairRowSingleValue;
using std::make_shared;
using std::string;

SectionPairRowSingleValue::SectionPairRowSingleValue(const section_pair_row_value &_value) : Class("SectionPairRowSingleValue"), SectionPairRowValue(SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE)
{
  this->_set(_value);
}

SectionPairRowSingleValue::~SectionPairRowSingleValue() noexcept = default;

section_pair_row_value SectionPairRowSingleValue::get() const
{
  return this->value;
}

SectionPairRowEnumType SectionPairRowSingleValue::getType()
{
  return this->_getType();
}

byte_field SectionPairRowSingleValue::marshal()
{
  ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  return this->serializable->marshal();
}

void SectionPairRowSingleValue::set(const section_pair_row_value &_value)
{
  this->_set(_value);
}

void SectionPairRowSingleValue::unmarshal(const byte_field &_data)
{
  ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  this->serializable->unmarshal(_data);
}

void SectionPairRowSingleValue::_createSerializable()
{
  SerializableSectionPairParameter parameter{};
  parameter.setValue(shared_from_this());

  if (!this->_getReservedNewLine().empty())
  {
    parameter.setNewLine(this->_getReservedNewLine());
  }

  this->serializable = make_shared<SerializableSectionPairRowSingleValue>(parameter);
}

void SectionPairRowSingleValue::_set(const section_pair_row_value &_value)
{
  EmptyStringArgumentEvaluator{_value}.evaluate();
  SectionPairValueArgumentEvaluator(_value).evaluate();
  const string newLine = this->_getReservedNewLine().empty() ? NewLine::get() : this->_getReservedNewLine();
  this->value = _value;

  if (this->value.find(newLine) != string::npos)
  {
    this->value.replace(this->value.find(newLine), newLine.size(), "");
  }
}
