class Avo::BaseAction

def action_name

def action_name
  return name if name.present?
  self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true)
end

def context

def context
  self.class.context
end

def download(path, filename)

def download(path, filename)
  response[:type] = :download
  response[:path] = path
  response[:filename] = filename
  self
end

def fail(text)

def fail(text)
  response[:message_type] = :alert
  response[:message] = text
  self
end

def form_data_attributes

def form_data_attributes
  # We can't respond with a file download from Turbo se we disable it on the form
  if may_download_file
    {turbo: false, remote: false}
  else
    {"turbo-frame": "_top", "action-target": "form"}
  end
end

def get_attributes_for_action

def get_attributes_for_action
  get_fields.map do |field|
    [field.id, field.value]
  end
    .to_h
end

def get_field_definitions

def get_field_definitions
  return [] if self.class.fields.blank?
  self.class.fields.map do |field|
    field.hydrate(action: self)
  end
end

def get_fields

def get_fields
  get_field_definitions.map do |field|
    field.hydrate(action: self, model: @model)
  end
    .select do |field|
    field.visible?
  end
end

def handle_action(**args)

def handle_action(**args)
  models, fields, current_user, resource = args.values_at(:models, :fields, :current_user, :resource)
  avo_fields = get_fields.map { |field| [field.id, field] }.to_h
  if fields.present?
    processed_fields = fields.to_unsafe_h.map do |name, value|
      [name, avo_fields[name.to_sym].resolve_attribute(value)]
    end
    processed_fields = processed_fields.to_h
  else
    processed_fields = {}
  end
  args = {
    fields: processed_fields,
    current_user: current_user,
    resource: resource
  }
  args[:models] = models unless standalone
  handle(**args)
  self
end

def initialize(model: nil, resource: nil, user: nil, view: nil)

def initialize(model: nil, resource: nil, user: nil, view: nil)
  self.class.model = model if model.present?
  self.class.resource = resource if resource.present?
  self.class.user = user if user.present?
  self.class.view = view if view.present?
  self.class.message ||= I18n.t("avo.are_you_sure_you_want_to_run_this_option")
  self.class.confirm_button_label ||= I18n.t("avo.run")
  self.class.cancel_button_label ||= I18n.t("avo.cancel")
  @response ||= {}
  @response[:message_type] ||= :notice
  @response[:message] ||= I18n.t("avo.action_ran_successfully")
end

def param_id

def param_id
  self.class.to_s.demodulize.underscore.tr "/", "_"
end

def redirect_to(path = nil, &block)

def redirect_to(path = nil, &block)
  response[:type] = :redirect
  response[:path] = if block.present?
    block
  else
    path
  end
  self
end

def reload

def reload
  response[:type] = :reload
  self
end

def submit_button_data_attributes

We can't respond with a file download from Turbo se we disable close the modal manually after a while (it's a hack, we know)
def submit_button_data_attributes
  if may_download_file
    {action: "click->modal#delayedClose"}
  else
    {}
  end
end

def succeed(text)

def succeed(text)
  response[:message_type] = :notice
  response[:message] = text
  self
end

def visible_in_view

def visible_in_view
  return true unless visible.present?
  instance_exec(resource: self.class.resource, view: view, &visible)
end