class ViewComponent::Compiler

def define_render_template_for

def define_render_template_for
  @templates.each do |template|
    @redefinition_lock.synchronize do
      template.compile_to_component
    end
  end
  method_body =
    if @templates.one?
      @templates.first.safe_method_name
    elsif (template = @templates.find(&:inline?))
      template.safe_method_name
    else
      branches = []
      @templates.each do |template|
        conditional =
          if template.inline_call?
            "variant&.to_sym == #{template.variant.inspect}"
          else
            [
              template.default_format? ? "(format == #{ViewComponent::Base::VC_INTERNAL_DEFAULT_FORMAT.inspect} || format.nil?)" : "format == #{template.format.inspect}",
              template.variant.nil? ? "variant.nil?" : "variant&.to_sym == #{template.variant.inspect}"
            ].join(" && ")
          end
        branches << [conditional, template.safe_method_name]
      end
      out = branches.each_with_object(+"") do |(conditional, branch_body), memo|
        memo << "#{(!memo.present?) ? "if" : "elsif"} #{conditional}\n  #{branch_body}\n"
      end
      out << "else\n  #{templates.find { _1.variant.nil? && _1.default_format? }.safe_method_name}\nend"
    end
  @redefinition_lock.synchronize do
    @component.silence_redefinition_of_method(:render_template_for)
    @component.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def render_template_for(variant = nil, format = nil)
      #{method_body}
    end
    RUBY
  end
end