# shellcheck disable=SC2154,SC3043,SC2034,SC3028 shell=sh

# We should use 7zip for better compresssion rate: `x 7z a --ttar "$target_file" "$@"`
___x_cmd_zuz_z_tar(){
    if [ "$#" -eq 0 ]; then
        if [ "$size" != 0 ]; then           LC_ALL="$___X_CMD_LOCALE_DEF_C" tar cf - --files-from "/dev/stdin" | ___x_cmd_zuz_run ___x_cmd 7z a -v"$size" "$target_file" && ___x_cmd_zuz_z_pipe_volume_name    "$target_file"
        else                                LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_zuz_run tar cv --files-from "/dev/stdin" -f "$target_file"                   && ___x_cmd_zuz_z_pipe_name           "$target_file"
        fi
        return
    fi

    if [ "$size" != 0 ]; then               ___x_cmd_zuz_run ___x_cmd 7z a -v"$size" -ttar "$target_file" "$@" && ___x_cmd_zuz_z_pipe_volume_name  "$target_file"
    else                                    LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_zuz_run tar cvf "$target_file" "$@"                && ___x_cmd_zuz_z_pipe_name         "$target_file"
    fi
}

# NOTICE: 7z doesn't support resolving tar.Z volumes directly
___x_cmd_zuz_z_tarz(){
    ___x_cmd_hasbin compress || \
        N=zuz M="compress command not found. @see: https://command-not-found.com/compress" log:ret:1

    if [ "$#" -eq 0 ]; then
        # if [ "$size" != 0 ]; then         tar cZ --files-from "/dev/stdin" -f "${target_file}.$$.tmp" | ___x_cmd_zuz_run x 7z a -v"$size" -si -sdel "$target_file" "${target_file}.$$.tmp"
        if [ "$size" != 0 ]; then           zuz:error "tar.Z doesn't support creating volumes"; return 1
        else                                LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_zuz_run tar cZ --files-from "/dev/stdin" -f "$target_file" && ___x_cmd_zuz_z_pipe_name "$target_file"
        fi
        return
    fi

    if [ "$size" != 0 ]; then               zuz:error "tar.Z doesn't support creating volumes"; return 1
    else                                    LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_zuz_run tar cZf "$target_file" "$@" && ___x_cmd_zuz_z_pipe_name "$target_file"
    fi
}

___x_cmd_zuz_z_tgz(){
    if [ "$___X_CMD_ZUZ_ONEMODE" = 1 ];then
        [ "$1" != "${1%.tar*}" ] || target_file="${target_file%.tar*}.gz"
        ___x_cmd_zuz_z_many "$@"
        return
    fi

    ___x_cmd_zuz_z_7z_tool "$@"
}

___x_cmd_zuz_z_tar_xz(){
    if [ "$___X_CMD_ZUZ_ONEMODE" = 1 ];then
        [ "$1" != "${1%.tar*}" ] || target_file="${target_file%.tar*}.xz"
        ___x_cmd_zuz_z_many "$@"
        return
    fi
    ___x_cmd_zuz_z_7z_tool "$@"
}

___x_cmd_zuz_z_7z_tool(){
    if [ "$#" -eq 0 ]; then
        if [ "$size" != 0 ]; then           LC_ALL="$___X_CMD_LOCALE_DEF_C" tar cf - --files-from "/dev/stdin" | ___x_cmd_zuz_run x 7z a -si -v"$size" "$target_file" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
        else                                LC_ALL="$___X_CMD_LOCALE_DEF_C" tar cf - --files-from "/dev/stdin" | ___x_cmd_zuz_run x 7z a -si           "$target_file" && ___x_cmd_zuz_z_pipe_name        "$target_file"
        fi
        return
    fi

    if [ "$size" != 0 ]; then               LC_ALL="$___X_CMD_LOCALE_DEF_C" tar cf - "$@" | ___x_cmd_zuz_run x 7z a -si -v"$size" "$target_file" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
    else                                    LC_ALL="$___X_CMD_LOCALE_DEF_C" tar cf - "$@" | ___x_cmd_zuz_run x 7z a -si           "$target_file" && ___x_cmd_zuz_z_pipe_name        "$target_file"
    fi
}

___x_cmd_zuz_z_tar_zstd(){
    target_file="${target_file%.zst*}"
    [ "$size" = 0 ] || zuz:warn "zstd no support create volumes"
    LC_ALL="$___X_CMD_LOCALE_DEF_C" tar cf "$target_file" "$@"
    x zstd --rm "$target_file"
}

