# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

# Used to prevent deprecation warnings from flooding stdout
ENV['PB_IGNORE_DEPRECATIONS'] = 'true'

# Top-level namespace for Contrast Security agent
module Contrast
end

# Some developers override various methods on Object, which can often involve
# changing expected method parity/behavior which in turn prevents us from being
# able to reliably use affected methods.
# We alias these method so that we always have access to them.
#
# Because we use these methods in constructing classes (e.g., calling #freeze
# on constants within class definitions) we do this aliasing ASAP.
class Object
  alias_method :cs__class, :class
  alias_method :cs__freeze, :freeze
  alias_method :cs__frozen?, :frozen?
  alias_method :cs__is_a?, :is_a?
  alias_method :cs__method, :method
  alias_method :cs__respond_to?, :respond_to?
  alias_method :cs__singleton_class, :singleton_class
end

if RUBY_VERSION >= '3.0.0'
  # This fixes Ruby 3.0 issues with Module#(some instance method) patching by preventing the prepending of
  # a JSON helper on protobuf load. String.instance_method(:+) is one of the most noticable.
  # TODO: RUBY-1132 Remove this once Ruby 3 is fixed.
  # See bug here: https://bugs.ruby-lang.org/issues/17725
  class Class
    alias_method(:cs__orig_prepend, :prepend)
    def prepend other
      include(other)
    end
  end
end

# component interface for class creation
# config gets built as a consequence of this require
require 'contrast/components/interface'

# This needs to be required very early, after component interfaces, and before instrumentation attempts
require 'contrast/funchook/funchook'

# shared configuration support
require 'contrast/config'
require 'contrast/configuration'

require 'contrast/agent/version'

# errors and exceptions
require 'contrast/security_exception'

# shared utils
require 'contrast/utils/timer'
require 'contrast/utils/preflight_util'

require 'contrast/utils/assess/sampling_util'
require 'contrast/agent'

if RUBY_VERSION >= '3.0.0'
  # Put prepend back as it was.
  Class.alias_method(:prepend, :cs__orig_prepend)
  Class.remove_method(:cs__orig_prepend)
end