src/gleeam_code@submit.erl

-module(gleeam_code@submit).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeam_code/submit.gleam").
-export([run/3]).
-export_type([submit_result/0]).

-type submit_result() :: {submit_result, binary(), binary(), binary()}.

-file("src/gleeam_code/submit.gleam", 85).
-spec require_session() -> {ok, binary()} | {error, binary()}.
require_session() ->
    case gleeam_code@internal@config:get_session() of
        {ok, S} ->
            {ok, S};

        {error, _} ->
            {error, <<"No session found. Run 'glc auth' first."/utf8>>}
    end.

-file("src/gleeam_code/submit.gleam", 92).
-spec extract_slug(binary()) -> binary().
extract_slug(Module_name) ->
    case gleam@string:split_once(Module_name, <<"_"/utf8>>) of
        {ok, {_, Slug_part}} ->
            gleam@string:replace(Slug_part, <<"_"/utf8>>, <<"-"/utf8>>);

        {error, _} ->
            Module_name
    end.

-file("src/gleeam_code/submit.gleam", 110).
-spec drop_leading_zeros(binary()) -> binary().
drop_leading_zeros(S) ->
    case gleam_stdlib:string_pop_grapheme(S) of
        {ok, {<<"0"/utf8>>, Rest}} when Rest =/= <<""/utf8>> ->
            drop_leading_zeros(Rest);

        _ ->
            S
    end.

-file("src/gleeam_code/submit.gleam", 99).
-spec extract_question_id(binary()) -> binary().
extract_question_id(Module_name) ->
    Id_part = begin
        _pipe = Module_name,
        _pipe@1 = gleam@string:drop_start(_pipe, 1),
        gleam@string:split_once(_pipe@1, <<"_"/utf8>>)
    end,
    case Id_part of
        {ok, {Num, _}} ->
            drop_leading_zeros(Num);

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

-file("src/gleeam_code/submit.gleam", 134).
-spec extract_csrf_from_headers(list({binary(), binary()})) -> {ok, binary()} |
    {error, binary()}.
extract_csrf_from_headers(Headers) ->
    case Headers of
        [] ->
            {error, <<"CSRF token not found in response"/utf8>>};

        [{Name, Value} | Rest] ->
            case (string:lowercase(Name) =:= <<"set-cookie"/utf8>>) andalso gleam_stdlib:contains_string(
                Value,
                <<"csrftoken="/utf8>>
            ) of
                true ->
                    Token = begin
                        _pipe = Value,
                        _pipe@1 = gleam@string:split(
                            _pipe,
                            <<"csrftoken="/utf8>>
                        ),
                        (fun(Parts) -> case Parts of
                                [_, After | _] ->
                                    _pipe@2 = After,
                                    _pipe@3 = gleam@string:split(
                                        _pipe@2,
                                        <<";"/utf8>>
                                    ),
                                    (fun(P) -> case P of
                                            [T | _] ->
                                                T;

                                            [] ->
                                                <<""/utf8>>
                                        end end)(_pipe@3);

                                _ ->
                                    <<""/utf8>>
                            end end)(_pipe@1)
                    end,
                    case Token of
                        <<""/utf8>> ->
                            {error, <<"Failed to extract CSRF token"/utf8>>};

                        T@1 ->
                            {ok, T@1}
                    end;

                false ->
                    extract_csrf_from_headers(Rest)
            end
    end.

-file("src/gleeam_code/submit.gleam", 117).
-spec fetch_csrf(binary()) -> {ok, binary()} | {error, binary()}.
fetch_csrf(Session) ->
    Req = begin
        _pipe = gleam@http@request:new(),
        _pipe@1 = gleam@http@request:set_method(_pipe, post),
        _pipe@2 = gleam@http@request:set_host(_pipe@1, <<"leetcode.com"/utf8>>),
        _pipe@3 = gleam@http@request:set_path(_pipe@2, <<"/graphql"/utf8>>),
        _pipe@4 = gleam@http@request:set_scheme(_pipe@3, https),
        _pipe@5 = gleam@http@request:set_body(
            _pipe@4,
            <<"{\"query\":\"{ user { username } }\"}"/utf8>>
        ),
        _pipe@6 = gleam@http@request:prepend_header(
            _pipe@5,
            <<"content-type"/utf8>>,
            <<"application/json"/utf8>>
        ),
        gleam@http@request:prepend_header(
            _pipe@6,
            <<"cookie"/utf8>>,
            <<"LEETCODE_SESSION="/utf8, Session/binary>>
        )
    end,
    case gleam@httpc:send(Req) of
        {error, _} ->
            {error, <<"Failed to connect to LeetCode for CSRF"/utf8>>};

        {ok, Resp} ->
            extract_csrf_from_headers(erlang:element(3, Resp))
    end.

