'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.wrapEditorInterface = wrapEditorInterface;
var _cloneDeep = require('lodash/cloneDeep');
var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
var _freezeSys = require('contentful-sdk-core/freeze-sys');
var _freezeSys2 = _interopRequireDefault(_freezeSys);
var _enhanceWithMethods = require('../enhance-with-methods');
var _enhanceWithMethods2 = _interopRequireDefault(_enhanceWithMethods);
var _toPlainObject = require('contentful-sdk-core/mixins/to-plain-object');
var _toPlainObject2 = _interopRequireDefault(_toPlainObject);
var _omit = require('lodash/omit');
var _omit2 = _interopRequireDefault(_omit);
var _errorHandler = require('../error-handler');
var _errorHandler2 = _interopRequireDefault(_errorHandler);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Editor Interface instances
* @namespace EditorInterface
*/
function createEditorInterfaceApi(http) {
return {
/**
* Sends an update to the server with any changes made to the object's properties
* @memberof EditorInterface
* @func update
* @return {Promise<EditorInterface>} Object returned from the server with updated changes.
* @example
* editorInterface.controls[0] = { "fieldId": "title", "widgetId": "singleLine"}
* editorInterface.update()
* .then(editorInterface => console.log(editorInterface.controls))
*/
update: function update() {
var raw = this.toPlainObject();
var data = (0, _omit2.default)(raw, ['sys']);
return http.put('content_types/' + this.sys.contentType.sys.id + '/editor_interface', data, {
headers: { 'X-Contentful-Version': this.sys.version }
}).then(function (response) {
return wrapEditorInterface(http, response.data);
}, _errorHandler2.default);
},
/**
* gets a control for a specific field
* @memberof EditorInterface
* @func getControlForField
* @return {?Object} control object for specific field.
* @example
* const control = editorInterface.getControlForField('fieldId')
* console.log(control)
*/
getControlForField: function getControlForField(fieldId) {
var result = this.controls.filter(function (control) {
return control.fieldId === fieldId;
});
return result && result.length > 0 ? result[0] : null;
}
};
}
/**
* @private
* @param {Object} http - HTTP client instance
* @param {Object} data - Raw editor-interface data
* @return {EditorInterface} Wrapped editor-interface data
*/
function wrapEditorInterface(http, data) {
var editorInterface = (0, _toPlainObject2.default)((0, _cloneDeep2.default)(data));
(0, _enhanceWithMethods2.default)(editorInterface, createEditorInterfaceApi(http));
return (0, _freezeSys2.default)(editorInterface);
}