'use strict';

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

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

define(['../../../lib/@waca/upgrades/UpgradeBase'], function (UpgradeBase) {

	var ABSOLUTE_TYPE = 'absolute';
	var SCALINGABSOLUTE_TYPE = 'scalingAbsolute';
	var GENERICPAGE_TYPE = 'genericPage';
	var ABSOLUTE_LAYOUTPOSITIONING = 'absolute';
	var RELATIVE_LAYOUTPOSITIONING = 'relative';

	/**
 * Upgrades absolute and scalingAbsolute layouts to new genericPage type.
 * The new genericPage layout has a layoutPositioning property (absolute or relative)
 * Freeform templates are converted to Template1.
 **/

	var LayoutPositioningUpgrade = function (_UpgradeBase) {
		_inherits(LayoutPositioningUpgrade, _UpgradeBase);

		function LayoutPositioningUpgrade() {
			_classCallCheck(this, LayoutPositioningUpgrade);

			var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));

			_this.VERSION = 1012;
			return _this;
		}

		/**
   * Perform upgrade
   *
   * @param {object} spec - spec to perform upgrade on
   *
   * @return {Promise} Promise to be resolved when upgrade performed
   */


		LayoutPositioningUpgrade.prototype.up = function up(spec) {
			var _this2 = this;

			return Promise.resolve().then(function () {
				_this2._init();

				if (spec && spec.layout) {
					_this2._upgradeLayout(spec.layout);
					_this2._setTopLayoutPositioning(spec.layout);
				}
				return spec;
			});
		};

		// Downgrades are not available in CA


		LayoutPositioningUpgrade.prototype.down = function down(spec) {
			return Promise.resolve(spec);
		};

		LayoutPositioningUpgrade.prototype._init = function _init() {
			this._hasRelativeLayouts = false;
			this._hasAbsoluteLayouts = false;
		};

		LayoutPositioningUpgrade.prototype._upgradeLayout = function _upgradeLayout(layout, parent) {
			if (layout.items) {
				// Recursively upgrade child layouts
				for (var _iterator = layout.items, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
					var _ref;

					if (_isArray) {
						if (_i >= _iterator.length) break;
						_ref = _iterator[_i++];
					} else {
						_i = _iterator.next();
						if (_i.done) break;
						_ref = _i.value;
					}

					var item = _ref;

					this._upgradeLayout(item, layout);
				}
			}
			this._upgradeLayoutPositioning(layout, parent);
		};

		LayoutPositioningUpgrade.prototype._upgradeLayoutPositioning = function _upgradeLayoutPositioning(layout, parent) {
			if (layout.type === ABSOLUTE_TYPE) {
				layout.layoutPositioning = ABSOLUTE_LAYOUTPOSITIONING;
				layout.type = GENERICPAGE_TYPE;
				this._hasAbsoluteLayouts = true;
				this._upgradeAbsoluteTemplate(layout, parent);
			} else if (layout.type === SCALINGABSOLUTE_TYPE) {
				layout.layoutPositioning = RELATIVE_LAYOUTPOSITIONING;
				layout.type = GENERICPAGE_TYPE;
				this._hasRelativeLayouts = true;
			}
		};

		// Convert the freeform template (NoTemplate) to one of the non-freeform templates - Template1


		LayoutPositioningUpgrade.prototype._upgradeAbsoluteTemplate = function _upgradeAbsoluteTemplate(layout, parent) {
			parent.templateName = 'Template1';
			layout.css = 'templateBox aspectRatio_default';
			layout.items.unshift({
				'type': 'templateIndicator',
				'style': {
					'top': '0%',
					'left': '0%',
					'right': '0%',
					'bottom': '0%'
				}
			});
		};

		LayoutPositioningUpgrade.prototype._setTopLayoutPositioning = function _setTopLayoutPositioning(layout) {
			if (this._hasAbsoluteLayouts && !this._hasRelativeLayouts) {
				layout.layoutPositioning = ABSOLUTE_LAYOUTPOSITIONING;
			} else if (this._hasRelativeLayouts && !this._hasAbsoluteLayouts) {
				layout.layoutPositioning = RELATIVE_LAYOUTPOSITIONING;
			}
			// Undefined otherwise
		};

		return LayoutPositioningUpgrade;
	}(UpgradeBase);

	return new LayoutPositioningUpgrade();
});
//# sourceMappingURL=LayoutPositioningUpgrade.js.map