-file("src/gleeam_code/submit.gleam", 194).
-spec find_name_line(list(binary())) -> {ok, binary()} | {error, binary()}.
find_name_line(Lines) ->
    case Lines of
        [] ->
            {error, <<"No 'name' field found in gleam.toml"/utf8>>};

        [Line | Rest] ->
            case gleam_stdlib:string_starts_with(
                gleam@string:trim(Line),
                <<"name"/utf8>>
            ) of
                true ->
                    case gleam@string:split_once(Line, <<"="/utf8>>) of
                        {ok, {_, Value}} ->
                            Name = begin
                                _pipe = Value,
                                _pipe@1 = gleam@string:trim(_pipe),
                                gleam@string:replace(
                                    _pipe@1,
                                    <<"\""/utf8>>,
                                    <<""/utf8>>
                                )
                            end,
                            {ok, Name};

                        {error, _} ->
                            find_name_line(Rest)
                    end;

                false ->
                    find_name_line(Rest)
            end
    end.

-file("src/gleeam_code/submit.gleam", 189).
-spec extract_name_from_toml(binary()) -> {ok, binary()} | {error, binary()}.
extract_name_from_toml(Content) ->
    Lines = gleam@string:split(Content, <<"\n"/utf8>>),
    find_name_line(Lines).

-file("src/gleeam_code/submit.gleam", 181).
-spec read_project_name(binary()) -> {ok, binary()} | {error, binary()}.
read_project_name(Base_dir) ->
    Toml_path = <<Base_dir/binary, "/gleam.toml"/utf8>>,
    case gleeam_code@internal@file:read(Toml_path) of
        {error, _} ->
            {error, <<"Could not read gleam.toml"/utf8>>};

        {ok, Content} ->
            extract_name_from_toml(Content)
    end.

-file("src/gleeam_code/submit.gleam", 215).
-spec read_erl_file(binary()) -> {ok, binary()} | {error, binary()}.
read_erl_file(Path) ->
    case gleeam_code@internal@file:read(Path) of
        {ok, Content} ->
            {ok, Content};

        {error, _} ->
            {error,
                <<"Could not read compiled .erl file at: "/utf8, Path/binary>>}
    end.

-file("src/gleeam_code/submit.gleam", 277).
-spec parse_submit_response(binary()) -> {ok, binary()} | {error, binary()}.
parse_submit_response(Body) ->
    Decoder = begin
        gleam@dynamic@decode:field(
            <<"submission_id"/utf8>>,
            {decoder, fun gleam@dynamic@decode:decode_int/1},
            fun(Id) -> gleam@dynamic@decode:success(Id) end
        )
    end,
    case gleam@json:parse(Body, Decoder) of
        {ok, Id@1} ->
            {ok, gleam@string:inspect(Id@1)};

        {error, _} ->
            {error, <<"Failed to parse submit response: "/utf8, Body/binary>>}
    end.

-file("src/gleeam_code/submit.gleam", 226).
-spec submit_to_leetcode(binary(), binary(), binary(), binary(), binary()) -> {ok,
        binary()} |
    {error, binary()}.
