'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 Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define([], function () {
	var PxPercentUtil = function () {
		function PxPercentUtil() {
			_classCallCheck(this, PxPercentUtil);
		}

		/**
   *  Convert the model positional values to Percent
   * @param {Object} positionalModel - An object that contains any of following positional properties
   * { top: xxx, left: xxx, bottom: xxx, right: xxx, width: xxx, height: xxx }
   * xxx should be in percent (ex. '25%')
   * @param {Object} pageSize - An object that describes the page size where the positional model resides
   * The structure is { width: xxxx, height: yyyy }.
   * yyy needs to be in pixels (ex. '100px')
   * @param {Number} decimalPlaces - The number of decimal places to truncate the percent float
   * @returns Undefined
   */
		PxPercentUtil.changePixelPropertiesToPercent = function changePixelPropertiesToPercent(positionalModel, pageSize, decimalPlaces) {
			var converter = new PixelToPercentConverter(decimalPlaces);
			this._convertUnits(positionalModel, pageSize, converter);
		};

		/**
   *  Convert the model positional values to Pixel
   * @param {Object} positionalModel - An object that contains any of following positional properties
   * { top: xxx, left: xxx, bottom: xxx, right: xxx, width: xxx, height: xxx }
   * xxx should be in pixels (ex. '100px')
   * @param {Object} pageSize - An object that describes the page size where the positional model resides
   * The structure is { width: xxxx, height: yyyy }.
   * yyy needs to be in pixels (ex. '100px')
   * @returns Undefined
   */


		PxPercentUtil.changePercentPropertiesToPixel = function changePercentPropertiesToPixel(positionalModel, pageSize) {
			var converter = new PercentToPixelConverter();
			this._convertUnits(positionalModel, pageSize, converter);
		};

		PxPercentUtil._convertUnits = function _convertUnits(positionalModel, pageSize, converter) {
			if (!positionalModel || !pageSize) {
				throw new Error('Invalid options specified.');
			}

			// Position values
			this._convertProperty(positionalModel, 'top', pageSize.height, converter);
			this._convertProperty(positionalModel, 'left', pageSize.width, converter);

			// Width/height values
			// Some models have right & bottom and others have width & height
			this._convertProperty(positionalModel, 'height', pageSize.height, converter);
			this._convertProperty(positionalModel, 'width', pageSize.width, converter);

			// Bottom and right values are relative to bottom and right side instead of top/left
			this._convertProperty(positionalModel, 'bottom', pageSize.height, converter, false);
			this._convertProperty(positionalModel, 'right', pageSize.width, converter, false);
		};

		PxPercentUtil._convertProperty = function _convertProperty(positionalModel, prop, maxSize, converter) {
			var relativeToBottomRight = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;

			if (positionalModel && positionalModel[prop]) {
				positionalModel[prop] = converter.convertValue(positionalModel[prop], maxSize, relativeToBottomRight);
			}
		};

		return PxPercentUtil;
	}();

	var PixelToPercentConverter = function () {
		function PixelToPercentConverter(decimalPlaces) {
			_classCallCheck(this, PixelToPercentConverter);

			this.decimalPlaces = decimalPlaces;
		}

		PixelToPercentConverter.prototype.convertValue = function convertValue(value, maxSize, relativeToBottomRight) {
			var result = value;
			if (value && value.indexOf && value.indexOf('px') !== -1) {
				var size = 100 * parseFloat(value) / maxSize;
				if (relativeToBottomRight) {
					size = 100 - size;
				}

				if (this.decimalPlaces) {
					result = String(Number(size.toFixed(this.decimalPlaces))) + '%';
				} else {
					result = size + '%';
				}
			}
			return result;
		};

		return PixelToPercentConverter;
	}();

	var PercentToPixelConverter = function () {
		function PercentToPixelConverter() {
			_classCallCheck(this, PercentToPixelConverter);
		}

		PercentToPixelConverter.prototype.convertValue = function convertValue(value, maxSize, relativeToBottomRight) {
			var result = value;
			if (value && value.indexOf && value.indexOf('%') !== -1) {
				var size = parseFloat(value);
				if (relativeToBottomRight && size === 0) {
					result = maxSize;
				} else {
					result = Math.round(maxSize * size / 100);
				}
				result = result + 'px';
			}
			return result;
		};

		return PercentToPixelConverter;
	}();

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