%<*package>

\seq_new:N \g_@@_nextgroup_seq
\quark_new:N \q_@@_prefix_mark

% [parameter] scope path of index (verbatim) , block index's parameter
\cs_new_protected:Nn \_@@_set_index:nn {

    \seq_if_in:NnTF \g_@@_index_seq {./#1} {
        \msg_warning:nnn {SimpleSystemTeX} {Block Index Repeated} {#1}
    } {
        \phantomsection \label{./#1}
        \seq_gput_right:Nn \g_@@_index_seq {./#1}
        \iow_now:Nn \@auxout {
            \Register@index {./#1}
        }
    }

    \iow_now:Nn \@auxout {
        \Register@indexparameter {#1} {#2}
    }

}

% [parameter] entry block's label (expanded) , path of entry block's parent section (verbatim)
\cs_new_protected:Nn \_@@_print_index_block:nn {
    \group_begin:

    % @indexblock
    \tl_set:Nn \l_@@_indexblock_tl {#1}
    % @indexblockname
    \prop_get:NnN \g_@@_blockname_register_prop {#1} \l_@@_indexblockname_tl
    % @indexblockparameter
    \prop_get:NnN \g_@@_blockparameter_register_prop {#1} \l_@@_indexblockparameter_tl
    % @indexblockcontent
    \prop_get:NnN \g_@@_blockcontent_register_prop {#1} \l_@@_indexblockcontent_tl
    % @indexsection
    \seq_set_split:Nnn \l_tmpa_seq {/} {#2}
    \seq_pop_right:NN \l_tmpa_seq \l_@@_indexsection_tl
    % @indexsectionpath
    \tl_set:Nn \l_@@_indexsectionpath_tl {#2}
    % @indexsectionpage
    \tl_set:Nn \l_@@_indexsectionpage_tl {\pageref*{./#2}}
    % @indexsectionname
    \prop_get:NnN \g_@@_sectionname_prop {#2} \l_@@_indexsectionname_tl
    % @indexsectionparameter
    \prop_get:NnN \g_@@_sectionparameter_prop {#2} \l_@@_indexsectionparameter_tl
    % @indexpart
    \prop_get:NnN \g_@@_sectionpart_prop {#2} \l_@@_indexpart_tl

    {\g_@@_IndexBlock_style_tl}

    \group_end:
}

% [parameter] scope path of index (verbatim) , block's label (expanded) , path of entry block's parent section (verbatim)
\cs_new_protected:Nn \_@@_process_index_item:nnn {
    \group_begin:

    \tl_set:Nn \l_tmpa_tl {\q_@@_prefix_mark #3}
    \tl_replace_once:Nnn \l_tmpa_tl {\q_@@_prefix_mark #1} {}
    \tl_if_in:NnF \l_tmpa_tl {\q_@@_prefix_mark} {
        \seq_set_split:NnV \l_tmpa_seq {/} \l_tmpa_tl
        \int_compare:nTF {\seq_count:N \l_tmpa_seq == 1} {
            \_@@_print_index_block:nn {#2} {#3}
        } {
            \seq_if_in:NeF \g_@@_nextgroup_seq {
                \exp_not:n {#1} \seq_item:Nn \l_tmpa_seq {1} /
            } {
                \seq_gput_right:Ne \g_@@_nextgroup_seq {
                    \exp_not:n {#1} \seq_item:Nn \l_tmpa_seq {1} /
                }
            }
        }
    }

    \group_end:
}

% [parameter] whether to add label , scope path of index (verbatim) , list of block type names , block index's parameter
\cs_new_protected:Nn \_@@_generate_block_index:Nnnn {
    \group_begin:

    \clist_set:Nn \l_tmpa_clist {#3}

    % @index(...)group
    \_@@_parse_path:nn {#2.groupkeep} {index}
    % @indexparameter
    \tl_set:Nn \l_@@_indexparameter_tl {#4}

    \bool_if:NT #1 {
        \_@@_set_index:nn {#2} {#4}
    }

    \int_case:nn {\seq_count:N \l_@@_path_seq} {
        {0} {
            \msg_fatal:nnn {SimpleSystemTeX} {Invalid Group Path} {#2}
        }
        {1} {{\g_@@_IndexGroup_style_tl}}
        {2} {{\g_@@_IndexSubgroup_style_tl}}
        {3} {{\g_@@_IndexSubsubgroup_style_tl}}
        {4} {{\g_@@_IndexSubsubsubgroup_style_tl}}
    }

    \seq_map_inline:Nn \g_@@_block_register_seq {
        \clist_if_empty:NTF \l_tmpa_clist {
            \exp_args:Nnne \_@@_process_index_item:nnn {#2} {##1} {\prop_item:Nn \g_@@_blocksectionpath_register_prop {##1}}
        } {
            \exp_args:NNe \clist_if_in:NnT \l_tmpa_clist {
                \prop_item:Nn \g_@@_blocktype_register_prop {##1}
            } {
                \exp_args:Nnne \_@@_process_index_item:nnn {#2} {##1} {\prop_item:Nn \g_@@_blocksectionpath_register_prop {##1}}
            }
        }
    }

    \seq_map_inline:Nn \g_@@_nextgroup_seq {
        \_@@_generate_block_index:Nnnn #1 {##1} {#3} {#4}
    }

    \group_end:
}
\cs_generate_variant:Nn \_@@_generate_block_index:Nnnn {NnVV}

%</package>