package downloader

import (
	
	
	

	

	
	
	
)

// nolint:gocognit
func ( *Downloader) (
	 context.Context,  *reader,  sink,
	 int,  io.WriterAt,
) (tg.StorageFileTypeClass, error) {
	var (
		    tg.StorageFileTypeClass
		 sync.Mutex
	)

	 := tdsync.NewReady()
	 := tdsync.NewCancellableGroup()
	 := make(chan block, )

	 := func( tg.StorageFileTypeClass) {
		// Blocks past known end of file (see Builder.WithSize) carry no type:
		// keep the first type reported by a server response.
		if  != nil {
			.Lock()
			if  == nil {
				 = 
			}
			.Unlock()
		}
		.Signal()
	}

	// Download loop
	.Go(func( context.Context) error {
		 := tdsync.NewCancellableGroup()
		defer close()

		for  := 0;  < ; ++ {
			.Go(func( context.Context) error {
				for {
					select {
					case <-.Done():
						return .Err()
					case <-.Ready():
						return nil
					default:
					}

					,  := .Next()
					if  != nil {
						return errors.Wrap(, "get file")
					}

					// If returned chunk is zero, that means we read all file.
					 := len(.data)
					if  < 1 {
						(.tag)
						return nil
					}

					select {
					case <-.Done():
						return .Err()
					case  <- :
					}

					if .last() {
						(.tag)
						return nil
					}
				}
			})
		}

		return .Wait()
	})

	// Write loop
	.Go(writeAtLoop(syncio.NewWriterAt(), , ))

	 := .Wait()
	.Lock()
	defer .Unlock()
	return , 
}