module Kameleoon::Targeting::TreeBuilder

def create_first_level(conditions_data_json)

def create_first_level(conditions_data_json)
  unless conditions_data_json['firstLevel'].empty?
    left_first_level = conditions_data_json['firstLevel'].shift
    left_child = create_second_level(left_first_level)
    unless conditions_data_json['firstLevel'].empty?
      or_operator = conditions_data_json['firstLevelOrOperators'].shift
      if or_operator
        return Tree.new(or_operator, left_child, create_first_level(conditions_data_json))
      end
      right_first_level = conditions_data_json['firstLevel'].shift
      right_child = create_second_level(right_first_level)
      tree = Tree.new(or_operator, left_child, right_child)
      unless conditions_data_json['firstLevel'].empty?
        return Tree.new(conditions_data_json['firstLevelOrOperators'].shift, tree, create_first_level(conditions_data_json))
      end
      return tree
    end
    return left_child
  end
  nil
end

def create_second_level(conditions_data_json)

def create_second_level(conditions_data_json)
  unless conditions_data_json.nil? or conditions_data_json['conditions'].empty?
    left_child = Tree.new
    left_child.condition = get_condition(conditions_data_json['conditions'].shift)
    unless conditions_data_json['conditions'].empty?
      or_operator = conditions_data_json['orOperators'].shift
      if or_operator
        return Tree.new(or_operator, left_child, create_second_level(conditions_data_json))
      end
      right_child = Tree.new
      right_child.condition = get_condition(conditions_data_json['conditions'].shift)
      tree = Tree.new(or_operator, left_child, right_child)
      unless conditions_data_json['conditions'].empty?
        return Tree.new(conditions_data_json['orOperators'].shift, tree, create_second_level(conditions_data_json))
      end
      return tree
    end
    return left_child
  end
  nil
end

def create_tree(conditions_data_json)

def create_tree(conditions_data_json)
  return nil if conditions_data_json.nil?
  Logging::KameleoonLogger.debug('CALL: TreeBuilder.create_tree(conditions_data_json: %s)',
                                 conditions_data_json)
  if conditions_data_json['firstLevel'].empty?
    conditions_data_json['firstLevelOrOperators'] = []
  end
  tree = create_first_level(conditions_data_json)
  Logging::KameleoonLogger.debug('RETURN: TreeBuilder.create_tree(conditions_data_json: %s) -> (tree)',
                                 conditions_data_json)
  tree
end