<commit_before>module SectionTabHelper
  def available_section_tabs(context, dashboard_cards: false)
    AvailableSectionTabs.new(
      context, @current_user, @domain_root_account, session, dashboard_cards: dashboard_cards
    ).to_a
  end

  def section_tabs
    @section_tabs ||= begin
      if @context && available_section_tabs(@context).any?
        content_tag(:nav, {
          :role => 'navigation',
          :'aria-label' => 'context'
        }) do
          concat(content_tag(:ul, id: 'section-tabs') do
            available_section_tabs(@context).map do |tab|
              section_tab_tag(tab, @context, @active_tab)
            end
          end)
        end
      end
    end
    raw(@section_tabs)
  end

  def section_tab_tag(tab, context, active_tab)
    concat(SectionTabTag.new(tab, context, active_tab).to_html)
  end

  class AvailableSectionTabs
    DASHBOARD_CARD_TABS = [
      Course::TAB_DISCUSSIONS, Course::TAB_ASSIGNMENTS,
      Course::TAB_ANNOUNCEMENTS, Course::TAB_FILES
    ].freeze

    def initialize(context, current_user, domain_root_account, session, dashboard_cards: false)
      @context = context
      @current_user = current_user
      @domain_root_account = domain_root_account
      @session = session
      @dashboard_cards = dashboard_cards
    end
    attr_reader :context, :current_user, :domain_root_account, :session, :dashboard_cards

    def to_a
      return [] unless context.respond_to?(:tabs_available)

      Rails.cache.fetch(cache_key, expires_in: 1.hour) do
        new_collaborations_enabled = context.feature_enabled?(:new_collaborations) if context.respond_to?(:feature_enabled?)

        tabs = context.tabs_available(current_user, {
          session: session,
          root_account: domain_root_account
        }).select { |tab|
          tab_has_required_attributes?(tab)
        }

        if dashboard_cards
          tabs.select! { |tab| DASHBOARD_CARD_TABS.include?(tab[:id]) }
        else
          tabs.reject! { |tab|
            if tab_is?(tab, 'TAB_COLLABORATIONS')
              new_collaborations_enabled ||
                !Collaboration.any_collaborations_configured?(@context)
            elsif tab_is?(tab, 'TAB_COLLABORATIONS_NEW')
              !new_collaborations_enabled
            elsif tab_is?(tab, 'TAB_CONFERENCES')
              !WebConference.config
            end
          }
        end

        tabs
      end
    end

    private
    def cache_key
      [ context, current_user, domain_root_account,
        Lti::NavigationCache.new(domain_root_account),
        "section_tabs_hash", I18n.locale
      ].cache_key
    end

    def tab_has_required_attributes?(tab)
      tab[:href] && tab[:label]
    end

    def tab_is?(tab, const_name)
      context.class.const_defined?(const_name) &&
        tab[:id] == context.class.const_get(const_name)
    end
  end

  class SectionTabTag
    include ActionView::Context
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::TextHelper<commit_msg>bump section tab cache key<commit_after>module SectionTabHelper
  def available_section_tabs(context, dashboard_cards: false)
    AvailableSectionTabs.new(
      context, @current_user, @domain_root_account, session, dashboard_cards: dashboard_cards
    ).to_a
  end

  def section_tabs
    @section_tabs ||= begin
      if @context && available_section_tabs(@context).any?
        content_tag(:nav, {
          :role => 'navigation',
          :'aria-label' => 'context'
        }) do
          concat(content_tag(:ul, id: 'section-tabs') do
            available_section_tabs(@context).map do |tab|
              section_tab_tag(tab, @context, @active_tab)
            end
          end)
        end
      end
    end
    raw(@section_tabs)
  end

  def section_tab_tag(tab, context, active_tab)
    concat(SectionTabTag.new(tab, context, active_tab).to_html)
  end

  class AvailableSectionTabs
    DASHBOARD_CARD_TABS = [
      Course::TAB_DISCUSSIONS, Course::TAB_ASSIGNMENTS,
      Course::TAB_ANNOUNCEMENTS, Course::TAB_FILES
    ].freeze

    def initialize(context, current_user, domain_root_account, session, dashboard_cards: false)
      @context = context
      @current_user = current_user
      @domain_root_account = domain_root_account
      @session = session
      @dashboard_cards = dashboard_cards
    end
    attr_reader :context, :current_user, :domain_root_account, :session, :dashboard_cards

    def to_a
      return [] unless context.respond_to?(:tabs_available)

      Rails.cache.fetch(cache_key, expires_in: 1.hour) do
        new_collaborations_enabled = context.feature_enabled?(:new_collaborations) if context.respond_to?(:feature_enabled?)

        tabs = context.tabs_available(current_user, {
          session: session,
          root_account: domain_root_account
        }).select { |tab|
          tab_has_required_attributes?(tab)
        }

        if dashboard_cards
          tabs.select! { |tab| DASHBOARD_CARD_TABS.include?(tab[:id]) }
        else
          tabs.reject! { |tab|
            if tab_is?(tab, 'TAB_COLLABORATIONS')
              new_collaborations_enabled ||
                !Collaboration.any_collaborations_configured?(@context)
            elsif tab_is?(tab, 'TAB_COLLABORATIONS_NEW')
              !new_collaborations_enabled
            elsif tab_is?(tab, 'TAB_CONFERENCES')
              !WebConference.config
            end
          }
        end

        tabs
      end
    end

    private
    def cache_key
      [ context, current_user, domain_root_account,
        Lti::NavigationCache.new(domain_root_account),
        "section_tabs_hash2", I18n.locale
      ].cache_key
    end

    def tab_has_required_attributes?(tab)
      tab[:href] && tab[:label]
    end

    def tab_is?(tab, const_name)
      context.class.const_defined?(const_name) &&
        tab[:id] == context.class.const_get(const_name)
    end
  end

  class SectionTabTag
    include ActionView::Context
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::TextHelper
{'fraction_non_alphanumeric': 0.0915702479338843, 'fraction_numerical': 0.0011570247933884298, 'mean_word_length': 2.7607209446861405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '951374', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1822, 'n_words': 333}