class Lutaml::Model::XmlAdapter::NokogiriAdapter

def self.parse(xml)

def self.parse(xml)
  parsed = Nokogiri::XML(xml)
  root = NokogiriElement.new(parsed.root)
  new(root)
end

def add_to_xml(xml, value, attribute, rule)

def add_to_xml(xml, value, attribute, rule)
  if value && (attribute&.type&.<= Lutaml::Model::Serialize)
    handle_nested_elements(
      xml,
      value,
      rule: rule,
      attribute: attribute,
    )
  else
    xml.public_send(rule.name) do
      if !value.nil?
        serialized_value = attribute.type.serialize(value)
        if attribute.type == Lutaml::Model::Type::Hash
          serialized_value.each do |key, val|
            xml.public_send(key) { xml.text val }
          end
        else
          xml.text(serialized_value)
        end
      end
    end
  end
end

def build_ordered_element(xml, element, options = {})

def build_ordered_element(xml, element, options = {})
  mapper_class = options[:mapper_class] || element.class
  xml_mapping = mapper_class.mappings_for(:xml)
  return xml unless xml_mapping
  attributes = build_attributes(element, xml_mapping)&.compact
  prefixed_xml = if options.key?(:namespace_prefix)
                   options[:namespace_prefix] ? xml[options[:namespace_prefix]] : xml
                 elsif xml_mapping.namespace_prefix
                   xml[xml_mapping.namespace_prefix]
                 else
                   xml
                 end
  tag_name = options[:tag_name] || xml_mapping.root_element
  prefixed_xml.public_send(tag_name, attributes) do
    if options.key?(:namespace_prefix) && !options[:namespace_prefix]
      xml.parent.namespace = nil
    end
    index_hash = {}
    element.element_order.each do |name|
      index_hash[name] ||= -1
      curr_index = index_hash[name] += 1
      element_rule = xml_mapping.find_by_name(name)
      next if element_rule.nil?
      attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class)
      value = attribute_value_for(element, element_rule)
      nsp_xml = element_rule.prefix ? xml[element_rule.prefix] : xml
      if element_rule == xml_mapping.content_mapping
        text = element.send(xml_mapping.content_mapping.to)
        text = text[curr_index] if text.is_a?(Array)
        prefixed_xml.text text
      elsif attribute_def.collection?
        add_to_xml(nsp_xml, value[curr_index], attribute_def,
                   element_rule)
      elsif !value.nil? || element_rule.render_nil?
        add_to_xml(nsp_xml, value, attribute_def, element_rule)
      end
    end
  end
end

def build_unordered_element(xml, element, options = {})

def build_unordered_element(xml, element, options = {})
  mapper_class = options[:mapper_class] || element.class
  xml_mapping = mapper_class.mappings_for(:xml)
  return xml unless xml_mapping
  attributes = options[:xml_attributes] ||= {}
  attributes = build_attributes(element,
                                xml_mapping).merge(attributes)&.compact
  prefixed_xml = if options.key?(:namespace_prefix)
                   options[:namespace_prefix] ? xml[options[:namespace_prefix]] : xml
                 elsif xml_mapping.namespace_prefix
                   xml[xml_mapping.namespace_prefix]
                 else
                   xml
                 end
  tag_name = options[:tag_name] || xml_mapping.root_element
  prefixed_xml.public_send(tag_name, attributes) do
    if options.key?(:namespace_prefix) && !options[:namespace_prefix]
      xml.parent.namespace = nil
    end
    xml_mapping.elements.each do |element_rule|
      attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class)
      value = attribute_value_for(element, element_rule)
      next if value.nil? && !element_rule.render_nil?
      nsp_xml = element_rule.prefix ? xml[element_rule.prefix] : xml
      if attribute_def.collection?
        value.each do |v|
          add_to_xml(nsp_xml, v, attribute_def, element_rule)
        end
      elsif !value.nil? || element_rule.render_nil?
        add_to_xml(nsp_xml, value, attribute_def, element_rule)
      end
    end
    if xml_mapping.content_mapping
      text = element.send(xml_mapping.content_mapping.to)
      text = text.join if text.is_a?(Array)
      prefixed_xml.text text
    end
  end
end

def to_xml(options = {})

def to_xml(options = {})
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    if root.is_a?(Lutaml::Model::XmlAdapter::NokogiriElement)
      root.to_xml(xml)
    else
      mapper_class = options[:mapper_class] || @root.class
      options[:xml_attributes] = build_namespace_attributes(mapper_class)
      build_element(xml, @root, options)
    end
  end
  xml_options = {}
  xml_options[:indent] = 2 if options[:pretty]
  xml_data = builder.doc.root.to_xml(xml_options)
  options[:declaration] ? declaration(options) + xml_data : xml_data
end