submit_to_leetcode(Slug, Question_id, Code, Session, Csrf) ->
    Body = gleam@json:to_string(
        gleam@json:object(
            [{<<"lang"/utf8>>, gleam@json:string(<<"erlang"/utf8>>)},
                {<<"questionSlug"/utf8>>, gleam@json:string(Slug)},
                {<<"question_id"/utf8>>, gleam@json:string(Question_id)},
                {<<"typed_code"/utf8>>, gleam@json:string(Code)}]
        )
    ),
    Req = begin
        _pipe = gleam@http@request:new(),
        _pipe@1 = gleam@http@request:set_method(_pipe, post),
        _pipe@2 = gleam@http@request:set_host(_pipe@1, <<"leetcode.com"/utf8>>),
        _pipe@3 = gleam@http@request:set_path(
            _pipe@2,
            <<<<"/problems/"/utf8, Slug/binary>>/binary, "/submit/"/utf8>>
        ),
        _pipe@4 = gleam@http@request:set_scheme(_pipe@3, https),
        _pipe@5 = gleam@http@request:set_body(_pipe@4, Body),
        _pipe@6 = gleam@http@request:prepend_header(
            _pipe@5,
            <<"content-type"/utf8>>,
            <<"application/json"/utf8>>
        ),
        _pipe@7 = gleam@http@request:prepend_header(
            _pipe@6,
            <<"cookie"/utf8>>,
            <<<<<<"LEETCODE_SESSION="/utf8, Session/binary>>/binary,
                    "; csrftoken="/utf8>>/binary,
                Csrf/binary>>
        ),
        _pipe@8 = gleam@http@request:prepend_header(
            _pipe@7,
            <<"x-csrftoken"/utf8>>,
            Csrf
        ),
        gleam@http@request:prepend_header(
            _pipe@8,
            <<"referer"/utf8>>,
            <<<<"https://leetcode.com/problems/"/utf8, Slug/binary>>/binary,
                "/"/utf8>>
        )
    end,
    case gleam@httpc:send(Req) of
        {error, _} ->
            {error, <<"Failed to connect to LeetCode"/utf8>>};

        {ok, Resp} ->
            case erlang:element(2, Resp) of
                200 ->
                    parse_submit_response(erlang:element(4, Resp));

                _ ->
                    {error,
                        <<<<<<"Submit failed with status: "/utf8,
                                    (gleam@string:inspect(
                                        erlang:element(2, Resp)
                                    ))/binary>>/binary,
                                " - "/utf8>>/binary,
                            (erlang:element(4, Resp))/binary>>}
            end
    end.

-file("src/gleeam_code/submit.gleam", 352).
-spec parse_final_result(binary()) -> {ok, submit_result()} | {error, binary()}.
parse_final_result(Body) ->
    Decoder = begin
        gleam@dynamic@decode:field(
            <<"status_msg"/utf8>>,
            {decoder, fun gleam@dynamic@decode:decode_string/1},
            fun(Status) ->
                gleam@dynamic@decode:field(
                    <<"status_runtime"/utf8>>,
                    {decoder, fun gleam@dynamic@decode:decode_string/1},
                    fun(Runtime) ->
                        gleam@dynamic@decode:field(
                            <<"status_memory"/utf8>>,
                            {decoder, fun gleam@dynamic@decode:decode_string/1},
                            fun(Memory) ->
                                gleam@dynamic@decode:success(
                                    {submit_result, Status, Runtime, Memory}
                                )
                            end
                        )
                    end
                )
            end
        )
    end,
    case gleam@json:parse(Body, Decoder) of
        {ok, Result} ->
            {ok, Result};

        {error, _} ->
            {error, <<"Failed to parse submission result: "/utf8, Body/binary>>}
    end.

-file("src/gleeam_code/submit.gleam", 335).
-spec parse_check_response(binary()) -> {ok, submit_result()} |
    {error, binary()}.
parse_check_response(Body) ->
    State_decoder = begin
        gleam@dynamic@decode:field(
            <<"state"/utf8>>,
            {decoder, fun gleam@dynamic@decode:decode_string/1},
            fun(State) -> gleam@dynamic@decode:success(State) end
        )
    end,
    case gleam@json:parse(Body, State_decoder) of
        {error, _} ->
            {error, <<"Failed to parse check response"/utf8>>};

        {ok, State@1} ->
            case State@1 of
                <<"PENDING"/utf8>> ->
                    {error, <<"pending"/utf8>>};

                <<"STARTED"/utf8>> ->
                    {error, <<"pending"/utf8>>};

                <<"SUCCESS"/utf8>> ->
                    parse_final_result(Body);

                _ ->
                    {error, <<"Unexpected state: "/utf8, State@1/binary>>}
            end
    end.

