src/gleeam_code@internal@config.erl

-module(gleeam_code@internal@config).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeam_code/internal/config.gleam").
-export([session_file_exists/0, save_session/1, get_session/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(false).

-file("src/gleeam_code/internal/config.gleam", 62).
?DOC(false).
-spec config_path() -> binary().
config_path() ->
    case envoy_ffi:get(<<"HOME"/utf8>>) of
        {ok, Home} ->
            <<<<Home/binary, "/"/utf8>>/binary, ".gleeam"/utf8>>;

        {error, _} ->
            <<".gleeam"/utf8>>
    end.

-file("src/gleeam_code/internal/config.gleam", 27).
?DOC(false).
-spec session_file_exists() -> boolean().
session_file_exists() ->
    Path = <<<<(config_path())/binary, "/"/utf8>>/binary, "session"/utf8>>,
    gleeam_code@internal@file:exists(Path).

-file("src/gleeam_code/internal/config.gleam", 33).
?DOC(false).
-spec save_session(binary()) -> {ok, nil} | {error, binary()}.
save_session(Cookie) ->
    Dir = config_path(),
    Path = <<<<Dir/binary, "/"/utf8>>/binary, "session"/utf8>>,
    case gleeam_code@internal@file:mkdir(Dir) of
        {ok, _} ->
            case gleeam_code@internal@file:write(
                Path,
                gleam@string:trim(Cookie)
            ) of
                {ok, _} ->
                    {ok, nil};

                {error, Err} ->
                    {error,
                        <<"Failed to write session: "/utf8,
                            (gleeam_code@internal@file:describe_error(Err))/binary>>}
            end;

        {error, Err@1} ->
            {error,
                <<"Failed to create config dir: "/utf8,
                    (gleeam_code@internal@file:describe_error(Err@1))/binary>>}
    end.

-file("src/gleeam_code/internal/config.gleam", 48).
?DOC(false).
-spec read_session_file() -> {ok, binary()} | {error, binary()}.
read_session_file() ->
    Path = <<<<(config_path())/binary, "/"/utf8>>/binary, "session"/utf8>>,
    case gleeam_code@internal@file:read(Path) of
        {ok, Contents} ->
            Trimmed = gleam@string:trim(Contents),
            case Trimmed of
                <<""/utf8>> ->
                    {error, <<"Session file is empty. Run: glc auth"/utf8>>};

                _ ->
                    {ok, Trimmed}
            end;

        {error, _} ->
            {error, <<"No session file found"/utf8>>}
    end.

-file("src/gleeam_code/internal/config.gleam", 13).
?DOC(false).
-spec get_session() -> {ok, binary()} | {error, binary()}.
get_session() ->
    case read_session_file() of
        {ok, Value} ->
            {ok, Value};

        {error, _} ->
            case envoy_ffi:get(<<"LEETCODE_SESSION"/utf8>>) of
                {ok, Value@1} when Value@1 =/= <<""/utf8>> ->
                    {ok, Value@1};

                _ ->
                    {error,
                        <<"No session found. Set LEETCODE_SESSION env var or run: glc auth"/utf8>>}
            end
    end.