<commit_before>require 'node_presenter'
require 'gds_api/helpers'

class SmartAnswerPresenter
  include GdsApi::Helpers
  extend Forwardable
  include Rails.application.routes.url_helpers

  attr_reader :request, :params, :flow

  def_delegators :@flow, :need_id

  def initialize(request, flow)
    @request = request
    @params = request.params
    @flow = flow
  end

  def artefact
    @artefact ||= content_api.artefact(@flow.name)
  end

  def i18n_prefix
    "flow.#{@flow.name}"
  end

  def lookup_translation(key)
    I18n.translate!("#{i18n_prefix}.#{key}")
  rescue I18n::MissingTranslationData
    nil
  end

  def markdown_for(key)
    markdown = lookup_translation(key)
    markdown && Govspeak::Document.new(markdown).to_html.html_safe
  end

  def title
    lookup_translation(:title) || @flow.name.to_s.humanize
  end

  def subtitle
    lookup_translation(:subtitle)
  end

  def body
    markdown_for('body')
  end

  def devolved_body
    markdown_for('devolved_body')
  end

  def has_subtitle?
    !! subtitle
  end

  def has_body?
    !! body
  end

  def has_devolved_body?
    !! devolved_body
  end

  def has_meta_description?
    !! lookup_translation('meta.description')
  end

  def meta_description
    lookup_translation('meta.description')
  end

  def started?
    params.has_key?(:started)
  end

  def finished?
    current_node.outcome?
  end

  def current_state
    @current_state ||= @flow.process(all_responses)
  end

  def error
    if current_state.error.present?
      puts current_state.error.inspect
      if current_state.error.is_a?(Symbol)
        puts "This is a symbol!"
        e = current_node.translate!("errors.#{current_state.error.to_s}")
      elsif !current_state.error.blank?
        e = current_state.error.to_s
      else
        e = current_node.error_message || I18n.translate('flow.defaults.error_message')
      end
      puts e
      return e
    end
  end

  def collapsed_questions
    @flow.path(all_responses).map do |name|
      presenter_for(@flow.node(name))
    end
  end

  def presenter_for(node)
    presenter_class = case node
    when SmartAnswer::Question::Date
      DateQuestionPresenter<commit_msg>Error messages can be declared as strings (with interpolation) after the InvalidResponse declaration.<commit_after>require 'node_presenter'
require 'gds_api/helpers'

class SmartAnswerPresenter
  include GdsApi::Helpers
  extend Forwardable
  include Rails.application.routes.url_helpers

  attr_reader :request, :params, :flow

  def_delegators :@flow, :need_id

  def initialize(request, flow)
    @request = request
    @params = request.params
    @flow = flow
  end

  def artefact
    @artefact ||= content_api.artefact(@flow.name)
  end

  def i18n_prefix
    "flow.#{@flow.name}"
  end

  def lookup_translation(key)
    I18n.translate!("#{i18n_prefix}.#{key}")
  rescue I18n::MissingTranslationData
    nil
  end

  def markdown_for(key)
    markdown = lookup_translation(key)
    markdown && Govspeak::Document.new(markdown).to_html.html_safe
  end

  def title
    lookup_translation(:title) || @flow.name.to_s.humanize
  end

  def subtitle
    lookup_translation(:subtitle)
  end

  def body
    markdown_for('body')
  end

  def devolved_body
    markdown_for('devolved_body')
  end

  def has_subtitle?
    !! subtitle
  end

  def has_body?
    !! body
  end

  def has_devolved_body?
    !! devolved_body
  end

  def has_meta_description?
    !! lookup_translation('meta.description')
  end

  def meta_description
    lookup_translation('meta.description')
  end

  def started?
    params.has_key?(:started)
  end

  def finished?
    current_node.outcome?
  end

  def current_state
    @current_state ||= @flow.process(all_responses)
  end

  def error
    if current_state.error.present?
      if current_state.error.include?("::")
        current_node.error_message || I18n.translate('flow.defaults.error_message')
      else
        current_state.error
      end
    end
  end

  def collapsed_questions
    @flow.path(all_responses).map do |name|
      presenter_for(@flow.node(name))
    end
  end

  def presenter_for(node)
    presenter_class = case node
    when SmartAnswer::Question::Date
      DateQuestionPresenter
{'fraction_non_alphanumeric': 0.09163911195087387, 'fraction_numerical': 0.004723665564478035, 'mean_word_length': 3.352517985611511, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, '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': '5094608', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1387, 'n_words': 290}