-file("src/gleeam_code/submit.gleam", 309).
-spec check_submission(binary(), binary(), binary()) -> {ok, submit_result()} |
    {error, binary()}.
check_submission(Submission_id, Session, Csrf) ->
    Req = begin
        _pipe = gleam@http@request:new(),
        _pipe@1 = gleam@http@request:set_method(_pipe, get),
        _pipe@2 = gleam@http@request:set_host(_pipe@1, <<"leetcode.com"/utf8>>),
        _pipe@3 = gleam@http@request:set_path(
            _pipe@2,
            <<<<"/submissions/detail/"/utf8, Submission_id/binary>>/binary,
                "/check/"/utf8>>
        ),
        _pipe@4 = gleam@http@request:set_scheme(_pipe@3, https),
        gleam@http@request:prepend_header(
            _pipe@4,
            <<"cookie"/utf8>>,
            <<<<<<"LEETCODE_SESSION="/utf8, Session/binary>>/binary,
                    "; csrftoken="/utf8>>/binary,
                Csrf/binary>>
        )
    end,
    case gleam@httpc:send(Req) of
        {error, _} ->
            {error, <<"Failed to check submission status"/utf8>>};

        {ok, Resp} ->
            case erlang:element(2, Resp) of
                200 ->
                    parse_check_response(erlang:element(4, Resp));

                _ ->
                    {error,
                        <<"Check failed with status: "/utf8,
                            (gleam@string:inspect(erlang:element(2, Resp)))/binary>>}
            end
    end.

-file("src/gleeam_code/submit.gleam", 370).
-spec format_result(submit_result()) -> binary().
format_result(Result) ->
    case erlang:element(2, Result) of
        <<"Accepted"/utf8>> ->
            <<<<<<"✓ Accepted! Runtime: "/utf8,
                        (erlang:element(3, Result))/binary>>/binary,
                    ", Memory: "/utf8>>/binary,
                (erlang:element(4, Result))/binary>>;

        _ ->
            <<<<<<<<<<"✗ "/utf8, (erlang:element(2, Result))/binary>>/binary,
                            " | Runtime: "/utf8>>/binary,
                        (erlang:element(3, Result))/binary>>/binary,
                    ", Memory: "/utf8>>/binary,
                (erlang:element(4, Result))/binary>>
    end.

-file("src/gleeam_code/submit.gleam", 384).
-spec read_solution_file(binary()) -> {ok, binary()} | {error, binary()}.
read_solution_file(Path) ->
    case gleeam_code@internal@file:read(Path) of
        {ok, Content} ->
            {ok, Content};

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

-file("src/gleeam_code/submit.gleam", 409).
-spec generate_conversion_fns(boolean(), boolean()) -> binary().
generate_conversion_fns(Needs_tree, Needs_list) ->
    Tree_fns = case Needs_tree of
        true ->
            <<<<<<<<<<"tree_to_record(none) -> null;\n"/utf8,
                                "tree_to_record({some, {tree_node, Val, Left, Right}}) ->\n"/utf8>>/binary,
                            "    #tree_node{val = Val, left = tree_to_record(Left), right = tree_to_record(Right)}.\n\n"/utf8>>/binary,
                        "tree_from_record(null) -> none;\n"/utf8>>/binary,
                    "tree_from_record(#tree_node{val = Val, left = Left, right = Right}) ->\n"/utf8>>/binary,
                "    {some, {tree_node, Val, tree_from_record(Left), tree_from_record(Right)}}.\n\n"/utf8>>;

        false ->
            <<""/utf8>>
    end,
    List_fns = case Needs_list of
        true ->
            <<<<<<<<<<"list_to_record(none) -> null;\n"/utf8,
                                "list_to_record({some, {list_node, Val, Next}}) ->\n"/utf8>>/binary,
                            "    #list_node{val = Val, next = list_to_record(Next)}.\n\n"/utf8>>/binary,
                        "list_from_record(null) -> none;\n"/utf8>>/binary,
                    "list_from_record(#list_node{val = Val, next = Next}) ->\n"/utf8>>/binary,
                "    {some, {list_node, Val, list_from_record(Next)}}.\n\n"/utf8>>;

        false ->
            <<""/utf8>>
    end,
    <<Tree_fns/binary, List_fns/binary>>.

