# frozen_string_literal: true

module UxToolkitIssueHintsHelper
  def ux_toolkit_issue_hints(issue)
    return [] unless issue&.visible?(User.current)

    hints = []
    hints << hint(l(:text_ux_toolkit_hint_issue_overdue), :warning) if ux_toolkit_issue_overdue?(issue)
    hints << hint(l(:text_ux_toolkit_hint_issue_due_today), :warning) if ux_toolkit_issue_due_today?(issue)
    hints << hint(l(:text_ux_toolkit_hint_issue_stale, count: ux_toolkit_issue_stale_days(issue)), :information) if ux_toolkit_issue_stale?(issue)
    hints << hint(l(:text_ux_toolkit_hint_issue_unassigned), :information) if ux_toolkit_issue_unassigned?(issue)

    open_checklist_items = ux_toolkit_open_checklist_item_count(issue)
    if open_checklist_items.positive?
      hints << hint(l(:text_ux_toolkit_hint_open_checklist_items, count: open_checklist_items), :action, '#ux-toolkit-checklist')
    end

    open_subtasks = ux_toolkit_open_subtask_count(issue)
    hints << hint(l(:text_ux_toolkit_hint_open_subtasks, count: open_subtasks), :action, '#issue_tree') if open_subtasks.positive?

    hints
  end

  private

  def hint(text, kind, target = nil)
    {text: text, kind: kind, target: target}
  end

  def ux_toolkit_open_checklist_item_count(issue)
    return 0 unless RedmineUxToolkit::Services::Checklists.enabled?

    RedmineUxToolkit::Services::Checklists.open_item_count_for(issue)
  end

  def ux_toolkit_open_subtask_count(issue)
    RedmineUxToolkit::Services::IssueCloseGuards.open_subtask_count_for(issue)
  end
end
