// Copyright (c) 2024 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 fx

import (
	
	
	
)

// broadcaster broadcasts signals to registered signal listeners.
// All methods on the broadcaster are concurrency-safe.
type broadcaster struct {
	// This lock is used to protect all fields of broadcaster.
	// Methods on broadcaster should protect all concurrent access
	// by taking this lock when accessing its fields.
	// Conversely, this lock should NOT be taken outside of broadcaster.
	m sync.Mutex

	// last will contain a pointer to the last ShutdownSignal received, or
	// nil if none, if a new channel is created by Wait or Done, this last
	// signal will be immediately written to, this allows Wait or Done state
	// to be read after application stop
	last *ShutdownSignal

	// contains channels created by Done
	done []chan os.Signal

	// contains channels created by Wait
	wait []chan ShutdownSignal
}

func ( *broadcaster) () {
	.m.Lock()
	defer .m.Unlock()
	.last = nil
}

// Done creates a new channel that will receive signals being broadcast
// via the broadcaster.
//
// If a signal has been received prior to the call of Done,
// the signal will be sent to the new channel.
func ( *broadcaster) () <-chan os.Signal {
	.m.Lock()
	defer .m.Unlock()

	 := make(chan os.Signal, 1)
	// If we had received a signal prior to the call of done, send it's
	// os.Signal to the new channel.
	// However we still want to have the operating system notify signals to this
	// channel should the application receive another.
	if .last != nil {
		 <- .last.Signal
	}
	.done = append(.done, )
	return 
}

// Wait creates a new channel that will receive signals being broadcast
// via the broadcaster.
//
// If a signal has been received prior to the call of Wait,
// the signal will be sent to the new channel.
func ( *broadcaster) () <-chan ShutdownSignal {
	.m.Lock()
	defer .m.Unlock()

	 := make(chan ShutdownSignal, 1)

	if .last != nil {
		 <- *.last
	}

	.wait = append(.wait, )
	return 
}

// Broadcast sends the given signal to all channels that have been created
// via Done or Wait. It does not block on sending, and returns an unsentSignalError
// if any send did not go through.
func ( *broadcaster) ( ShutdownSignal) error {
	.m.Lock()
	defer .m.Unlock()

	.last = &

	,  := .broadcast(
		,
		.broadcastDone,
		.broadcastWait,
	)

	if  != 0 {
		return &unsentSignalError{
			Signal: ,
			Total:  ,
			Unsent: ,
		}
	}

	return nil
}

func ( *broadcaster) (
	 ShutdownSignal,
	 ...func(ShutdownSignal) (int, int),
) (int, int) {
	var ,  int

	for ,  := range  {
		,  := ()
		 += 
		 += 
	}

	return , 
}

func ( *broadcaster) ( ShutdownSignal) (int, int) {
	var  int

	for ,  := range .done {
		select {
		case  <- .Signal:
		default:
			++
		}
	}

	return len(.done), 
}

func ( *broadcaster) ( ShutdownSignal) (int, int) {
	var  int

	for ,  := range .wait {
		select {
		case  <- :
		default:
			++
		}
	}

	return len(.wait), 
}

type unsentSignalError struct {
	Signal ShutdownSignal
	Unsent int
	Total  int
}

func ( *unsentSignalError) () string {
	return fmt.Sprintf(
		"send %v signal: %v/%v channels are blocked",
		.Signal,
		.Unsent,
		.Total,
	)
}