1# frozen_string_literal: true
 2
 3require "json"
 4
 5require_relative "geo_code_repo"
 6
 7class AreaCodeRepo
 8	def initialize(db: DB, geo_code_repo: GeoCodeRepo.new)
 9		@db = db
10		@geo_code_repo = geo_code_repo
11	end
12
13	def find(q, limit: 3)
14		@geo_code_repo.find(q).then { |geo|
15			@db.query_defer(<<~SQL, [geo.country, geo.sql_point, limit])
16				SELECT area_code FROM area_codes
17				WHERE country=$1
18				ORDER BY location <-> $2
19				LIMIT $3
20			SQL
21		}.then { |rows|
22			rows.map { |row| row["area_code"] }
23		}
24	end
25end