'use strict';

/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| IBM Cognos Products: Dashboard
 *| (C) Copyright IBM Corp. 2017, 2018
 *|
 *| US Government Users Restricted Rights - Use, duplication or disclosure
 *| restricted by GSA ADP Schedule Contract with IBM Corp.
 *+------------------------------------------------------------------------+
 */

define(['../../../../lib/@waca/dashboard-common/dist/core/Model', 'underscore'], function (Model, _) {
	'use strict';

	var ShapingModel = Model.extend({
		whitelistAttrs: ['shapingId', 'embeddedModuleId', 'embeddedModuleName', 'embeddedModuleUpToDate', 'moserJSON'],

		init: function init(model, options) {
			if (model) {
				_.defaults(model, {
					embeddedModuleUpToDate: true
				});
			}

			ShapingModel.inherited('init', this, arguments);
			if (this.moserJSON && !this.shapingId) {
				this.shapingId = '__initialShapingID__';
			}
			this.logger = options.logger;
			// Used for undo/redo to know if we're back to the initial spec
			this._initialShapingId = this.shapingId;

			// Used to track what moserJSON we currently have set in the moserJSON property
			this._currentMoserJSONID = this.shapingId;

			this._initialEmbeddedModuleUpToData = this.embeddedModuleUpToDate;
		},

		destroy: function destroy() {
			if (this.shapingHelper && this.shapingModelManager) {
				if (this.shapingModelManager.isTemporaryModule()) {
					if (this.shapingHelper.getModuleId() && this.shapingHelper.getModuleId().indexOf('_sessionTemp') > 0 || this.shapingHelper.getModuleURL() && this.shapingHelper.getModuleURL().indexOf('temp-service') > 0) {
						this.shapingHelper.deleteSessionModule();
					}
				}
				this.shapingModelManager.destroy();

				this.shapingHelper = null;
				this.shapingModelManager = null;
			}
		},

		getShapingId: function getShapingId() {
			return this.get('shapingId') || null;
		},

		setShapingHelper: function setShapingHelper(shapingHelper) {
			this.shapingHelper = shapingHelper;
		},

		setShapingModelManager: function setShapingModelManager(shapingModelManager) {
			this.shapingModelManager = shapingModelManager;
		},

		/**
   * Make sure we update the moserJSON when being asked to serialize ourselves
   * @Overrides
   * @return {Object} The information to be saved in the dashboard spec
   */
		toJSON: function toJSON() {
			var shapingId = this.get('shapingId');
			if (!shapingId) {
				this._currentMoserJSONID = null;

				// Clear the moserJSON from the model
				this.updateMoserJSON(undefined);
			} else if (this.shapingHelper && (!this._currentMoserJSONID || this._currentMoserJSONID !== shapingId)) {
				this._currentMoserJSONID = shapingId;
				// If we're back to the initial spec
				if (shapingId === this._initialShapingId) {
					this.setEmbeddedModuleUpToDate(this._initialEmbeddedModuleUpToData);
				} else {
					this.setEmbeddedModuleUpToDate(false);
				}
				this.updateMoserJSON(this.shapingHelper.toJSON());
			}

			return ShapingModel.inherited('toJSON', this, arguments);
		},

		setEmbeddedModuleUpToDate: function setEmbeddedModuleUpToDate(upToDate) {
			this.set({
				embeddedModuleUpToDate: upToDate
			}, {
				silent: true,
				payloadData: {
					skipUndoRedo: true
				}
			});
		},

		updateMoserJSON: function updateMoserJSON(moserJSON) {
			this.set({
				moserJSON: moserJSON
			}, {
				silent: true,
				payloadData: {
					skipUndoRedo: true
				}
			});
		},

		getDeploymentReference: function getDeploymentReference(deploymentReferences, processedAssetIds, isSaveAs, contentLocale) {
			if (this.embeddedModuleId && !processedAssetIds[this.embeddedModuleId] && !isSaveAs) {
				var _name;

				var shapingContentLocale = contentLocale || 'en-us';
				processedAssetIds[this.embeddedModuleId] = true;
				deploymentReferences.push({
					objects: [{
						type: 'module',
						searchPath: 'storeID("' + this.embeddedModuleId + '")'
					}],
					name: (_name = {}, _name[shapingContentLocale] = this.embeddedModuleId, _name)
				});
			}
		}
	});

	return ShapingModel;
});
//# sourceMappingURL=ShapingModel.js.map