package reg

import (
	"reflect"

	"gitlink.org.cn/cloudream/common/utils/reflect2"
	clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
	"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
	cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
)

type BuilderCtor func(detail *clitypes.UserSpaceDetail) types.StorageBuilder

var StorageBuilders = make(map[reflect.Type]BuilderCtor)

// 注册针对指定存储服务类型的Builder
func RegisterBuilder[T cortypes.StorageType](ctor BuilderCtor) {
	StorageBuilders[reflect2.TypeOf[T]()] = ctor
}

// 注：此函数只给storage包内部使用，外部包请使用外层的factory.GetBuilder
// 此函数永远不会返回nil。如果找不到对应的Builder，则会返回EmptyBuilder，
// 此Builder的所有函数都会返回否定值或者封装后的ErrUnsupported错误（需要使用errors.Is检查）
func GetBuilderInternal(detail *clitypes.UserSpaceDetail) types.StorageBuilder {
	typ := reflect.TypeOf(detail.Storage.Type)

	ctor, ok := StorageBuilders[typ]
	if !ok {
		return &types.EmptyBuilder{}
	}

	return ctor(detail)
}
