class GdsApi::FinderSchema

def allowed_values_for(facet_key)

def allowed_values_for(facet_key)
  find_facet(facet_key).fetch("allowed_values")
end

def facets

def facets
  schema_hash.fetch("facets")
end

def find_facet(facet_key)

def find_facet(facet_key)
  facets.find { |facet| facet.fetch("key") == facet_key }
end

def find_schema_allowed_value_entry(facet_key, value)

def find_schema_allowed_value_entry(facet_key, value)
  value_label_pair = allowed_values_for(facet_key)
    .find { |schema_value|
      schema_value.fetch("value") == value
    }
  if value_label_pair.nil?
    raise_value_not_found_error(facet_key, value)
  else
    value_label_pair.fetch("label")
  end
end

def initialize(schema_hash)

def initialize(schema_hash)
  @schema_hash = schema_hash
end

def raise_value_not_found_error(facet_key, value)

def raise_value_not_found_error(facet_key, value)
  raise NotFoundError.new("#{facet_key} value '#{value}' not found in #{slug} schema")
end

def slug

def slug
  schema_hash.fetch("slug")
end

def user_friendly_facet_label(facet_key)

def user_friendly_facet_label(facet_key)
  find_facet(facet_key.to_s).fetch("name")
end

def user_friendly_values(document_attributes)

def user_friendly_values(document_attributes)
  document_attributes.each_with_object({}) do |(k, v), values|
    values.store(
      user_friendly_facet_label(k.to_s),
      find_schema_allowed_value_entry(k.to_s, v)
    )
  end
end