-file("src/gleeam_code/submit.gleam", 473).
-spec generate_arg_names(integer(), integer(), list(binary())) -> list(binary()).
generate_arg_names(Count, Current, Acc) ->
    case Current > Count of
        true ->
            lists:reverse(Acc);

        false ->
            Name = <<"Arg"/utf8, (gleam@string:inspect(Current))/binary>>,
            generate_arg_names(Count, Current + 1, [Name | Acc])
    end.

-file("src/gleeam_code/submit.gleam", 506).
-spec extract_inner_type(binary()) -> binary().
extract_inner_type(List_type) ->
    _pipe = List_type,
    _pipe@1 = gleam@string:drop_start(_pipe, 5),
    gleam@string:drop_end(_pipe@1, 1).

-file("src/gleeam_code/submit.gleam", 487).
-spec gleam_type_to_erlang_spec(binary()) -> binary().
gleam_type_to_erlang_spec(Gleam_type) ->
    case Gleam_type of
        <<"Option(TreeNode)"/utf8>> ->
            <<"'null' | #tree_node{}"/utf8>>;

        <<"Option(ListNode)"/utf8>> ->
            <<"'null' | #list_node{}"/utf8>>;

        <<"Int"/utf8>> ->
            <<"integer()"/utf8>>;

        <<"Float"/utf8>> ->
            <<"float()"/utf8>>;

        <<"Bool"/utf8>> ->
            <<"boolean()"/utf8>>;

        <<"String"/utf8>> ->
            <<"unicode:unicode_binary()"/utf8>>;

        _ ->
            case gleam_stdlib:string_starts_with(Gleam_type, <<"List("/utf8>>) of
                true ->
                    <<<<"["/utf8,
                            (gleam_type_to_erlang_spec(
                                extract_inner_type(Gleam_type)
                            ))/binary>>/binary,
                        "]"/utf8>>;

                false ->
                    <<"any()"/utf8>>
            end
    end.

-file("src/gleeam_code/submit.gleam", 512).
-spec convert_arg_expr(binary(), binary()) -> binary().
convert_arg_expr(Arg_name, Type_str) ->
    case Type_str of
        <<"Option(TreeNode)"/utf8>> ->
            <<<<"tree_from_record("/utf8, Arg_name/binary>>/binary, ")"/utf8>>;

        <<"Option(ListNode)"/utf8>> ->
            <<<<"list_from_record("/utf8, Arg_name/binary>>/binary, ")"/utf8>>;

        _ ->
            Arg_name
    end.

-file("src/gleeam_code/submit.gleam", 520).
-spec convert_result_expr(binary(), binary()) -> binary().
convert_result_expr(Result_var, Return_type) ->
    case Return_type of
        <<"Option(TreeNode)"/utf8>> ->
            <<<<"tree_to_record("/utf8, Result_var/binary>>/binary, ")"/utf8>>;

        <<"Option(ListNode)"/utf8>> ->
            <<<<"list_to_record("/utf8, Result_var/binary>>/binary, ")"/utf8>>;

        _ ->
            Result_var
    end.

