# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.FunctionCall do
  @moduledoc """
  An active function call.
  """

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

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

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

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

    Client.execute(function_call.client, query_builder)
  end

  @doc """
  The argument values the function is being invoked with.
  """
  @spec input_args(t()) :: {:ok, [Dagger.FunctionCallArgValue.t()]} | {:error, term()}
  def input_args(%__MODULE__{} = function_call) do
    query_builder =
      function_call.query_builder |> QB.select("inputArgs") |> QB.select("id")

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

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

    Client.execute(function_call.client, query_builder)
  end

  @doc """
  The value of the parent object of the function being called. If the function is top-level to the module, this is always an empty object.
  """
  @spec parent(t()) :: {:ok, Dagger.JSON.t()} | {:error, term()}
  def parent(%__MODULE__{} = function_call) do
    query_builder =
      function_call.query_builder |> QB.select("parent")

    Client.execute(function_call.client, query_builder)
  end

  @doc """
  The name of the parent object of the function being called. If the function is top-level to the module, this is the name of the module.
  """
  @spec parent_name(t()) :: {:ok, String.t()} | {:error, term()}
  def parent_name(%__MODULE__{} = function_call) do
    query_builder =
      function_call.query_builder |> QB.select("parentName")

    Client.execute(function_call.client, query_builder)
  end

  @doc """
  Return an error from the function.
  """
  @spec return_error(t(), Dagger.Error.t()) :: :ok | {:error, term()}
  def return_error(%__MODULE__{} = function_call, error) do
    query_builder =
      function_call.query_builder
      |> QB.select("returnError")
      |> QB.put_arg("error", Dagger.ID.id!(error))

    case Client.execute(function_call.client, query_builder) do
      {:ok, _} -> :ok
      error -> error
    end
  end

  @doc """
  Set the return value of the function call to the provided value.
  """
  @spec return_value(t(), Dagger.JSON.t()) :: :ok | {:error, term()}
  def return_value(%__MODULE__{} = function_call, value) do
    query_builder =
      function_call.query_builder |> QB.select("returnValue") |> QB.put_arg("value", value)

    case Client.execute(function_call.client, query_builder) do
      {:ok, _} -> :ok
      error -> error
    end
  end
end

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

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