# frozen_string_literal: true

class SIMKind
	def initialize(variant)
		@klass =
			case variant
			when "sim"
				SIMOrder
			when "esim"
				SIMOrder::ESIM
			else
				raise "Unknown SIM kind"
			end
		@variant = variant
	end

	def self.from_form(form)
		new(form.field("sim_kind")&.value)
	end

	# @param [Customer] customer
	# @raise [ArgumentError] If matching `CONFIG[:sims]`
	# 	key not found for `@variant` and customer
	def get_processor(customer)
		cfg = cfg(customer.currency)
		raise ArgumentError, "Invalid config" unless cfg

		@klass.for(customer, **cfg)
	end

	# @return [String] The result of either
	# 	SIMOrder.label or SIMOrder::ESIM.label
	def label
		@klass.label
	end

	# @param [Customer] customer
	# @return [Float, NilClass] Returns nil if `customer.currency`
	# 	is nil, or if @variant is malformed.
	def price(customer)
		cfg = cfg(customer.currency || :USD)
		cfg[:price] / 100.to_d
	end

protected

	# @param [Symbol] currency One of the currencies from
	# 	CONFIG[:plans] entries
	def cfg(currency)
		CONFIG.dig(:sims, @variant.to_sym, currency)
	end
end