___x_cmd_zuz_z_many(){
    if [ "$#" -eq 0 ]; then
        if [ "$size" != 0 ]; then           ___x_cmd_zuz_run ___x_cmd 7z a -si -v"$size" "$target_file"< "$(___x_cmd_cmds_cat)" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
        else                                ___x_cmd_zuz_run ___x_cmd 7z a -si           "$target_file"< "$(___x_cmd_cmds_cat)" && ___x_cmd_zuz_z_pipe_name        "$target_file"
        fi
        return
    fi

    if [ "$size" != 0 ]; then               ___x_cmd_zuz_run ___x_cmd 7z a -v"$size" "$target_file" "$@" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
    else                                    ___x_cmd_zuz_run ___x_cmd 7z a           "$target_file" "$@" && ___x_cmd_zuz_z_pipe_name        "$target_file"
    fi
}

___x_cmd_zuz_z_zstd(){
    [ "$#" -le 1 ] || zuz:warn "zstd only support archive single file. Directory and multi file, Please use tar.zst"
    x zstd -c "$1" > "$target_file"
}

___x_cmd_zuz_z_zip(){
    if [ "$#" -eq 0 ]; then
        local _file_record_path
        _file_record_path="${target_file}.$(x rand int 1 99999).txt"
        if [ "$size" != 0 ]; then           command tee "$_file_record_path" | ___x_cmd_zuz_run ___x_cmd 7z a -tzip -r -v"$size" "$target_file" "@${_file_record_path}" && ___x_cmd_cmds_rm -f "$_file_record_path" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
        else                                command tee "$_file_record_path" | ___x_cmd_zuz_run ___x_cmd 7z a -tzip -r           "$target_file" "@${_file_record_path}" && ___x_cmd_cmds_rm -f "$_file_record_path" && ___x_cmd_zuz_z_pipe_name        "$target_file"
        fi
        return
    fi

    if [ "$size" != 0 ]; then               ___x_cmd_zuz_run ___x_cmd 7z a -tzip -r -v"$size" "$target_file" "$@" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
    else                                    ___x_cmd_zuz_run ___x_cmd 7z a -tzip -r           "$target_file" "$@" && ___x_cmd_zuz_z_pipe_name        "$target_file"
    fi
}

# NOTE: Wait static build. use 7zr command
___x_cmd_zuz_z_7z(){
    if [ "$#" -eq 0 ]; then
        local _file_record_path
        _file_record_path="${target_file}.$(x rand int 1 99999).txt"
        if [ "$size" != 0 ]; then           command tee "$_file_record_path" | ___x_cmd_zuz_run ___x_cmd 7z a -t7z -v"$size" "$target_file" "@${_file_record_path}" && ___x_cmd_cmds_rm -f "$_file_record_path" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
        else                                command tee "$_file_record_path" | ___x_cmd_zuz_run ___x_cmd 7z a -t7z           "$target_file" "@${_file_record_path}" && ___x_cmd_cmds_rm -f "$_file_record_path" && ___x_cmd_zuz_z_pipe_name        "$target_file"
        fi
        return
    fi

    if [ "$size" != 0 ]; then               ___x_cmd_zuz_run ___x_cmd 7z a -t7z -v"$size" "$target_file" "$@" && ___x_cmd_zuz_z_pipe_volume_name "$target_file"
    else                                    ___x_cmd_zuz_run ___x_cmd 7z a -t7z           "$target_file" "$@" && ___x_cmd_zuz_z_pipe_name        "$target_file"
    fi
}


___x_cmd_zuz_z_lha(){
    ___x_cmd os name_
    if [ "$___X_CMD_OS_NAME_" != "linux" ];then
        ___x_cmd_hasbin lha || { zuz:warn "The current x-cmd does not support the LHA package; please install LHA in advance."; return 1; }
        lha a "$target_file" "$@"
    else
        ___x_cmd_hasbin jlha || { zuz:warn "The current x-cmd does not support the LHA package; please install jlha in advance."; return 1; }
        jlha a "$target_file" "$@"
    fi
    return 0
}

# TODO: wait x rar
___x_cmd_zuz_z_rar(){
    zuz:warn "Currently we don't support creating rar archieve."
    zuz:warn "We would like to support it for our users in the future. But before that we have license issues to resolve."
    return 1
}

___x_cmd_zuz_z_pipe_name() { printf "%s\n" "$@"; }

___x_cmd_zuz_z_pipe_volume_name() { find . -maxdepth 1 -name "${1:?-"Provide target file name"}*"; }

