// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package dig

import (
	
	
	
	
	
	

	
)

// A ScopeOption modifies the default behavior of Scope; currently,
// there are no implementations.
type ScopeOption interface {
	noScopeOption() // yet
}

// Scope is a scoped DAG of types and their dependencies.
// A Scope may also have one or more child Scopes that inherit
// from it.
type Scope struct {
	// This implements containerStore interface.

	// Name of the Scope
	name string
	// Mapping from key to all the constructor node that can provide a value for that
	// key.
	providers map[key][]*constructorNode

	// Mapping from key to the decorator that decorates a value for that key.
	decorators map[key]*decoratorNode

	// constructorNodes provided directly to this Scope. i.e. it does not include
	// any nodes that were provided to the parent Scope this inherited from.
	nodes []*constructorNode

	// Values that generated via decorators in the Scope.
	decoratedValues map[key]reflect.Value

	// Values that generated directly in the Scope.
	values map[key]reflect.Value

	// Values groups that generated directly in the Scope.
	groups map[key][]reflect.Value

	// Values groups that generated via decoraters in the Scope.
	decoratedGroups map[key]reflect.Value

	// Source of randomness.
	rand *rand.Rand

	// Flag indicating whether the graph has been checked for cycles.
	isVerifiedAcyclic bool

	// Defer acyclic check on provide until Invoke.
	deferAcyclicVerification bool

	// Recover from panics in user-provided code and wrap in an exported error type.
	recoverFromPanics bool

	// invokerFn calls a function with arguments provided to Provide or Invoke.
	invokerFn invokerFn

	// graph of this Scope. Note that this holds the dependency graph of all the
	// nodes that affect this Scope, not just the ones provided directly to this Scope.
	gh *graphHolder

	// Parent of this Scope.
	parentScope *Scope

	// All the child scopes of this Scope.
	childScopes []*Scope

	// clockSrc stores the source of time. Defaults to system clock.
	clockSrc digclock.Clock
}

func newScope() *Scope {
	 := &Scope{
		providers:       make(map[key][]*constructorNode),
		decorators:      make(map[key]*decoratorNode),
		values:          make(map[key]reflect.Value),
		decoratedValues: make(map[key]reflect.Value),
		groups:          make(map[key][]reflect.Value),
		decoratedGroups: make(map[key]reflect.Value),
		invokerFn:       defaultInvoker,
		rand:            rand.New(rand.NewSource(time.Now().UnixNano())),
		clockSrc:        digclock.System,
	}
	.gh = newGraphHolder()
	return 
}

// Scope creates a new Scope with the given name and options from current Scope.
// Any constructors that the current Scope knows about, as well as any modifications
// made to it in the future will be propagated to the child scope.
// However, no modifications made to the child scope being created will be propagated
// to the parent Scope.
func ( *Scope) ( string,  ...ScopeOption) *Scope {
	 := newScope()
	.name = 
	.parentScope = 
	.invokerFn = .invokerFn
	.clockSrc = .clockSrc
	.deferAcyclicVerification = .deferAcyclicVerification
	.recoverFromPanics = .recoverFromPanics

	// child copies the parent's graph nodes.
	for ,  := range .gh.nodes {
		.gh.nodes = append(.gh.nodes, )
		if ,  := .Wrapped.(*constructorNode);  {
			.CopyOrder(, )
		}
	}

	for ,  := range  {
		.noScopeOption()
	}

	.childScopes = append(.childScopes, )
	return 
}

// ancestors returns a list of scopes of ancestors of this scope up to the
// root. The scope at at index 0 is this scope itself.
func ( *Scope) () []*Scope {
	var  []*Scope
	for  := ;  != nil;  = .parentScope {
		 = append(, )
	}
	return 
}

func ( *Scope) ( []*Scope) []*Scope {
	 = append(, )
	for ,  := range .childScopes {
		 = .()
	}
	return 
}

func ( *Scope) () []containerStore {
	 := .ancestors()
	 := make([]containerStore, len())
	for ,  := range  {
		[] = 
	}
	return 
}

