package downloader

import (
	
	
	

	

	
	
	
)

// nolint:gocognit
func ( *Downloader) (
	 context.Context,  *reader,
	 int,  io.WriterAt,
) (tg.StorageFileTypeClass, error) {
	var  tg.StorageFileTypeClass
	 := &sync.Once{}

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

	 := func( tg.StorageFileTypeClass) {
		.Do(func() {
			 = 
		})
		.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(), ))

	return , .Wait()
}