# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.Function do
  @moduledoc """
  Function represents a resolver provided by a Module.

  A function always evaluates against a parent object and is given a set of named arguments.
  """

  use Dagger.Core.Base, kind: :object, name: "Function"

  alias Dagger.Core.Client
  alias Dagger.Core.QueryBuilder, as: QB

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  Arguments accepted by the function, if any.
  """
  @spec args(t()) :: {:ok, [Dagger.FunctionArg.t()]} | {:error, term()}
  def args(%__MODULE__{} = function) do
    query_builder =
      function.query_builder |> QB.select("args") |> QB.select("id")

    with {:ok, items} <- Client.execute(function.client, query_builder) do
      {:ok,
       for %{"id" => id} <- items do
         %Dagger.FunctionArg{
           query_builder:
             QB.query()
             |> QB.select("loadFunctionArgFromID")
             |> QB.put_arg("id", id),
           client: function.client
         }
       end}
    end
  end

  @doc """
  A doc string for the function, if any.
  """
  @spec description(t()) :: {:ok, String.t()} | {:error, term()}
  def description(%__MODULE__{} = function) do
    query_builder =
      function.query_builder |> QB.select("description")

    Client.execute(function.client, query_builder)
  end

  @doc """
  A unique identifier for this Function.
  """
  @spec id(t()) :: {:ok, Dagger.FunctionID.t()} | {:error, term()}
  def id(%__MODULE__{} = function) do
    query_builder =
      function.query_builder |> QB.select("id")

    Client.execute(function.client, query_builder)
  end

  @doc """
  The name of the function.
  """
  @spec name(t()) :: {:ok, String.t()} | {:error, term()}
  def name(%__MODULE__{} = function) do
    query_builder =
      function.query_builder |> QB.select("name")

    Client.execute(function.client, query_builder)
  end

  @doc """
  The type returned by the function.
  """
  @spec return_type(t()) :: Dagger.TypeDef.t()
  def return_type(%__MODULE__{} = function) do
    query_builder =
      function.query_builder |> QB.select("returnType")

    %Dagger.TypeDef{
      query_builder: query_builder,
      client: function.client
    }
  end

  @doc """
  The location of this function declaration.
  """
  @spec source_map(t()) :: Dagger.SourceMap.t() | nil
  def source_map(%__MODULE__{} = function) do
    query_builder =
      function.query_builder |> QB.select("sourceMap")

    %Dagger.SourceMap{
      query_builder: query_builder,
      client: function.client
    }
  end

  @doc """
  Returns the function with the provided argument
  """
  @spec with_arg(t(), String.t(), Dagger.TypeDef.t(), [
          {:description, String.t() | nil},
          {:default_value, Dagger.JSON.t() | nil},
          {:default_path, String.t() | nil},
          {:ignore, [String.t()]},
          {:source_map, Dagger.SourceMapID.t() | nil}
        ]) :: Dagger.Function.t()
  def with_arg(%__MODULE__{} = function, name, type_def, optional_args \\ []) do
    query_builder =
      function.query_builder
      |> QB.select("withArg")
      |> QB.put_arg("name", name)
      |> QB.put_arg("typeDef", Dagger.ID.id!(type_def))
      |> QB.maybe_put_arg("description", optional_args[:description])
      |> QB.maybe_put_arg("defaultValue", optional_args[:default_value])
      |> QB.maybe_put_arg("defaultPath", optional_args[:default_path])
      |> QB.maybe_put_arg("ignore", optional_args[:ignore])
      |> QB.maybe_put_arg("sourceMap", optional_args[:source_map])

    %Dagger.Function{
      query_builder: query_builder,
      client: function.client
    }
  end

  @doc """
  Returns the function with the given doc string.
  """
  @spec with_description(t(), String.t()) :: Dagger.Function.t()
  def with_description(%__MODULE__{} = function, description) do
    query_builder =
      function.query_builder
      |> QB.select("withDescription")
      |> QB.put_arg("description", description)

    %Dagger.Function{
      query_builder: query_builder,
      client: function.client
    }
  end

  @doc """
  Returns the function with the given source map.
  """
  @spec with_source_map(t(), Dagger.SourceMap.t()) :: Dagger.Function.t()
  def with_source_map(%__MODULE__{} = function, source_map) do
    query_builder =
      function.query_builder
      |> QB.select("withSourceMap")
      |> QB.put_arg("sourceMap", Dagger.ID.id!(source_map))

    %Dagger.Function{
      query_builder: query_builder,
      client: function.client
    }
  end
end

defimpl Jason.Encoder, for: Dagger.Function do
  def encode(function, opts) do
    {:ok, id} = Dagger.Function.id(function)
    Jason.Encode.string(id, opts)
  end
end

defimpl Nestru.Decoder, for: Dagger.Function do
  def decode_fields_hint(_struct, _context, id) do
    {:ok, Dagger.Client.load_function_from_id(Dagger.Global.dag(), id)}
  end
end
