# frozen_string_literal: true

# Helper for views
module ViewsHelper
  def dropdown_link(name, menu_links)
    content_tag(:div, class: 'dropdown no-arrow') do
      concat link_for('#', name)
      concat dropdown_menu(menu_links)
    end
  end

  def link_for(href, text)
    link_to(text, href, class: 'dropdown-toggle', id: 'ddMenuLink',
                        role: 'button', data: { toggle: 'dropdown' },
                        aria: { haspopup: 'true', expanded: 'false' })
  end

  def dropdown_menu(menu_links)
    content_tag(:div, class: 'dropdown no-arrow') do
      menu_links.each do |ml|

      end
    end
  end

  def menu_item(item)
    if item.nil?
      # divider
      tag.div class: 'dropdown-divider'
    elsif item.class.to_s == 'String'
      # header
      content_tag :h6, I18n.t(item), class: 'dropdown-header'
    elsif item.class.to_s == 'Array'
      # link (menu item)
      icon = item[2].nil? ? '' : "<i class=\"far fa-fw #{item[2]}\"></i>"
      nm = I18n.t(sub[0])
      link_to(raw("#{icon} #{nm}"), sub[1], class: 'dropdown-item')
    end
  end

  def paginator(object)
    paginate(object, remote: true, theme: 'twitter-bootstrap-4',
                     pagination_class: 'justify-content-center',
                     nav_class: 'pg-right', params: normalize_pagination_params)
  end

  def normalize_pagination_params
    params.to_unsafe_h.inject({}) do |params_hash, p|
      params_hash[p[0]] = nil unless p[0] == 'controller' || p[0] == 'search'
      params_hash
    end
  end

=begin
<div class="dropdown no-arrow">
  <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <i class="fas fa-ellipsis-v fa-sm fa-fw text-gray-400"></i>
  </a>
  <div class="dropdown-menu dropdown-menu-right shadow animated--fade-in" aria-labelledby="dropdownMenuLink">
    <div class="dropdown-header">Dropdown Header:</div>
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <div class="dropdown-divider"></div>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>
=end
end
