# frozen_string_literal: true

module <%= class_name %>Item::Provided
  extend ActiveSupport::Concern

  def <%= file_name %>_provider
    return nil unless credentials_configured?

    Provider::<%= class_name %>.new(
<% secret_fields = parsed_fields.select { |f| f[:secret] } -%>
<% default_fields = parsed_fields.select { |f| f[:default] } -%>
<% secret_fields.each_with_index do |field, index| -%>
<% needs_comma = index < secret_fields.size - 1 || default_fields.any? -%>
      <%= field[:name] %>: <%= field[:name] %><%= needs_comma ? ',' : '' %>
<% end -%>
<% default_fields.each_with_index do |field, index| -%>
      <%= field[:name] %>: effective_<%= field[:name] %><%= index < default_fields.size - 1 ? ',' : '' %>
<% end -%>
    )
  end

  # Returns credentials hash for API calls that need them passed explicitly
  def <%= file_name %>_credentials
    return nil unless credentials_configured?

    {
<% parsed_fields.select { |f| f[:secret] }.each_with_index do |field, index| -%>
      <%= field[:name] %>: <%= field[:name] %><%= index < parsed_fields.select { |f| f[:secret] }.size - 1 ? ',' : '' %>
<% end -%>
    }
  end
end
