# STATUS: Untested

NamedFunction = require "./NamedFunction"

# The beginning of the Type singleton.
Type = module.exports = NamedFunction "Type", -> 
	return _factory.apply this, arguments

# Create a Factory to create new Types with. 
# This will be called in ./../Type
Type.init = (opts) ->
	_factory = Factory opts
	delete Type.init

Type.register = (types) ->
	types = Array::slice.call arguments unless testType types, Array
	types.map _registerType

#
# Internal
#

{ testType, setType, validateTypes } = require "./TypeUtils"

Factory = require "./Factory"
Constructor = require "./Constructor"
Null = require "./../Null"
Undefined = require "./../Undefined"
Validator = require "./Validator"
Property = require "./../Property"

# Constructs new Types.
# This is later replaced in ./../Type
_factory = (opts) ->
	validateTypes opts,
		name: [String]
		constructor: [Function]
	type = NamedFunction opts.name, opts.constructor
	return setType type, Type

# Registers a Function as a Type.
_registerType = (type) ->
	return false unless testType type, Function
	setType type, Type
	Object.defineProperty type, "isConstructable", { value: true }
	return true

Type.register [
	Array
	Boolean
	Date
	Error
	Function
	Number
	Null
	Object
	Property
	RegExp
	String
	Symbol ? null
	Undefined
	Validator
]
