<commit_before>'use strict'

###*
 # @ngdoc function
 # @name <%= code_suitable_name %>.controller:MainCtrl
 # @description
 # # MainCtrl
 # Controller of the <%= code_suitable_name %>
###
<%- def boolean_expression_to_js(expression)
  case expression
  when Therapeutor::BooleanExpression::BooleanConstant then expression.value.to_s
  when Therapeutor::BooleanExpression::Variable then "answers.#{variable(expression.name).code_suitable_name}"
  when Therapeutor::BooleanExpression::NotExpression then  "!answers.#{variable(expression.operand.name).code_suitable_name}"
  when Therapeutor::BooleanExpression::AndExpression then and_to_js(expression)
  when Therapeutor::BooleanExpression::OrExpression then or_to_js(expression)
  end
end
def and_to_js(expression)
  "(#{expression.operands.map do |operand|
    boolean_expression_to_js(operand)
  end.join(' and ')})"
end
def or_to_js(expression)
  expression.operands.map do |operand|
    boolean_expression_to_js(operand)
  end.join(' or ')
end-%>

angular.module '<%= code_suitable_name %>'
  .controller 'MainCtrl', ($scope) ->
    $scope.answers = {}
  <%- therapies.each do |therapy| -%>
    <%= therapy.code_suitable_name %> =
    <%- therapy.properties.each do |property, value| -%>
        <%= property %>: <%= value %>
    <%- end -%>
    <%- recommendation_levels.each do |recommendation_level| -%>
        <%= recommendation_level.code_suitable_name %>: (answers) ->
            <%- recommendation_level.previously_discarded_levels.each do |previously_discarded_level| -%>
            !@<%= previously_discarded_level.code_suitable_name %>(answers) and
            <%- end -%>
            <%- level_condition = therapy.level_conditions.detect{|lc| lc.level == recommendation_level} -%>
            <%- if level_condition.nil? or level_condition.conditions.empty? -%>
            <%= recommendation_level.last ? 'true' : 'false' %>
            <%- else -%>
            (<%= level_condition.conditions.map(&:condition).map do |condition|
            boolean_expression_to_js(condition.to_dnf)
            end.join(' or ') %>)
    <%- end -%>
    <%- end -%>
    <%- preference_orders.select do |order|
      order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
    end.each do |order| -%>
        <%= order.code_suitable_name %>: (answers) ->
          <%- order_condition = therapy.order_conditions.detect{|oc| oc.preference_order == order} -%>
          <%- conditions = order_condition ? order_condition.conditions : [] -%>
            [<%= conditions.map(&:condition).map do |condition|
                boolean_expression_to_js(condition.to_dnf)
              end.join(', ') %>].filter((condition) ->
              condition
            ).length
    <%- end -%>
  <%- end -%>
    $scope.therapies=
  <%- therapies.each do |therapy| -%>
      <%= therapy.code_suitable_name %>: <%= therapy.code_suitable_name%>
  <%- end -%>
    $scope.defaultTherapyOrder = [<%= therapies.map(&:code_suitable_name).join(', ') %>]
    $scope.showCompleteEvaluation = false
    $scope.visible = (therapy) ->
      therapy == $scope.recommendedTherapy()
    $scope.recommendedTherapy = () ->
      $scope.suitableTherapies()[0]
    $scope.recommendationLevel = (therapy) ->
  <%- recommendation_levels.each.with_index do |recommendation_level, i| -%>
      return <%= i %> if therapy.<%=recommendation_level.code_suitable_name%>($scope.answers)
  <%- end -%>
      return -1
    $scope.suitableTherapies = () ->
      $scope.defaultTherapyOrder.filter((therapy) ->
        <%= recommendation_levels.select(&:banning).map{|level| "not therapy.#{level.code_suitable_name}($scope.answers)"}.join(' and ')%>
      ).sort((a, b) ->
        a_level = $scope.recommendationLevel(a)
        b_level = $scope.recommendationLevel(b)
        return b_level - a_level if a_level != b_level
  <%- preference_orders.each do |order|
    order_evaluation = if order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
      "#{order.code_suitable_name}($scope.answers)"
    else
      "#{order.code_suitable_name}"
    end -%>
        a_level = <%= order.descending ? '-' : '' %>a.<%= order_evaluation%>
        b_level = <%= order.descending ? '-' : '' %>b.<%= order_evaluation%>
        return b_level - a_level if a_level != b_level
  <%- end -%>
        return 0
      )
  <%- preference_orders.each do |order| -%>
    $scope.drawResolvedBy<%= order.code_suitable_name.capitalize%> = () ->
      suitableTherapies = $scope.suitableTherapies()
      return false if suitableTherapies.length < 2
      recommended = suitableTherapies[0]
      second_best = suitableTherapies[1]
      return false if $scope.recommendationLevel(recommended) != $scope.recommendationLevel(second_best)
    <%- order.previous_preference_orders.each do |previous_order|
      order_evaluation = if previous_order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
        "#{order.code_suitable_name}($scope.answers)"
      else
        "#{order.code_suitable_name}"
      end -%>
      return false if recommended.<%= order_evaluation%> != second_best.<%= order_evaluation%>
    <%- end -%>
    <%- order_evaluation = if order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
      "#{order.code_suitable_name}($scope.answers)"
    else
      "#{order.code_suitable_name}"
    end -%>
      return recommended.<%= order_evaluation%> != second_best.<%= order_evaluation %>
  <%- end -%><commit_msg>Fixes therapy sorting bug in template's coffeescript<commit_after>'use strict'

###*
 # @ngdoc function
 # @name <%= code_suitable_name %>.controller:MainCtrl
 # @description
 # # MainCtrl
 # Controller of the <%= code_suitable_name %>
