'use strict';

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Licensed Materials - Property of IBM
 * IBM Business Analytics (C) Copyright IBM Corp. 2019
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

/**
 * @class DashboardState
 * @hideconstructor
 * @classdesc This class provides APIs for dashboard avtivity state
 */
define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../app/nls/StringResources', './DomainValidator', '../../../dashboard/widgets/staticwidget/media/MediaResolver'], function (APIFactory, resources, DomainValidator, MediaResolver) {
	/**
  * this class is used in VisPropertyProvider.
  */
	var PropertyUtils = function () {
		function PropertyUtils(options) {
			_classCallCheck(this, PropertyUtils);

			void APIFactory;
			this.transaction = options.features['Transaction'];
			this.dashboardApi = options.features.API;
		}

		PropertyUtils.prototype.getAPI = function getAPI() {
			// we have to return implementation here. 
			return this;
		};

		PropertyUtils.prototype.initialize = function initialize() {
			var _this = this;

			var VALID_DOMAIN_LIST_CONFIGURATION_PARAMETER = 'ClientValidDomainList';
			return this.dashboardApi.getGlassCoreSvc('.Config').getConfigValue(VALID_DOMAIN_LIST_CONFIGURATION_PARAMETER).then(function (configValueStr) {
				_this.domainValidator = new DomainValidator(configValueStr);
			});
		};

		/**
   * Restore the crosstab format to default
   */


		PropertyUtils.prototype.setProperties = function setProperties(_content, props) {
			var transactionToken = this.transaction.startTransaction();
			var propNames = Object.keys(props);
			propNames.forEach(function (propName) {
				var propVal = props[propName];
				_content.setPropertyValue(propName, propVal, transactionToken);
			});
			this.transaction.endTransaction(transactionToken);
		};

		/** Validates a URL.
   * @param sUrl - the url to validate.
   * @param errorData - if the URL is invalid, it will contain a message describing that fact.
   * @returns - returns true if the URL is valid, false otherwise.
   */


		PropertyUtils.prototype.validateImageInput = function validateImageInput(sUrl) {
			var imagePattern = /(^https?:\/\/.+\.(jpeg|jpg|gif|png)$)|(^data:image\/(jpeg|jpg|gif|png);)|(^v[1-9]\/ext\/.+\.(jpeg|jpg|gif|png))/i;
			var localImageRegex = /(^data:image\/(jpeg|jpg|gif|png);)|(^v[1-9]\/ext\/.+\.(jpeg|jpg|gif|png))/i;
			var ret = {
				valid: true
			};
			if (!imagePattern.test(sUrl)) {
				ret.message = resources.get('imgUrlValidationError');
				ret.valid = false;
			} else if (!this.domainValidator.isAllowedDomain(sUrl) && !localImageRegex.test(sUrl)) {
				ret.message = resources.get('imgUrlInvalidDomainError', {
					domains: this.domainValidator.getAllowedDomains()
				});
				ret.valid = false;
			}
			return ret;
		};

		/** Callback provided to WidgetPropertiesManager for URL validation.
   * @param sUrl - the URI to test.
   * @param errorData - Will be filled with a message saying the URI is invalid, if it is invalid.
   * @returns - true if it is valid, false otherwise.
   */


		PropertyUtils.prototype.validateWebpageLink = function validateWebpageLink(sUrl) {
			var ret = {
				valid: true
			};
			if (!sUrl) {
				ret.message = resources.get('webpageMissingUrl');
				ret.valid = false;
			} else if (!this.domainValidator.isAllowedProtocol(sUrl)) {
				ret.message = resources.get('webpageHttpValidationError');
				ret.valid = false;
			} else if (!this.domainValidator.isAllowedDomain(sUrl)) {
				ret.message = resources.get('webpageInvalidDomainError', {
					domains: this.domainValidator.getAllowedDomains()
				});
				ret.valid = false;
			}
			return ret;
		};

		/** Validates the specified URL.
   * @param url - the URL to validate.
   * @param errorData - if the URL is invalid, this will contain an error message indicating the reason.
   * @returns true if the URL is valid, false otherwise.
   */


		PropertyUtils.prototype.validateMediaInput = function validateMediaInput(url) {
			var ret = {
				valid: true
			};
			if (!url) {
				ret.message = resources.get('mediaUrlValidationError');
				ret.valid = false;
			} else if (!this.domainValidator.isAllowedProtocol(url)) {
				ret.message = resources.get('mediaHttpValidationError');
				ret.valid = false;
			} else if (!this.domainValidator.isAllowedDomain(url)) {
				ret.message = resources.get('mediaInvalidDomainError', {
					domains: this.domainValidator.getAllowedDomains()
				});
				ret.valid = false;
			} else {
				var media = MediaResolver.loadFromUrl(url);
				if (!media) {
					ret.message = resources.get('mediaUnknownValidationError');
					ret.valid = false;
				} else {
					ret.media = media;
				}
			}

			return ret;
		};

		return PropertyUtils;
	}();

	return PropertyUtils;
});
//# sourceMappingURL=PropertiesUtils.js.map