<commit_before>module Kaui
  module PluginHelper
    # including plugin that are installed
    def plugin_repository
      plugins = []
      plugin_repository = Kaui::AdminTenant::get_plugin_repository
      installed_plugins = installed_plugins()

      plugin_repository.each_pair do |key, info|
        found_plugin = installed_plugins.reject! { |p| p.plugin_key.eql?(key.to_s) }
        plugins << {
            plugin_key: plugin_key(key.to_s, info),
            plugin_name: plugin_name(key.to_s, info),
            plugin_type: info[:type],
            installed: !found_plugin.nil?
        }
      end

      installed_plugins.each do |plugin|
        plugins << {
            plugin_key: plugin.plugin_key,
            plugin_name: plugin.plugin_name,
            plugin_type: nil,
            installed: true
        }
      end

      plugins.sort! { |a,b| a[:plugin_key] <=> b[:plugin_key] && b[:installed].to_s <=> a[:installed].to_s }
      plugins
    end

    private

      def plugin_name(key, info)
        if info[:artifact_id].nil?
          "killbill-#{key}"
        else
          "killbill-#{info[:artifact_id].gsub('killbill-','').gsub('-plugin','')}"
        end
      end

      def plugin_key(key, info)
        # hack:: replace paypal key with paypal_express, to set configuration and allow the ui to find the right configuration inputs
        if key.eql?('paypal')
          'paypal_express'
        else
          "#{key}"
        end
      end

      def installed_plugins
        nodes_info = KillBillClient::Model::NodesInfo.nodes_info(Kaui.current_tenant_user_options(current_user, session)) || []
        plugins_info = nodes_info.first.plugins_info || []

        plugins_info.select { |plugin| !plugin.version.nil?}
      end
  end
end<commit_msg>Issue#165: De-duplicate Installed plugins list in the drop down, fix order<commit_after>module Kaui
  module PluginHelper
    # including plugin that are installed
    def plugin_repository
      plugins = []
      plugin_repository = Kaui::AdminTenant::get_plugin_repository
      installed_plugins = installed_plugins()

      plugin_repository.each_pair do |key, info|
        plugins << {
            plugin_key: plugin_key(key.to_s, info),
            plugin_name: plugin_name(key.to_s, info),
            plugin_type: info[:type],
            installed: false
        }
      end

      plugins.sort! { |a,b| a[:plugin_key] <=> b[:plugin_key] }
      plugins.each {|plugin| installed_plugins << plugin }

      installed_plugins
    end

    private

      def plugin_name(key, info)
        if info[:artifact_id].nil?
          "killbill-#{key}"
        else
          "killbill-#{info[:artifact_id].gsub('killbill-','').gsub('-plugin','')}"
        end
      end

      def plugin_key(key, info)
        # hack:: replace paypal key with paypal_express, to set configuration and allow the ui to find the right configuration inputs
        if key.eql?('paypal')
          'paypal_express'
        else
          "#{key}"
        end
      end

      def installed_plugins
        installed_plugins = []
        nodes_info = KillBillClient::Model::NodesInfo.nodes_info(Kaui.current_tenant_user_options(current_user, session)) || []
        plugins_info = nodes_info.first.plugins_info || []

        plugins_info.each do |plugin|
          next if plugin.version.nil?
          installed_plugins << {
              plugin_key: Kaui::AdminTenant.rewrite_plugin_key(plugin.plugin_key),
              plugin_name: plugin.plugin_name,
              plugin_type: nil,
              installed: true
          }
        end

        installed_plugins.sort! { |a,b| a[:plugin_key] <=> b[:plugin_key] }
      end
  end
end
{'fraction_non_alphanumeric': 0.10526315789473684, 'fraction_numerical': 0.0008097165991902834, 'mean_word_length': 2.303030303030303, 'pattern_counts': {'":': 0, '<': 17, '<?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': '2955250', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1085, 'n_words': 232}