# frozen_string_literal: true require_relative "form_to_h" class ConfigureCallsForm using FormToH def initialize(customer) @customer = customer end def render FormTemplate.render("configure_calls", customer: @customer) end def parse(form) params = form.to_h {}.tap do |result| result[:fwd] = parse_fwd(params["fwd"]) if params.key?("fwd") if params.key?("voicemail_transcription") result[:transcription_enabled] = ["1", "true"].include?(params["voicemail_transcription"]) end result[:lidb_name] = params["lidb_name"].strip if lidb_guard(params) end end protected def lidb_guard(params) !params["lidb_name"].to_s.strip.empty? && !@customer.tndetails.dig(:features, :lidb) end def parse_fwd(fwd_from_form) @customer.fwd.with(fwd_from_form.each_with_object({}) { |(var, val), args| args[var.to_sym] = var == "voicemail_enabled" ? ["1", "true"].include?(val) : val&.strip }) end end