"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 _uuid = require("uuid");

var _uuid2 = _interopRequireDefault(_uuid);

var _lodash = require("lodash.clonedeep");

var _lodash2 = _interopRequireDefault(_lodash);

var _lodash3 = require("lodash.reduce");

var _lodash4 = _interopRequireDefault(_lodash3);

var _ObjectProperty2 = require("../ObjectProperty");

var _ObjectProperty3 = _interopRequireDefault(_ObjectProperty2);

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

var _Shadow3 = _interopRequireDefault(_Shadow2);

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

var _StateType2 = _interopRequireDefault(_StateType);

var _Store = require("../Store");

var _Store2 = _interopRequireDefault(_Store);

var _PojoQueryBuilder = require("./PojoQueryBuilder");

var _PojoQueryBuilder2 = _interopRequireDefault(_PojoQueryBuilder);

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

/*

*/

var PojoEndpointProperty = function (_ObjectProperty) {
	_inherits(PojoEndpointProperty, _ObjectProperty);

	function PojoEndpointProperty() {
		var pojo = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

		_classCallCheck(this, PojoEndpointProperty);

		var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(PojoEndpointProperty).call(this, PojoEndpointProperty.type));

		_this.setShadowClass(PojoEndpointShadow);

		_this.id = (0, _uuid2.default)();

		// model objects kept in instance variable instead of state so direct changes (manual) do not
		// cause a store change
		_this.models = pojo;
		return _this;
	}

	return PojoEndpointProperty;
}(_ObjectProperty3.default);

exports.default = PojoEndpointProperty;


_StateType2.default.defineType(PojoEndpointProperty, function (spec) {
	return spec.initialState({});
});

var PojoEndpointShadow = function (_Shadow) {
	_inherits(PojoEndpointShadow, _Shadow);

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

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

	_createClass(PojoEndpointShadow, [{
		key: "isConnected",
		value: function isConnected() {
			return true;
		}
	}, {
		key: "queryBuilder",
		value: function queryBuilder() {
			return new _PojoQueryBuilder2.default();
		}
	}, {
		key: "doCreate",
		value: function doCreate(shadowModel, model) {
			var obj = (0, _lodash2.default)(model);

			if (!obj.id) {
				obj.id = (0, _uuid2.default)();
			}

			this.models[obj.id] = obj;

			return _Store2.default.resolve(obj);
		}
	}, {
		key: "doDelete",
		value: function doDelete(id) {
			delete this.models[id];

			return _Store2.default.resolve(id);
		}
	}, {
		key: "doFetch",
		value: function doFetch(filter) {
			var models = filter ? filter.on(this.models) : Object.values(this.models);

			return _Store2.default.resolve(models);
		}
	}, {
		key: "doFind",
		value: function doFind(id) {
			return _Store2.default.resolve(this.models[id]);
		}
	}, {
		key: "doUpdate",
		value: function doUpdate(id, shadowModel, changedProps) {
			this.models[id] = changedProps;

			return _Store2.default.resolve(changedProps);
		}
	}, {
		key: "id",
		get: function get() {
			return this.$$().id;
		}
	}, {
		key: "models",
		get: function get() {
			return this.$$().models;
		}
	}]);

	return PojoEndpointShadow;
}(_Shadow3.default);