# frozen_string_literal: true

module RailsEventStore
  module AsyncHandler
    def self.with_defaults
      Module.new do
        def self.prepended(_host_class)
          prepend AsyncHandler.with
        end
      end
    end

    def self.with(event_store: Rails.configuration.event_store, serializer: YAML)
      Module.new do
        define_method :perform do |payload|
          super(event_store.deserialize(serializer: serializer, **payload.symbolize_keys))
        end
      end
    end

    def self.prepended(host_class)
      host_class.prepend with_defaults
    end
  end

  module CorrelatedHandler
    def perform(event)
      Rails.configuration.event_store.with_metadata(
        correlation_id: event.metadata[:correlation_id],
        causation_id:   event.event_id
      ) do
        super
      end
    end
  end
end

# frozen_string_literal: true

module RailsEventStore
  module AsyncHandler
    def self.with_defaults
      with
    end

    def self.with(event_store: Rails.configuration.event_store, serializer: YAML)
      Module.new do
        define_method :perform do |payload|
          super(event_store.deserialize(serializer: serializer, **payload.symbolize_keys))
        end
      end
    end

    def self.prepended(host_class)
      host_class.prepend with_defaults
    end
  end

  module CorrelatedHandler
    def perform(event)
      Rails.configuration.event_store.with_metadata(
        correlation_id: event.metadata[:correlation_id],
        causation_id:   event.event_id
      ) do
        super
      end
    end
  end
end

Kill mutant by simplifying the method
Kill mutant by simplifying the method

That whole anonymous module is completely unnecessary
