module WebhooksHelper
  ACTION_LABELS = {
    card_published: "Card added",
    card_title_changed: "Card title changed",
    card_collection_changed: "Card collection changed",
    comment_created: "Comment added",
    card_assigned: "Card assigned",
    card_unassigned: "Card unassigned",
    card_triaged: "Card column changed",
    card_closed: "Card moved to “Done”",
    card_reopened: "Card reopened",
    card_postponed: "Card moved to “Not Now”",
    card_auto_postponed: "Card auto-closed as “Not Now”",
    card_sent_back_to_triage: "Card moved back to “Maybe?”"
  }.with_indifferent_access.freeze

  def webhook_action_options(actions = Webhook::PERMITTED_ACTIONS)
    ACTION_LABELS.select { |key, _| actions.include?(key.to_s) }
  end

  def webhook_action_label(action)
    ACTION_LABELS[action] || action.to_s.humanize
  end

  def link_to_webhooks(collection, &)
    link_to collection_webhooks_path(collection_id: collection),
        class: [ "btn", { "btn--reversed": collection.webhooks.any? } ],
        data: { controller: "tooltip" } do
      icon_tag("world") + tag.span("Webhooks", class: "for-screen-reader")
    end
  end
end
