module GdsApi::TestHelpers::Mapit

def stub_mapit_has_a_postcode_and_areas(postcode, coords, areas)

def stub_mapit_has_a_postcode_and_areas(postcode, coords, areas)
  response = {
    "wgs84_lat" => coords.first,
    "wgs84_lon" => coords.last,
    "postcode"  => postcode,
  }
  area_response = Hash[areas.map.with_index { |area, i|
    [i, {
      "codes" => {
        "ons" => area["ons"],
        "gss" => area["gss"],
        "govuk_slug" => area["govuk_slug"],
      },
      "name" => area["name"],
      "type" => area["type"],
      "country_name" => area["country_name"],
    }]
  }]
  stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/" + postcode.tr(" ", "+") + ".json")
    .to_return(body: response.merge("areas" => area_response).to_json, status: 200)
  stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/partial/" + postcode.split(" ").first + ".json")
    .to_return(body: response.to_json, status: 200)
end