<commit_before># common constants and methods for MWInteractive and ManagedInteractive

module BaseInteractive
  DEFAULT_CLICK_TO_PLAY_PROMPT = "Click here to start the interactive."

  def storage_key
    if name.present?
      "#{interactive_page.lightweight_activity.id}_#{interactive_page.id}_#{id}_#{self.class.to_s.underscore.gsub(/\//, '_')}_#{name.downcase.gsub(/ /, '_')}"
    else
      "#{interactive_page.lightweight_activity.id}_#{interactive_page.id}_#{id}_#{self.class.to_s.underscore.gsub(/\//, '_')}"
    end
  end

  def portal_hash
    result = report_service_hash
    # When Portal receives iframe_interactive, it expects:
    # id, name, url, native_width, native_height, is_required, show_in_featured_question_report, display_in_iframe.
    # Add / map property names that are different in report_service_hash.

    result[:native_width] = result[:width]
    result[:native_height] = result[:height]
    result[:is_required] = !!result[:required]
    # If name is not available, but prompt is, this should make Portal details report a bit more readable.
    unless result[:name].present?
      result[:name] = result[:prompt]
    end

    # Open response and multiple choice properties are the same as in report_service_hash and/or properties mapped above.
    # Portal expects less fields that will be actually sent, but it doesn't seem to cause any problems.
    result
  end

  def parsed_authored_state
    JSON.parse(authored_state).symbolize_keys rescue {}
  end

  def report_service_hash
    # Expected metadata format can be checked in lara-typescript/interactive-api-client/metadata-types.ts:
    # IAuthoring<...>Metadata interfaces.<commit_msg>Fix publication when prompt is empty<commit_after># common constants and methods for MWInteractive and ManagedInteractive

module BaseInteractive
  DEFAULT_CLICK_TO_PLAY_PROMPT = "Click here to start the interactive."

  def storage_key
    if name.present?
      "#{interactive_page.lightweight_activity.id}_#{interactive_page.id}_#{id}_#{self.class.to_s.underscore.gsub(/\//, '_')}_#{name.downcase.gsub(/ /, '_')}"
    else
      "#{interactive_page.lightweight_activity.id}_#{interactive_page.id}_#{id}_#{self.class.to_s.underscore.gsub(/\//, '_')}"
    end
  end

  def portal_hash
    result = report_service_hash
    # When Portal receives iframe_interactive, it expects:
    # id, name, url, native_width, native_height, is_required, show_in_featured_question_report, display_in_iframe.
    # Add / map property names that are different in report_service_hash.

    result[:native_width] = result[:width]
    result[:native_height] = result[:height]
    result[:is_required] = !!result[:required]
    if result[:prompt].nil?
      # Portal doesn't like nil prompt. Publication fails with:
      # HTTP Unprocessable Entity 422
      # NoMethodError: undefined method `gsub' for nil:NilClass
      # Details: https://www.pivotaltracker.com/story/show/173400823
      result[:prompt] = ""
    end

    # If name is not available, but prompt is, this should make Portal details report a bit more readable.
    unless result[:name].present?
      result[:name] = result[:prompt]
    end

    # Open response and multiple choice properties are the same as in report_service_hash and/or properties mapped above.
    # Portal expects less fields that will be actually sent, but it doesn't seem to cause any problems.
    result
  end

  def parsed_authored_state
    JSON.parse(authored_state).symbolize_keys rescue {}
  end

  def report_service_hash
    # Expected metadata format can be checked in lara-typescript/interactive-api-client/metadata-types.ts:
    # IAuthoring<...>Metadata interfaces.
{'fraction_non_alphanumeric': 0.09869494290375204, 'fraction_numerical': 0.0032626427406199023, 'mean_word_length': 4.081491712707182, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '3687764', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1090, 'n_words': 300}