// db.Base extensions. Currently purely about base methods needed for JSON snapshots

'use strict';

var memoize  = require('memoizee/plain')
  , ensureDb = require('dbjs/valid-dbjs');

module.exports = memoize(function (db) {
	ensureDb(db).Base.prototype.__descriptorPrototype__.setProperties({
		// Field (label: value) JSON serializer
		fieldToJSON: function (ignore) {
			return {
				label: this.dynamicLabelKey ? this.object.get(this.dynamicLabelKey) : this.label,
				value: this.type.valueToJSON(this.object.get(this.key))
			};
		}
	});

	db.Base.prototype.defineProperties({
		// Whether value should be considered as empty
		isEmpty: { type: db.Function },
		toJSON: { type: db.Function }
	});

	return db.Base.defineProperties({
		// Whether provided value should be considered empty
		isValueEmpty: { type: db.Function, value: function (value) {
			if (value == null) return true;
			if (typeof value.isEmpty !== 'function') return false;
			return value.isEmpty();
		} },
		// Returns JSON representation of provided value (in many cases it's same as value)
		valueToJSON: { type: db.Function, value: function (value) {
			if (value == null) return value;
			if (typeof value.toJSON === 'function') return value.toJSON(this);
			if (this.database.isObjectType(this)) value = value.toString(this);
			return (new this(value)).toString(this);
		} }
	});
}, { normalizer: require('memoizee/normalizers/get-1')() });

@@ -522,16 +522,22 @@
 his.key)
+, this
 )%0A%09%09%09%7D;%0A
@@ -1121,36 +1121,48 @@
  function (value
+, descriptor
 ) %7B%0A
-
 %09%09%09if (value == 
@@ -1177,24 +1177,68 @@
 turn value;%0A
+%09%09%09if (this.database.isObjectType(this)) %7B%0A%09
 %09%09%09if (typeo
@@ -1302,75 +1302,48 @@
 %0A%09%09%09
-if (this.database.isObjectType(this)) value = value.toString(this);
+%09return value.toString(descriptor);%0A%09%09%09%7D
 %0A%09%09%09
@@ -1376,20 +1376,26 @@
 oString(
-this
+descriptor
 );%0A%09%09%7D %7D

Fix toJSON resolution