module CancerRegistryReportingTestKit::ValidationTest

def check_for_dar(resource)

def check_for_dar(resource)
  unless scratch[:dar_code_found]
    resource.each_element do |element, _meta, _path|
      next unless element.is_a?(FHIR::Coding)
      check_for_dar_code(element)
    end
  end
  return if scratch[:dar_extension_found]
  check_for_dar_extension(resource)
end

def check_for_dar_code(coding)

def check_for_dar_code(coding)
  return unless coding.code == 'unknown' && coding.system == DAR_CODE_SYSTEM_URL
  scratch[:dar_code_found] = true
  output dar_code_found: 'true'
end

def check_for_dar_extension(resource)

def check_for_dar_extension(resource)
  return unless resource.source_contents&.include? DAR_EXTENSION_URL
  scratch[:dar_extension_found] = true
  output dar_extension_found: 'true'
end

def find_validation_errors(resourceType = resource_type,

def find_validation_errors(resourceType = resource_type,
                      resources,
                      profile_url,
                      profile_version,
                      skip_if_empty: true)
  skip_if skip_if_empty && resources.blank?,
          "No #{resourceType} resources conforming to the #{profile_url} profile were returned"
  omit_if resources.blank?,
          "No #{resourceType} resources provided so the #{profile_url} profile does not apply"
  profile_with_version = "#{profile_url}|#{profile_version}"
  resources.each do |resource|
    resource_is_valid?(resource: resource, profile_url: profile_with_version)
    # DAR checks are a SHOULD requirement in CCRR 1.0.1
    # check_for_dar(resource)
  end
end

def perform_validation_test(resourceType = resource_type,

def perform_validation_test(resourceType = resource_type,
                            resources,
                            profile_url,
                            profile_version,
                            skip_if_empty: true)
  find_validation_errors(resourceType, resources, profile_url, profile_version, skip_if_empty: )
  errors_found = messages.any? { |message| message[:type] == 'error' }
  profile_with_version = "#{profile_url}|#{profile_version}"
  assert !errors_found, "Resource does not conform to the profile #{profile_with_version}"
end