###
<%- def boolean_expression_to_js(expression)
  case expression
  when Therapeutor::BooleanExpression::BooleanConstant then expression.value.to_s
  when Therapeutor::BooleanExpression::Variable then "answers.#{variable(expression.name).code_suitable_name}"
  when Therapeutor::BooleanExpression::NotExpression then  "!answers.#{variable(expression.operand.name).code_suitable_name}"
  when Therapeutor::BooleanExpression::AndExpression then and_to_js(expression)
  when Therapeutor::BooleanExpression::OrExpression then or_to_js(expression)
  end
end
def and_to_js(expression)
  "(#{expression.operands.map do |operand|
    boolean_expression_to_js(operand)
  end.join(' and ')})"
end
def or_to_js(expression)
  expression.operands.map do |operand|
    boolean_expression_to_js(operand)
  end.join(' or ')
end-%>

angular.module '<%= code_suitable_name %>'
  .controller 'MainCtrl', ($scope) ->
    $scope.answers = {}
  <%- therapies.each do |therapy| -%>
    <%= therapy.code_suitable_name %> =
    <%- therapy.properties.each do |property, value| -%>
        <%= property %>: <%= value %>
    <%- end -%>
    <%- recommendation_levels.each do |recommendation_level| -%>
        <%= recommendation_level.code_suitable_name %>: (answers) ->
            <%- recommendation_level.previously_discarded_levels.each do |previously_discarded_level| -%>
            !@<%= previously_discarded_level.code_suitable_name %>(answers) and
            <%- end -%>
            <%- level_condition = therapy.level_conditions.detect{|lc| lc.level == recommendation_level} -%>
            <%- if level_condition.nil? or level_condition.conditions.empty? -%>
            <%= recommendation_level.last ? 'true' : 'false' %>
            <%- else -%>
            (<%= level_condition.conditions.map(&:condition).map do |condition|
            boolean_expression_to_js(condition.to_dnf)
            end.join(' or ') %>)
    <%- end -%>
    <%- end -%>
    <%- preference_orders.select do |order|
      order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
    end.each do |order| -%>
        <%= order.code_suitable_name %>: (answers) ->
          <%- order_condition = therapy.order_conditions.detect{|oc| oc.preference_order == order} -%>
          <%- conditions = order_condition ? order_condition.conditions : [] -%>
            [<%= conditions.map(&:condition).map do |condition|
                boolean_expression_to_js(condition.to_dnf)
              end.join(', ') %>].filter((condition) ->
              condition
            ).length
    <%- end -%>
  <%- end -%>
    $scope.therapies=
  <%- therapies.each do |therapy| -%>
      <%= therapy.code_suitable_name %>: <%= therapy.code_suitable_name%>
  <%- end -%>
    $scope.defaultTherapyOrder = [<%= therapies.map(&:code_suitable_name).join(', ') %>]
    $scope.showCompleteEvaluation = false
    $scope.visible = (therapy) ->
      therapy == $scope.recommendedTherapy()
    $scope.recommendedTherapy = () ->
      $scope.suitableTherapies()[0]
    $scope.recommendationLevel = (therapy) ->
  <%- recommendation_levels.each.with_index do |recommendation_level, i| -%>
      return <%= i %> if therapy.<%=recommendation_level.code_suitable_name%>($scope.answers)
  <%- end -%>
      return -1
    $scope.suitableTherapies = () ->
      $scope.defaultTherapyOrder.filter((therapy) ->
        <%= recommendation_levels.select(&:banning).map{|level| "not therapy.#{level.code_suitable_name}($scope.answers)"}.join(' and ')%>
      ).sort((a, b) ->
        a_level = $scope.recommendationLevel(a)
        b_level = $scope.recommendationLevel(b)
        return b_level - a_level if a_level != b_level
  <%- preference_orders.each do |order|
    order_evaluation = if order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
      "#{order.code_suitable_name}($scope.answers)"
    else
      "#{order.code_suitable_name}"
    end -%>
        a_level = <%= order.descending ? '-' : '' %>a.<%= order_evaluation%>
        b_level = <%= order.descending ? '-' : '' %>b.<%= order_evaluation%>
        return b_level - a_level if a_level != b_level
  <%- end -%>
        return 0
      )
  <%- preference_orders.each do |order| -%>
    $scope.drawResolvedBy<%= order.code_suitable_name.capitalize%> = () ->
      suitableTherapies = $scope.suitableTherapies()
      return false if suitableTherapies.length < 2
      recommended = suitableTherapies[0]
      second_best = suitableTherapies[1]
      return false if $scope.recommendationLevel(recommended) != $scope.recommendationLevel(second_best)
    <%- order.previous_preference_orders.each do |previous_order|
      order_evaluation = if previous_order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
        "#{previous_order.code_suitable_name}($scope.answers)"
      else
        "#{previous_order.code_suitable_name}"
      end -%>
      return false if recommended.<%= order_evaluation%> != second_best.<%= order_evaluation%>
    <%- end -%>
    <%- order_evaluation = if order.is_a?(Therapeutor::Questionnaire::Therapy::ConditionCountOrder)
      "#{order.code_suitable_name}($scope.answers)"
    else
      "#{order.code_suitable_name}"
    end -%>
      return recommended.<%= order_evaluation%> != second_best.<%= order_evaluation %>
  <%- end -%>
{'fraction_non_alphanumeric': 0.15500136524984073, 'fraction_numerical': 0.0010921998725766816, 'mean_word_length': 3.4830681354549164, 'pattern_counts': {'":': 0, '<': 115, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7480311', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3508, 'n_words': 524}