# frozen_string_literal: true

class API
	def self.for(customer)
		EMPromise.all([
			sgx_jmp?(customer),
			api_version(customer)
		]).then do |is_jmp, api|
			is_jmp ? JMP.new : api
		end
	end

	def self.sgx_jmp?(customer)
		key = "catapult_cred-customer_#{customer.customer_id}@jmp.chat"
		REDIS.exists(key).then { |is_sgx| is_sgx == 1 }
	end

	def self.api_version(customer)
		REDIS.lindex("catapult_cred-#{customer.jid}", 0).then do |api|
			case api
			when CONFIG.dig(:creds, :account)
				V2.new
			else
				new
			end
		end
	end

	class V2 < API
		def to_s
			"v2"
		end
	end

	class JMP < V2
		def to_s
			"sgx-jmp"
		end
	end

	def to_s
		"not JMP"
	end
end
