package future

import (
	
	
)

type ValueFutureResolver[ any] func(value , err error)

// A Future represents a value that will be available in the Future.
// It is always associated with a context that can be used to wait for the value to be available.
// When the parent context is canceled, the Future will be canceled as well.
type ValueFuture[ any] struct {
	ctx context.Context
}

func ( *ValueFuture[]) () <-chan struct{} {
	return .ctx.Done()
}

func ( *ValueFuture[]) () (, error) {
	<-.ctx.Done()

	 := context.Cause(.ctx)
	if  != nil {
		if ,  := .(*valueFutureResolution[]);  {
			return .value, .err
		}
	}
	var  
	return , 
}

// Get waits for the future to complete and returns the output and any error that occurred.
func ( *ValueFuture[]) () (, error) {
	return .Result()
}

func [ any]( context.Context) (*ValueFuture[], ValueFutureResolver[]) {
	,  := context.WithCancelCause()
	 := &ValueFuture[]{
		ctx: ,
	}
	return , func( ,  error) {
		(&valueFutureResolution[]{
			value: ,
			err:   ,
		})
	}
}

type valueFutureResolution[ any] struct {
	value 
	err   error
}

func ( *valueFutureResolution[]) () string {
	if .err != nil {
		return .err.Error()
	}
	return fmt.Sprintf("future resolved: %#v", .value)
}