'use strict';

/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| IBM Cognos Products: Dashboard
 *| (C) Copyright IBM Corp. 2017 - 2019
 *|
 *| 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', './shaping/ShapingModel', 'underscore'], function (Model, ShapingModel, _) {
	'use strict';

	var SourceModel = Model.extend({
		nestedModels: {
			shaping: ShapingModel
		},

		whitelistAttrs: ['id', 'assetId', 'type', 'clientId', 'module', 'name', 'pinSourceName', 'shaping', 'customGroupColumns'],

		init: function init(model) {
			if (model) {
				_.defaults(model, {
					shaping: {},
					assetId: this._generateUniqueId('assetId')
				});
			}
			SourceModel.inherited('init', this, arguments);
		},

		destroy: function destroy() {
			if (this.getTempModuleUrl() && this.ajaxSvc) {
				this.ajaxSvc.ajax({
					contentType: 'application/json; charset=utf-8',
					url: this.getTempModuleUrl(),
					type: 'DELETE'
				});
			}
			this.get('shaping').destroy();
		},

		setAjaxSvc: function setAjaxSvc(ajaxSvc) {
			this.ajaxSvc = ajaxSvc;
		},

		setUserProfileSvc: function setUserProfileSvc(userProfileSvc) {
			this.userProfileSvc = userProfileSvc;
		},

		getDeploymentReference: function getDeploymentReference(deploymentReferences, processedAssetIds, isSaveAs) {
			if (this.state !== 'error' && this.errorCode !== '404' && !processedAssetIds[this.assetId]) {
				var _name;

				var userPreferences = this.userProfileSvc && this.userProfileSvc.preferences;
				var contentLocale = userPreferences && userPreferences.contentLocale || 'en-us';
				processedAssetIds[this.assetId] = true;
				deploymentReferences.push({
					objects: [{
						type: this.type,
						searchPath: 'storeID("' + this.assetId + '")'
					}],
					name: (_name = {}, _name[contentLocale] = this.assetId, _name)
				});

				if (this.shaping) {
					this.shaping.getDeploymentReference(deploymentReferences, processedAssetIds, isSaveAs, contentLocale);
				}
			}
		},

		getIsOlapPackage: function getIsOlapPackage() {
			return this.get('isOlapPackage');
		},

		hasInlineModule: function hasInlineModule() {
			return typeof this.module !== 'undefined';
		},

		setTempModuleUrl: function setTempModuleUrl(url) {
			this._tempModuleUrl = url;
		},

		getTempModuleUrl: function getTempModuleUrl() {
			return this._tempModuleUrl;
		}
	});

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