"use strict";

exports.__esModule = true;
exports.default = createPropertyClass;

var _Shadow = require("./Shadow");

var _Shadow2 = _interopRequireDefault(_Shadow);

var _StateType = require("./StateType");

var _StateType2 = _interopRequireDefault(_StateType);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/*
	Factory function for creating an Property subclass suitable for using with new.

	Parameters (all are optional):
		shadowType: one of a pojo or a class. This parameter defines the new property's shadow
			type. If pojo specified, each property and function is mapped onto a subclass of the
			BaseShadowClass parameter. The BaseShadowClass parameter is ignored if this parameter
			is a class.
		stateType: a StateType instance defining the Property
		specCallback: a callback function that will be passed the StateType spec for additional
			customization, such as setting autoshadow, initial state, or readonly.
		PropertyClass: the base class for defining the property. Must be a Property subclass.
		BaseShadowClass: The base shadow class for when shadowType parameter is not a class/function
			(default is Shadow)
*/
function createPropertyClass(shadowType, initialState, specCallback, PropertyClass) {
	var BaseShadowClass = arguments.length <= 4 || arguments[4] === undefined ? _Shadow2.default : arguments[4];

	var ShadowClass;

	// get the shadow class
	if (typeof shadowType === 'function') {
		// shadow class passed into method
		ShadowClass = shadowType;
	} else {
		var CustomShadow = function (_BaseShadowClass) {
			_inherits(CustomShadow, _BaseShadowClass);

			function CustomShadow() {
				_classCallCheck(this, CustomShadow);

				return _possibleConstructorReturn(this, Object.getPrototypeOf(CustomShadow).apply(this, arguments));
			}

			return CustomShadow;
		}(BaseShadowClass);

		var proto = CustomShadow.prototype;
		var names = Object.getOwnPropertyNames(shadowType);
		var name, desc;

		for (var i = 0, len = names.length; i < len; i++) {
			name = names[i];
			desc = Object.getOwnPropertyDescriptor(shadowType, name);

			Object.defineProperty(proto, name, desc);
		}

		ShadowClass = CustomShadow;
	}

	// create the property subclass

	var CustomProperty = function (_PropertyClass) {
		_inherits(CustomProperty, _PropertyClass);

		function CustomProperty(stateType) {
			_classCallCheck(this, CustomProperty);

			var _this2 = _possibleConstructorReturn(this, Object.getPrototypeOf(CustomProperty).call(this, stateType));

			_this2.setShadowClass(ShadowClass);
			return _this2;
		}

		return CustomProperty;
	}(PropertyClass);

	// create the type class variable


	_StateType2.default.defineType(CustomProperty, function (spec) {
		if (initialState !== undefined) {
			spec.initialState(initialState);
		}

		if (specCallback) {
			specCallback(spec, CustomProperty);
		}
	});

	return CustomProperty;
}