require 'cucumber/messages.dtos'
require 'json'

# The code was auto-generated by {this script}[https://github.com/cucumber/messages/blob/main/jsonschema/scripts/codegen.rb]
#

module Cucumber
  module Messages
  <%- @schemas.each do |key, schema| -%>

    class <%= class_name(key) %>

      ##
      # Returns a new <%= class_name(key) %> from the given hash.
      # If the hash keys are camelCased, they are properly assigned to the
      # corresponding snake_cased attributes.
      #
      #   Cucumber::Messages::<%= class_name(key) %>.from_h(some_hash) # => #<Cucumber::Messages::<%= class_name(key) %>:0x... ...>
      #

      def self.from_h(hash)
        return nil if hash.nil?

        self.new(
        <%-
          schema['properties'].each do |property_name, property|
            ref = property['$ref']
            items_ref = property.dig('items', '$ref')
        -%>
          <%= "#{underscore(property_name)}: " -%>
          <%- if items_ref -%>hash[:<%= property_name -%>]&.map { |item| <%= class_name(items_ref) %>.from_h(item) },
          <%- elsif ref -%><%= class_name(ref) %>.from_h(hash[:<%= property_name %>]),
          <%- else -%>hash[:<%= property_name %>],
          <%- end -%>
        <%- end -%>
        )
      end
    end
  <%- end -%>
  end
end