func ( *Scope) () []reflect.Type {
	 := make(map[reflect.Type]struct{}, len(.providers))
	for  := range .providers {
		[.t] = struct{}{}
	}

	 := make([]reflect.Type, 0, len())
	for  := range  {
		 = append(, )
	}
	sort.Sort(byTypeName())
	return 
}

func ( *Scope) ( string,  reflect.Type) ( reflect.Value,  bool) {
	,  = .values[key{name: , t: }]
	return
}

func ( *Scope) ( string,  reflect.Type) ( reflect.Value,  bool) {
	,  = .decoratedValues[key{name: , t: }]
	return
}

func ( *Scope) ( string,  reflect.Type,  reflect.Value) {
	.values[key{name: , t: }] = 
}

func ( *Scope) ( string,  reflect.Type,  reflect.Value) {
	.decoratedValues[key{name: , t: }] = 
}

func ( *Scope) ( string,  reflect.Type) []reflect.Value {
	 := .groups[key{group: , t: }]
	// shuffle the list so users don't rely on the ordering of grouped values
	return shuffledCopy(.rand, )
}

func ( *Scope) ( string,  reflect.Type) (reflect.Value, bool) {
	,  := .decoratedGroups[key{group: , t: }]
	return , 
}

func ( *Scope) ( string,  reflect.Type,  reflect.Value) {
	 := key{group: , t: }
	.groups[] = append(.groups[], )
}

func ( *Scope) ( string,  reflect.Type,  reflect.Value) {
	 := key{group: , t: }
	.decoratedGroups[] = 
}

func ( *Scope) ( string,  reflect.Type) []provider {
	return .getProviders(key{name: , t: })
}

func ( *Scope) ( string,  reflect.Type) []provider {
	return .getProviders(key{group: , t: })
}

func ( *Scope) ( string,  reflect.Type) (decorator, bool) {
	return .getDecorators(key{name: , t: })
}

func ( *Scope) ( string,  reflect.Type) (decorator, bool) {
	return .getDecorators(key{group: , t: })
}

func ( *Scope) ( key) (decorator, bool) {
	,  := .decorators[]
	return , 
}

func ( *Scope) ( key) []provider {
	 := .providers[]
	 := make([]provider, len())
	for ,  := range  {
		[] = 
	}
	return 
}

func ( *Scope) ( string,  reflect.Type) []provider {
	return .getAllProviders(key{group: , t: })
}

func ( *Scope) ( string,  reflect.Type) []provider {
	return .getAllProviders(key{name: , t: })
}

func ( *Scope) ( key) []provider {
	 := .ancestors()
	var  []provider
	for ,  := range  {
		 = append(, .getProviders()...)
	}
	return 
}

func ( *Scope) () invokerFn {
	return .invokerFn
}

func ( *Scope) () digclock.Clock {
	return .clockSrc
}

// adds a new graphNode to this Scope and all of its descendent
// scope.
func ( *Scope) ( interface{},  map[*Scope]int) {
	[] = .gh.NewNode()
	for ,  := range .childScopes {
		.(, )
	}
}

func ( *Scope) ( []int) error {
	var  []cycleErrPathEntry
	for ,  := range  {
		if ,  := .gh.Lookup().(*constructorNode);  {
			 = append(, cycleErrPathEntry{
				Key: key{
					t: .CType(),
				},
				Func: .Location(),
			})
		}
	}
	return errCycleDetected{Path: , scope: }
}

// Returns the root Scope that can be reached from this Scope.
func ( *Scope) () *Scope {
	 := 
	for .parentScope != nil {
		 = .parentScope
	}
	return 
}

// String representation of the entire Scope
func ( *Scope) () string {
	 := &bytes.Buffer{}
	fmt.Fprintln(, "nodes: {")
	for ,  := range .providers {
		for ,  := range  {
			fmt.Fprintln(, "\t", , "->", )
		}
	}
	fmt.Fprintln(, "}")

	fmt.Fprintln(, "values: {")
	for ,  := range .values {
		fmt.Fprintln(, "\t", , "=>", )
	}
	for ,  := range .groups {
		for ,  := range  {
			fmt.Fprintln(, "\t", , "=>", )
		}
	}
	fmt.Fprintln(, "}")

	return .String()
}