class Aws::Plugins::RegionalEndpoint

@api private

def after_initialize(client)

def after_initialize(client)
  region = client.config.region
  raise Errors::MissingRegionError if region.nil? || region == ''
  # resolve a default endpoint to preserve legacy behavior
  initialize_default_endpoint(client) if client.config.endpoint.nil?
  region_set = client.config.sigv4a_signing_region_set
  return if region_set.nil?
  raise Errors::InvalidRegionSetError unless region_set.is_a?(Array)
  region_set = region_set.compact.reject(&:empty?)
  raise Errors::InvalidRegionSetError if region_set.empty?
  client.config.sigv4a_signing_region_set = region_set
end

def env_global_endpoint(cfg)

def env_global_endpoint(cfg)
  return unless endpoint = ENV['AWS_ENDPOINT_URL']
  cfg.logger&.debug(
    "Endpoint configured from ENV['AWS_ENDPOINT_URL']: #{endpoint}\n")
  endpoint
end

def env_service_endpoint(cfg)

def env_service_endpoint(cfg)
  service_id = cfg.api.metadata['serviceId'] || cfg.api.metadata['endpointPrefix']
  env_service_id = service_id.gsub(" ", "_").upcase
  return unless endpoint = ENV["AWS_ENDPOINT_URL_#{env_service_id}"]
  cfg.logger&.debug(
    "Endpoint configured from ENV['AWS_ENDPOINT_URL_#{env_service_id}']: #{endpoint}\n")
  endpoint
end

def handle_legacy_pseudo_regions(cfg)

def handle_legacy_pseudo_regions(cfg)
  region = cfg.region
  new_region = region.gsub('fips-', '').gsub('-fips', '')
  if region != new_region
    warn("Legacy region #{region} was transformed to #{new_region}."\
         '`use_fips_endpoint` config was set to true.')
    cfg.override_config(:use_fips_endpoint, true)
    cfg.override_config(:region, new_region)
  end
end

def initialize_default_endpoint(client)

def initialize_default_endpoint(client)
  client_module = Object.const_get(client.class.name.rpartition('::').first)
  param_class = client_module.const_get(:EndpointParameters)
  endpoint_provider = client.config.endpoint_provider
  params = param_class.create(client.config)
  endpoint = endpoint_provider.resolve_endpoint(params)
  client.config.endpoint = endpoint.url
rescue ArgumentError, NameError
  # fallback to legacy
  client.config.endpoint = resolve_legacy_endpoint(client.config)
end

def resolve_custom_config_endpoint(cfg)

get a custom configured endpoint from ENV or configuration
def resolve_custom_config_endpoint(cfg)
  return if cfg.ignore_configured_endpoint_urls
  env_service_endpoint(cfg) || env_global_endpoint(cfg) || shared_config_endpoint(cfg)
end

def resolve_endpoint(cfg)

Endpoints2.0 that a custom endpoint has NOT been configured by the user.
When the `regional_endpoint` config is set to true - this indicates to
parameter.
Additional behavior controls the setting of the custom SDK::Endpoint
we must preserve that behavior.
but because new old service gems may depend on new core versions
NOTE: with Endpoints 2.0, some of this logic is deprecated
def resolve_endpoint(cfg)
  endpoint = resolve_custom_config_endpoint(cfg)
  endpoint_prefix = cfg.api.metadata['endpointPrefix']
  return endpoint unless endpoint.nil? && cfg.region && endpoint_prefix
  validate_region!(cfg.region)
  handle_legacy_pseudo_regions(cfg)
  # set regional_endpoint flag - this indicates to Endpoints 2.0
  # that a custom endpoint has NOT been configured by the user
  cfg.override_config(:regional_endpoint, true)
  # a default endpoint is resolved in after_initialize
  nil
end

def resolve_ignore_configured_endpoint_urls(cfg)

def resolve_ignore_configured_endpoint_urls(cfg)
  value = ENV['AWS_IGNORE_CONFIGURED_ENDPOINT_URLS']
  value ||= Aws.shared_config.ignore_configured_endpoint_urls(profile: cfg.profile)
  Aws::Util.str_2_bool(value&.downcase) || false
end

def resolve_legacy_endpoint(cfg)

set a default endpoint in config using legacy (endpoints.json) resolver
def resolve_legacy_endpoint(cfg)
  endpoint_prefix = cfg.api.metadata['endpointPrefix']
  if cfg.respond_to?(:sts_regional_endpoints)
    sts_regional = cfg.sts_regional_endpoints
  end
  endpoint = Aws::Partitions::EndpointProvider.resolve(
    cfg.region,
    endpoint_prefix,
    sts_regional,
    {
      dualstack: cfg.use_dualstack_endpoint,
      fips: cfg.use_fips_endpoint
    }
  )
  URI(endpoint)
end

def resolve_region(cfg)

def resolve_region(cfg)
  keys = %w[AWS_REGION AMAZON_REGION AWS_DEFAULT_REGION]
  env_region = ENV.values_at(*keys).compact.first
  env_region = nil if env_region == ''
  cfg_region = Aws.shared_config.region(profile: cfg.profile)
  env_region || cfg_region
end

def resolve_sigv4a_signing_region_set(cfg)

def resolve_sigv4a_signing_region_set(cfg)
  value = ENV['AWS_SIGV4A_SIGNING_REGION_SET']
  value ||= Aws.shared_config.sigv4a_signing_region_set(profile: cfg.profile)
  value.split(',') if value
end

def resolve_use_dualstack_endpoint(cfg)

def resolve_use_dualstack_endpoint(cfg)
  value = ENV['AWS_USE_DUALSTACK_ENDPOINT']
  value ||= Aws.shared_config.use_dualstack_endpoint(
    profile: cfg.profile
  )
  Aws::Util.str_2_bool(value) || false
end

def resolve_use_fips_endpoint(cfg)

def resolve_use_fips_endpoint(cfg)
  value = ENV['AWS_USE_FIPS_ENDPOINT']
  value ||= Aws.shared_config.use_fips_endpoint(profile: cfg.profile)
  Aws::Util.str_2_bool(value) || false
end

def shared_config_endpoint(cfg)

def shared_config_endpoint(cfg)
  service_id = cfg.api.metadata['serviceId'] || cfg.api.metadata['endpointPrefix']
  return unless endpoint = Aws.shared_config.configured_endpoint(profile: cfg.profile, service_id: service_id)
  cfg.logger&.debug(
    "Endpoint configured from shared config(profile: #{cfg.profile}): #{endpoint}\n")
  endpoint
end

def validate_region!(region)

check region is a valid RFC host label
def validate_region!(region)
  unless Seahorse::Util.host_label?(region)
    raise Errors::InvalidRegionError
  end
end