class Lutaml::Model::Xml::NokogiriElement

def build_attributes(node, _options = {})

def build_attributes(node, _options = {})
  attrs = node.attributes.transform_values(&:value)
  attrs.merge(build_namespace_attributes(node))
end

def build_namespace_attributes(node)

def build_namespace_attributes(node)
  namespace_attrs = {}
  node.own_namespaces.each_value do |namespace|
    namespace_attrs[namespace.attr_name] = namespace.uri
  end
  node.children.each do |child|
    namespace_attrs = namespace_attrs.merge(
      build_namespace_attributes(child),
    )
  end
  namespace_attrs
end

def build_xml(builder = nil)

def build_xml(builder = nil)
  builder ||= Builder::Nokogiri.build
  if name == "text"
    builder.text(text)
  else
    builder.public_send(name, build_attributes(self)) do |xml|
      children.each do |child|
        child.build_xml(xml)
      end
    end
  end
  builder
end

def initialize(node, root_node: nil, default_namespace: nil)

def initialize(node, root_node: nil, default_namespace: nil)
  if root_node
    node.namespaces.each do |prefix, name|
      namespace = XmlNamespace.new(name, prefix)
      root_node.add_namespace(namespace)
    end
  end
  attributes = {}
  # Using `attribute_nodes` instead of `attributes` because
  # `attribute_nodes` handles name collisions as well
  # More info: https://devdocs.io/nokogiri/nokogiri/xml/node#method-i-attributes
  node.attribute_nodes.each do |attr|
    name = if attr.namespace
             "#{attr.namespace.prefix}:#{attr.name}"
           else
             attr.name
           end
    attributes[name] = XmlAttribute.new(
      name,
      attr.value,
      namespace: attr.namespace&.href,
      namespace_prefix: attr.namespace&.prefix,
    )
  end
  if root_node.nil? && !node.namespace&.prefix
    default_namespace = node.namespace&.href
  end
  super(
    node,
    attributes,
    parse_all_children(node, root_node: root_node || self,
                             default_namespace: default_namespace),
    node.text,
    name: node.name,
    parent_document: root_node,
    namespace_prefix: node.namespace&.prefix,
    default_namespace: default_namespace
  )
end

def inner_xml

def inner_xml
  children.map(&:to_xml).join
end

def parse_all_children(node, root_node: nil, default_namespace: nil)

def parse_all_children(node, root_node: nil, default_namespace: nil)
  node.children.map do |child|
    NokogiriElement.new(child, root_node: root_node,
                               default_namespace: default_namespace)
  end
end

def parse_children(node, root_node: nil)

def parse_children(node, root_node: nil)
  node.children.select(&:element?).map do |child|
    NokogiriElement.new(child, root_node: root_node)
  end
end

def text?

def text?
  # false
  children.empty? && text.length.positive?
end

def to_xml

def to_xml
  return text if text?
  build_xml.doc.root.to_xml
end