"use strict";

exports.__esModule = true;
exports.default = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _akutils = require("akutils");

var _noParentStateErrorMsg = require("./noParentStateErrorMsg");

var _noParentStateErrorMsg2 = _interopRequireDefault(_noParentStateErrorMsg);

var _Property = require("./Property");

var _Property2 = _interopRequireDefault(_Property);

var _Shader = require("./Shader");

var _Shader2 = _interopRequireDefault(_Shader);

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"); } }

/**
	Specialized shader that uses a {@link StateType} instance to instantiate a {@link Property}.
	Used by `KeyedApi` for adding child properties to the {@link Property#shader}.

	@use {@link KeyedApi}
	@see {@link Shader}
*/

var PropertyFactoryShader = function () {
	/*
 	Creates a factory that will create a shader for instantiating shadow properties for a property type.
 		Parameters:
 		stateType - StateType instance describing the property
 		parent - the parent Property instance
 */

	function PropertyFactoryShader(stateType, parent) {
		_classCallCheck(this, PropertyFactoryShader);

		this._stateType = stateType;
		this._parent = parent;
	}

	_createClass(PropertyFactoryShader, [{
		key: "addProperty",
		value: function addProperty(name, stateType) {
			(0, _akutils.assert)(function (a) {
				return a.is(stateType instanceof _StateType2.default, "stateType not a StateType");
			});

			if (!this.propertyShaderDefn) {
				this.propertyShaderDefn = {};
			}

			this.propertyShaderDefn[name] = stateType;
		}
	}, {
		key: "setElementType",
		value: function setElementType(stateType) {
			// ignoring since assuming set in StateType._setupShader() and also contained in the stateType var
			this.elementType = stateType;
		}
	}, {
		key: "shadowProperty",
		value: function shadowProperty(time, name, parentState, parentImpl) {
			var _this = this;

			(0, _akutils.assert)(function (a) {
				return a.is(parentState || !_this._parent, (0, _noParentStateErrorMsg2.default)(name, parentImpl));
			});

			// this should never happen but lets be safe
			if (!(0, _akutils.isSomething)(parentState)) {
				console.warn(this.noParentStateErrorMsg(name, parentImpl));

				return null;
			}

			var property = this._stateType.createProperty();
			var shader = property.shader();

			// set the proprety's parent property
			property.setParent(this._parent);

			this._configureShader(shader);

			return shader.shadowProperty(time, name, parentState, parentImpl);
		}
	}, {
		key: "shouldAutomount",
		value: function shouldAutomount() {
			var PropertyClass = this._stateType._PropertyClass;

			return PropertyClass.constructor.shouldAutomount && PropertyClass.constructor.shouldAutomount();
		}
	}, {
		key: "stateType",
		value: function stateType() {
			return this._stateType;
		}
	}, {
		key: "_configureShader",
		value: function _configureShader(shader) {
			var propertyShaderDefn = this.propertyShaderDefn;

			if (!propertyShaderDefn) {
				return;
			}

			var keys = Object.keys(propertyShaderDefn);
			var key, type;

			for (var i = 0, len = keys.length; i < len; i++) {
				key = keys[i];
				type = propertyShaderDefn[key];

				shader.addProperty(key, type);
			}
		}
	}]);

	return PropertyFactoryShader;
}();

exports.default = PropertyFactoryShader;