# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.SearchResult do
  @moduledoc """
  Dagger.SearchResult
  """

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

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

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  The byte offset of this line within the file.
  """
  @spec absolute_offset(t()) :: {:ok, integer()} | {:error, term()}
  def absolute_offset(%__MODULE__{} = search_result) do
    query_builder =
      search_result.query_builder |> QB.select("absoluteOffset")

    Client.execute(search_result.client, query_builder)
  end

  @doc """
  The path to the file that matched.
  """
  @spec file_path(t()) :: {:ok, String.t()} | {:error, term()}
  def file_path(%__MODULE__{} = search_result) do
    query_builder =
      search_result.query_builder |> QB.select("filePath")

    Client.execute(search_result.client, query_builder)
  end

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

    Client.execute(search_result.client, query_builder)
  end

  @doc """
  The first line that matched.
  """
  @spec line_number(t()) :: {:ok, integer()} | {:error, term()}
  def line_number(%__MODULE__{} = search_result) do
    query_builder =
      search_result.query_builder |> QB.select("lineNumber")

    Client.execute(search_result.client, query_builder)
  end

  @doc """
  The line content that matched.
  """
  @spec matched_lines(t()) :: {:ok, String.t()} | {:error, term()}
  def matched_lines(%__MODULE__{} = search_result) do
    query_builder =
      search_result.query_builder |> QB.select("matchedLines")

    Client.execute(search_result.client, query_builder)
  end

  @doc """
  Sub-match positions and content within the matched lines.
  """
  @spec submatches(t()) :: {:ok, [Dagger.SearchSubmatch.t()]} | {:error, term()}
  def submatches(%__MODULE__{} = search_result) do
    query_builder =
      search_result.query_builder |> QB.select("submatches") |> QB.select("id")

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

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

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