package ecs

// Resource provides access to a world resource.
//
// Create one with [NewResource].
type Resource[ any] struct {
	world *World
	id    ResID
}

// New creates a new [Resource]. It is safe to call on an uninitialized instance.
// It is a helper method, intended to avoid repeated specification of the type parameter.
func (Resource[]) ( *World) Resource[] {
	return NewResource[]()
}

// NewResource creates a new [Resource] mapper for a resource type.
// This does not add a resource to the world, but only creates a mapper for resource access!
//
// See also [World.Resources].
func [ any]( *World) Resource[] {
	return Resource[]{
		id:    ResourceID[](),
		world: ,
	}
}

// Add adds a resource to the world.
//
// Panics if there is already a resource of the given type.
func ( *Resource[]) ( *) {
	.world.Resources().Add(.id, )
}

// Remove removes a resource from the world.
//
// Panics if there is no resource of the given type.
//
// See also [ecs.Resources.Remove].
func ( *Resource[]) () {
	.world.Resources().Remove(.id)
}

// Get gets the resource of the given type.
//
// Returns nil if there is no such resource.
func ( *Resource[]) () * {
	 := .world.Resources().Get(.id)
	if  == nil {
		return nil
	}
	return .(*)
}

// Has returns whether the world has the resource type.
func ( *Resource[]) () bool {
	return .world.Resources().Has(.id)
}