package exec

import (
	"github.com/tidbcloud/AgentTeam/desktop/internal/token"
)

func newRuntimeCredentialLease(
	authorizationID,
	value string,
	info token.ExecTokenInfo,
) RuntimeCredentialLease {
	return RuntimeCredentialLease{
		AuthorizationID: authorizationID,
		CredentialID:    info.JTI,
		Token:           value,
		ExpiresAt:       info.ExpiresAt,
	}
}

func closedChannel() <-chan struct{} {
	done := make(chan struct{})
	close(done)
	return done
}

func cloneRuntimeAuthorization(snapshot runtimeAuthorization) runtimeAuthorization {
	if snapshot.Server != nil {
		api := *snapshot.Server
		snapshot.Server = &api
	}
	return snapshot
}

func (s *RuntimeAuthorizationStore) runtimeCatalogForAuthorization(
	candidate *RuntimeCatalog,
) (*RuntimeCatalog, error) {
	if candidate == nil {
		return s.defaultRuntimeCatalog()
	}
	return NewRuntimeCatalog(candidate.builtin, candidate.ExtensionSnapshot())
}