-file("src/gleeam_code/submit.gleam", 433).
-spec generate_wrapper(gleeam_code@internal@meta:submit_meta()) -> binary().
generate_wrapper(Submit_meta) ->
    Arg_count = erlang:length(erlang:element(3, Submit_meta)),
    Arg_names = generate_arg_names(Arg_count, 1, []),
    Spec_params = begin
        _pipe = gleam@list:map(
            erlang:element(3, Submit_meta),
            fun gleam_type_to_erlang_spec/1
        ),
        gleam@string:join(_pipe, <<", "/utf8>>)
    end,
    Spec_return = gleam_type_to_erlang_spec(erlang:element(4, Submit_meta)),
    Wrapper_params = gleam@string:join(Arg_names, <<", "/utf8>>),
    Converted_args = begin
        _pipe@1 = gleam@list:zip(Arg_names, erlang:element(3, Submit_meta)),
        gleam@list:map(
            _pipe@1,
            fun(Pair) ->
                convert_arg_expr(
                    erlang:element(1, Pair),
                    erlang:element(2, Pair)
                )
            end
        )
    end,
    Impl_args = gleam@string:join(Converted_args, <<", "/utf8>>),
    Result_expr = convert_result_expr(
        <<"GleamResult"/utf8>>,
        erlang:element(4, Submit_meta)
    ),
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"-spec "/utf8,
                                                                            (erlang:element(
                                                                                2,
                                                                                Submit_meta
                                                                            ))/binary>>/binary,
                                                                        "("/utf8>>/binary,
                                                                    Spec_params/binary>>/binary,
                                                                ") -> "/utf8>>/binary,
                                                            Spec_return/binary>>/binary,
                                                        ".\n"/utf8>>/binary,
                                                    (erlang:element(
                                                        2,
                                                        Submit_meta
                                                    ))/binary>>/binary,
                                                "("/utf8>>/binary,
                                            Wrapper_params/binary>>/binary,
                                        ") ->\n"/utf8>>/binary,
                                    "    GleamResult = "/utf8>>/binary,
                                (erlang:element(2, Submit_meta))/binary>>/binary,
                            "_impl("/utf8>>/binary,
                        Impl_args/binary>>/binary,
                    "),\n"/utf8>>/binary,
                "    "/utf8>>/binary,
            Result_expr/binary>>/binary,
        ".\n"/utf8>>.

-file("src/gleeam_code/submit.gleam", 391).
-spec bundle_with_types(
    binary(),
    gleeam_code@internal@meta:submit_meta(),
    boolean(),
    boolean()
) -> binary().
bundle_with_types(Erl_code, Submit_meta, Needs_tree, Needs_list) ->
    Conversion_fns = generate_conversion_fns(Needs_tree, Needs_list),
    Renamed = gleam@string:replace(
        Erl_code,
        <<(erlang:element(2, Submit_meta))/binary, "("/utf8>>,
        <<(erlang:element(2, Submit_meta))/binary, "_impl("/utf8>>
    ),
    Wrapper = generate_wrapper(Submit_meta),
    <<<<<<<<Conversion_fns/binary, "\n"/utf8>>/binary, Wrapper/binary>>/binary,
            "\n"/utf8>>/binary,
        Renamed/binary>>.

-file("src/gleeam_code/submit.gleam", 172).
-spec build_erlang(binary()) -> {ok, nil} | {error, binary()}.
build_erlang(Base_dir) ->
    Output = gleeam_code_submit_ffi:os_cmd(
        <<<<"cd "/utf8, Base_dir/binary>>/binary,
            " && gleam build --target erlang 2>&1"/utf8>>
    ),
    case gleam_stdlib:contains_string(Output, <<"error:"/utf8>>) of
        true ->
            {error, <<"Build failed:\n"/utf8, Output/binary>>};

        false ->
            {ok, nil}
    end.

-file("src/gleeam_code/submit.gleam", 289).
-spec poll_result(binary(), binary(), binary(), integer()) -> {ok,
        submit_result()} |
    {error, binary()}.
poll_result(Submission_id, Session, Csrf, Attempts) ->
    case Attempts > 20 of
        true ->
            {error, <<"Timed out waiting for submission result"/utf8>>};

        false ->
            gleeam_code_submit_ffi:sleep(1000),
            case check_submission(Submission_id, Session, Csrf) of
                {ok, Result} ->
                    {ok, Result};

                {error, <<"pending"/utf8>>} ->
                    poll_result(Submission_id, Session, Csrf, Attempts + 1);

                {error, Err} ->
                    {error, Err}
            end
    end.

-file("src/gleeam_code/submit.gleam", 16).
-spec run(binary(), binary(), fun((binary()) -> nil)) -> {ok, nil} |
    {error, binary()}.
