defmodule Wax.Messages.Interactive.Section do @doc """ Interactive Actions sections struct """ alias Wax.Messages.Interactive.Section.Row @type t :: %__MODULE__{ product_items: [map()], rows: [map()], title: String.t() } @max_title_length 24 @max_rows 10 @derive Jason.Encoder defstruct [ :product_items, {:rows, []}, :title ] @doc """ Creates a new Interactive Action Section object """ @spec new :: __MODULE__.t() def new do %__MODULE__{} end @doc """ Sets the title for the Section """ @spec put_title(Module.t(), String.t()) :: __MODULE__.t() def put_title(%__MODULE__{} = section, title) do if String.length(title) > @max_title_length do raise "A Section title cannot have more than 24 characters" end %{section | title: title} end @doc """ Adds a row to the Section """ @spec add_row(__MODULE__.t(), String.t(), String.t(), String.t()) :: __MODULE__.t() def add_row(%__MODULE__{rows: rows} = section, row_id, row_title, row_description \\ nil) do row = %Row{ id: row_id, title: row_title, description: row_description } %{section | rows: [row | rows]} end @doc """ Validates that the Section struct follows the Cloud API requirements """ @spec validate(__MODULE__.t()) :: :ok | {:error, String.t()} def validate(%__MODULE__{rows: rows}) when length(rows) > @max_rows do {:error, "A Section cannot have more than 10 rows"} end def validate(%__MODULE__{rows: rows}) do rows |> Enum.map(&Row.validate/1) |> Enum.find(fn :ok -> false {:error, _} -> true end) |> case do nil -> :ok {:error, error} -> {:error, error} end end end