class Lutaml::Model::Xml::NokogiriAdapter

def self.parse(xml, options = {})

def self.parse(xml, options = {})
  parsed = Nokogiri::XML(xml, nil, encoding(xml, options))
  @root = NokogiriElement.new(parsed.root)
  new(@root, parsed.encoding)
end

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

def build_ordered_element(xml, element, options = {})
  mapper_class = determine_mapper_class(element, options)
  xml_mapping = mapper_class.mappings_for(:xml)
  return xml unless xml_mapping
  attributes = build_attributes(element, xml_mapping)&.compact
  prefixed_xml = prefix_xml(xml, xml_mapping, options)
  tag_name = options[:tag_name] || xml_mapping.root_element
  tag_name = "#{tag_name}_" if prefixed_xml.respond_to?(tag_name)
  prefixed_xml.public_send(tag_name, attributes) do
    if options.key?(:namespace_prefix) && !options[:namespace_prefix]
      xml.parent.namespace = nil
    end
    index_hash = {}
    content = []
    element.element_order.each do |object|
      object_key = "#{object.name}-#{object.type}"
      index_hash[object_key] ||= -1
      curr_index = index_hash[object_key] += 1
      element_rule = xml_mapping.find_by_name(object.name, type: object.type)
      next if element_rule.nil?
      attribute_def = attribute_definition_for(element, element_rule,
                                               mapper_class: mapper_class)
      value = attribute_value_for(element, element_rule)
      if element_rule == xml_mapping.content_mapping
        next if element_rule.cdata && object.text?
        text = xml_mapping.content_mapping.serialize(element)
        text = text[curr_index] if text.is_a?(Array)
        next prefixed_xml.add_text(xml, text, cdata: element_rule.cdata) if element.mixed?
        content << text
      elsif !value.nil? || element_rule.render_nil?
        value = value[curr_index] if attribute_def.collection?
        add_to_xml(
          xml,
          element,
          element_rule.prefix,
          value,
          options.merge(
            attribute: attribute_def,
            rule: element_rule,
            mapper_class: mapper_class,
          ),
        )
      end
    end
    prefixed_xml.text content.join
  end
end

def prefix_xml(xml, mapping, options)

def prefix_xml(xml, mapping, options)
  if options.key?(:namespace_prefix)
    xml[options[:namespace_prefix]] if options[:namespace_prefix]
  elsif mapping.namespace_prefix
    xml[mapping.namespace_prefix]
  end
  xml
end

def to_xml(options = {})

def to_xml(options = {})
  builder_options = {}
  if options.key?(:encoding)
    builder_options[:encoding] = options[:encoding] unless options[:encoding].nil?
  elsif options.key?(:parse_encoding)
    builder_options[:encoding] = options[:parse_encoding]
  else
    builder_options[:encoding] = "UTF-8"
  end
  builder = Builder::Nokogiri.build(builder_options) do |xml|
    if root.is_a?(Lutaml::Model::Xml::NokogiriElement)
      root.build_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