# frozen_string_literal: true

module RedmineUxToolkit
  module Services
    module NavigationLinks
      LINK_KEYS = %w[
        home
        my_page
        projects
        help
        administration
        my_account
        my_work
      ].freeze

      MENU_ITEM_KEYS = {
        home: 'home',
        my_page: 'my_page',
        projects: 'projects',
        help: 'help',
        administration: 'administration',
        my_account: 'my_account',
        ux_toolkit_my_work: 'my_work'
      }.freeze

      LABEL_KEYS = {
        'home' => :label_ux_toolkit_nav_home,
        'my_page' => :label_ux_toolkit_nav_my_page,
        'projects' => :label_ux_toolkit_nav_projects,
        'help' => :label_ux_toolkit_nav_help,
        'administration' => :label_ux_toolkit_nav_administration,
        'my_account' => :label_ux_toolkit_nav_my_account,
        'my_work' => :label_ux_toolkit_nav_my_work
      }.freeze

      module_function

      def visible_link_keys(settings = Setting.plugin_redmine_ux_toolkit || {})
        raw_value = settings['visible_navigation_links']
        return LINK_KEYS if raw_value.nil?

        values = Array(raw_value).map(&:to_s).reject(&:blank?)
        sanitized = values & LINK_KEYS
        values.empty? || sanitized.present? ? sanitized : LINK_KEYS
      end

      def visible?(menu_item_name, settings = Setting.plugin_redmine_ux_toolkit || {})
        link_key = MENU_ITEM_KEYS[menu_item_name.to_sym]
        return true unless link_key

        visible_link_keys(settings).include?(link_key)
      end
    end
  end
end
