"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 _createPropertyClass = require("./createPropertyClass");

var _createPropertyClass2 = _interopRequireDefault(_createPropertyClass);

var _IndexedApi = require("./IndexedApi");

var _IndexedApi2 = _interopRequireDefault(_IndexedApi);

var _IndexedShadow = require("./IndexedShadow");

var _IndexedShadow2 = _interopRequireDefault(_IndexedShadow);

var _IndexedShadowImpl = require("./IndexedShadowImpl");

var _IndexedShadowImpl2 = _interopRequireDefault(_IndexedShadowImpl);

var _MapProperty = require("./MapProperty");

var _MapProperty2 = _interopRequireDefault(_MapProperty);

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

var _Property3 = _interopRequireDefault(_Property2);

var _PropertyFactoryShader = require("./PropertyFactoryShader");

var _PropertyFactoryShader2 = _interopRequireDefault(_PropertyFactoryShader);

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

/**
	`IndexedProperty` models an [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
	while limiting the shadow API to the non-mutating `Array` methods, such as `filter()`, `find()`, `indexOf()`,
	and `map()` to name a few. The {@link IndexedShadow} allows for random access and assignment, such as
	`todos[1].desc`.

	The default shadow class is {@link IndexedShadow}.

	Use `IndexedProperty` when you want to model an array state property without exposing mutating functions
	like `push()`, `remove()`, and `splice()`. You can then specify a custom shadow api to expose mutation
	methods that involve implementation logic.

	The `_indexed` instance variable exposes a {@link IndexedApi} instance for working with properties.
	Use `this._indexed` to perform `array` mutations, such as `pop()`, `push()`, `shift()`, and `splice()`.
	For example,

	```js
	this._indexed.push("new element");
	```
	would append "new element" to the array.

	@see {@link IndexedShadow}
*/

var IndexedProperty = function (_Property) {
	_inherits(IndexedProperty, _Property);

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

		var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(IndexedProperty).call(this, stateType));

		_this._indexed = new _IndexedApi2.default(_this);

		_this.setImplementationClass(_IndexedShadowImpl2.default);
		_this.setShadowClass(_IndexedShadow2.default);
		return _this;
	}

	/**
 	Factory function for creating an `IndexedProperty` subclass. The generated class will have
 	the `type` {@link StateType} descriptor set upon return.
 		Example usage:
 	```
 	class SomeShadow extends IndexedShadow {
 	    // definition here
 	}
 		export default IndexedProperty.createClass(SomeShadow, type => {
 		// configure type variable
 	});
 	```
 		@param {Object|IndexedShadow} [shadowType={}] - `IndexedShadow` subclass or object literal api definition.
 		If object literal specified, each property and function is mapped onto a Shadow subclass.
 	@param {function(type: StateType)} [typeCallback] - a callback function that will be passed the
 		{@link StateType} spec for additional customization, such as setting autoshadow, initial state,
 		or readonly.
 	@param {Object} [initialState]=[] - the initial state for the new property.
 		@return {IndexedProperty} newly defined `IndexedProperty` subclass.
 */


	_createClass(IndexedProperty, null, [{
		key: "createClass",
		value: function createClass() {
			var shadowType = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
			var typeCallback = arguments[1];
			var initialState = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];

			return (0, _createPropertyClass2.default)(shadowType, initialState, typeCallback, IndexedProperty, _IndexedShadow2.default);
		}

		/**
  	Factory function for setting up the {@link StateType} `type` class variable with an appropriately
  	configured intial state.
  		Example usage:
  	```
         export default class SomeProperty extends IndexedProperty {
  		// implement property here
         }
          class SomeShadow extends IndexedShadow {
  		// implement shadow api here
         }
          MapProperty.defineType(SomeProperty, SomeShadow, type => {
  		// configure type variable
         });
  	```
  		@param {IndexedProperty} PropClass - IndexedProperty subclass
  	@param {Object|IndexedShadow} [ShadowType] - `IndexedShadow` subclass or object literal api definition.
  		If object literal specified, each property and function is mapped onto a Shadow subclass.
  	@param {function(type: StateType)} [typeCallback] - a callback function that will be passed the
  		{@link StateType} spec for additional customization, such as setting autoshadow, initial state, or
  		readonly.
  	@param {Object} [initialState=[]] - the initial state for the new property.
  */

	}, {
		key: "defineType",
		value: function defineType(PropClass, ShadowType, typeCallback) {
			var initialState = arguments.length <= 3 || arguments[3] === undefined ? [] : arguments[3];

			(0, _akutils.assert)(function (a) {
				return a.is(IndexedProperty.isPrototypeOf(PropClass), "PropClass must subclass IndexedProperty");
			});

			_StateType2.default.defineTypeEx(PropClass, ShadowType, typeCallback, initialState);
		}
	}]);

	return IndexedProperty;
}(_Property3.default);

exports.default = IndexedProperty;


_StateType2.default.defineType(IndexedProperty, function (spec) {
	spec.initialState([]).elementType(_MapProperty2.default.type).typeName("IndexedProperty");
});