package downloader

import (
	
	

	
)

// sink tracks download progress.
type sink struct {
	done     int64
	total    int64
	progress ProgressFunc
}

func ( *sink) ( int) {
	.done += int64()
	if .progress != nil {
		.progress(.done, .total)
	}
}

func ( io.WriterAt,  sink,  <-chan block) func(context.Context) error {
	return func( context.Context) error {
		for {
			select {
			case <-.Done():
				return .Err()
			case ,  := <-:
				if ! {
					return nil
				}

				,  := .WriteAt(.data, .offset)
				if  != nil {
					return errors.Wrap(, "write output")
				}
				.written(len(.data))
			}
		}
	}
}

func ( io.Writer,  sink,  <-chan block) func(context.Context) error {
	return func( context.Context) error {
		for {
			select {
			case <-.Done():
				return .Err()
			case ,  := <-:
				if ! {
					return nil
				}

				,  := .Write(.data)
				if  != nil {
					return errors.Wrap(, "write output")
				}
				.written(len(.data))
			}
		}
	}
}