run(Base_dir, Target, Print) ->
    gleam@result:'try'(
        require_session(),
        fun(Session) ->
            gleam@result:'try'(
                gleeam_code@internal@resolver:resolve_module(Base_dir, Target),
                fun(Module_name) ->
                    Slug = extract_slug(Module_name),
                    Question_id = extract_question_id(Module_name),
                    Print(<<"Building solution..."/utf8>>),
                    gleam@result:'try'(
                        build_erlang(Base_dir),
                        fun(_) ->
                            gleam@result:'try'(
                                read_project_name(Base_dir),
                                fun(Project_name) ->
                                    Erl_path = <<<<<<<<<<Base_dir/binary,
                                                        "/build/dev/erlang/"/utf8>>/binary,
                                                    Project_name/binary>>/binary,
                                                "/_gleam_artefacts/solutions@"/utf8>>/binary,
                                            Module_name/binary>>/binary,
                                        "@solution.erl"/utf8>>,
                                    gleam@result:'try'(
                                        read_erl_file(Erl_path),
                                        fun(Erl_source) ->
                                            Converted = gleeam_code@internal@erlang_convert:convert(
                                                Erl_source
                                            ),
                                            Stdlib_dir = <<Base_dir/binary,
                                                "/build/dev/erlang/gleam_stdlib/_gleam_artefacts"/utf8>>,
                                            Bundled = gleeam_code@internal@stdlib_bundler:bundle(
                                                Converted,
                                                Stdlib_dir
                                            ),
                                            Solution_path = <<<<<<Base_dir/binary,
                                                        "/src/solutions/"/utf8>>/binary,
                                                    Module_name/binary>>/binary,
                                                "/solution.gleam"/utf8>>,
                                            gleam@result:'try'(
                                                read_solution_file(
                                                    Solution_path
                                                ),
                                                fun(Solution_source) ->
                                                    Needs_tree = gleam_stdlib:contains_string(
                                                        Solution_source,
                                                        <<"TreeNode"/utf8>>
                                                    ),
                                                    Needs_list = gleam_stdlib:contains_string(
                                                        Solution_source,
                                                        <<"ListNode"/utf8>>
                                                    ),
                                                    Meta_path = <<<<<<Base_dir/binary,
                                                                "/src/solutions/"/utf8>>/binary,
                                                            Module_name/binary>>/binary,
                                                        "/.glc_meta"/utf8>>,
                                                    gleam@result:'try'(
                                                        gleeam_code@internal@meta:read(
                                                            Meta_path
                                                        ),
                                                        fun(Submit_meta) ->
                                                            Final_code = case Needs_tree
                                                            orelse Needs_list of
                                                                false ->
                                                                    Bundled;

                                                                true ->
                                                                    bundle_with_types(
                                                                        Bundled,
                                                                        Submit_meta,
                                                                        Needs_tree,
                                                                        Needs_list
                                                                    )
                                                            end,
                                                            Print(
                                                                <<"Submitting to LeetCode as Erlang..."/utf8>>
                                                            ),
                                                            gleam@result:'try'(
                                                                fetch_csrf(
                                                                    Session
                                                                ),
                                                                fun(Csrf) ->
                                                                    gleam@result:'try'(
                                                                        submit_to_leetcode(
                                                                            Slug,
                                                                            Question_id,
                                                                            Final_code,
                                                                            Session,
                                                                            Csrf
                                                                        ),
                                                                        fun(
                                                                            Submission_id
                                                                        ) ->
                                                                            Print(
                                                                                <<"Submitted! Checking result..."/utf8>>
                                                                            ),
                                                                            gleam@result:'try'(
                                                                                poll_result(
                                                                                    Submission_id,
                                                                                    Session,
                                                                                    Csrf,
                                                                                    0
                                                                                ),
                                                                                fun(
                                                                                    Submit_result
                                                                                ) ->
                                                                                    Print(
                                                                                        format_result(
                                                                                            Submit_result
                                                                                        )
                                                                                    ),
                                                                                    gleeam_code@internal@meta:save_status(
                                                                                        Meta_path,
                                                                                        erlang:element(
                                                                                            2,
                                                                                            Submit_result
                                                                                        ),
                                                                                        erlang:element(
                                                                                            3,
                                                                                            Submit_result
                                                                                        ),
                                                                                        erlang:element(
                                                                                            4,
                                                                                            Submit_result
                                                                                        )
                                                                                    ),
                                                                                    {ok,
                                                                                        nil}
                                                                                end
                                                                            )
                                                                        end
                                                                    )
                                                                end
                                                            )
                                                        end
                                                    )
                                                end
                                            )
                                        end
                                    )
                                end
                            )
                        end
                    )
                end
            )
        end
    ).