# frozen_string_literal: true

# @param type [String] Classic notification type `error`, `alert` and `info` + custom `success`
# @param data [String, Hash] `String` for backward compatibility,
#   `Hash` for the new functionality `{title: '', body: '', timeout: 5, countdown: false, action: { url: '', method: '', name: ''}}`.
#   The `title` attribute for `Hash` is mandatory.
class NotificationComponent < ViewComponent::Base
  def initialize(type:, data:)
    @type = type
    @data = prepare_data(data)
    @icon_name = icon_name
    @icon_color_class = icon_color_class

    @data[:timeout] ||= 5
    @data[:action][:method] ||= "get" if @data[:action]
  end

  private

  def prepare_data(data)
    case data
    when Hash
      data
    else
      { title: data }
    end
  end

  def icon_name
    case @type
    when 'success'
      'check-circle'
    when 'error'
      'alert-octagon'
    when 'alert'
      'alert-octagon'
    else
      'info'
    end
  end

  def icon_color_class
    case @type
    when 'success'
      'text-emerald-500'
    when 'error'
      'text-rose-600'
    when 'alert'
      'text-rose-600'
    else
      'text-gray-400'
    end
  end
end
