#!/usr/bin/env zsh
# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-

# Copyright (c) 2020 Sebastian Gniazdowski

za-unscope-before-load-handler() {

    # Set the base and typically useful options.
    builtin emulate -LR zsh
    builtin setopt extendedglob warncreateglobal typesetsilent noshortloops rcquotes

    local type=$1 id=$2 id_as=$3 args=$4 ices=$5 hook=$6 subtype=$7

    # Conditionally enable the output from this annex
    integer ibshow_messages=$+ICE[debug]

    # Exclude IDs with slashes and also packages
    [[ $id == */* ]] && local bad_id=1 
    [[ $id == *:* ]] && local bad_id2=1
    [[ $ICE[teleid] == */* ]] && local bad_tele=1
    (( $+ICE[pack] )) && local is_pack=1
    if (( bad_id || bad_id2 || bad_tele || is_pack )) {
        if (( ibshow_messages )) {
            local -a msgs
            msgs=( ${bad_id:+the ID contains a slash} ${bad_id2:+the ID looks like a snippet ID}
                   ${bad_tele:+"the {ice}teleid{meta}''{msg} ice contains a slash"}
                   ${is_pack:+"the ID is a package / {ice}pack{meta}''{msg} ice given"} )
            +zinit-message "{pre}unscope annex: {msg}Skipping the ID: {meta2}$id{msg}" \
                "(reason(s): ${(j:, :)msgs}).{rst}"
        }
        return 0
    }

    local -a match mbegin mend reply
    local MATCH REPLY
    integer MBEGIN MEND ibghapi=${+ICE[ghapi]}

    local -A tpe_map
    tpe_map=( 0 plugin 1 snippet )

    # Strip the ID from some possible accidental /-s.
    id=${${id#@}%%(///|//|/)}

    # I've pressed C-J. Will now press C-J and C-H a few times
    # So the completion-method switching doesn't seem to work.
    if { za-unscope-scope-cmd -rt "$___id" ${${(M)ibghapi:#0}:+-n} } {

        if (( Zinit_Annex_Unscope[enable-output] )) {
            # Clear the messages-flag, for cleanness
            Zinit_Annex_Unscope[enable-output]=0
            
            # Enable the output from this annex
            # [Updated to disable via configuration]
            if (( $+ZINIT_ANNNEX_UNSCOPE_HUMBLE ))
            then
                integer ibshow_messages=0
            else
                integer ibshow_messages=1
            fi
        }

        local -a object_data
        object_data=( "${(@Q)${(@z)REPLY}}" )

        # Overwrite the outside parameters in order to introduce
        # the actual object's ID.
        ICE[id-as]=${id_as:-$___id}
        ___id=$object_data[1]       # main ID
        ___ehid=${ICE[id-as]} # handler ID
        ___etid=$object_data[1]     # teleid
        ICE[teleid]=$object_data[1]

        # Test if we're being called from the object-load context.
        if [[ $subtype == load ]] {
            # Possibly change the object type.
            ___is_snippet=$object_data[2]
        }

        # Debug message.
        if (( ibshow_messages )) {
            (($+functions[.zinit-two-paths])) || builtin source $ZINIT[BIN_DIR]/zinit-side.zsh
            .zinit-any-colorify-as-uspl2 $ICE[teleid]
            +zinit-message "{pre}unscope annex: {msg}Matched the key:" \
                "{meta}$id {ehi}→{rst} $REPLY" \
                "{msg}({meta2}$tpe_map[$object_data[2]]{msg}).{rst}"
        }
    }

    return 0
}

# vim:ft=zsh:tw=80:sw=4:sts=4:et
