/**
 * @license
 * Copyright (c) 2018 Fady Heiba. All rights reserved.
 * 
 * Copyrights licensed under the terms of the MIT license.
 * Original source <https://github.com/fadyheiba/ShowHide>
 */
define(["qlik","./properties"],function(qlik,properties){function getActiveVisID(conditionalVisList){var activeChart;return conditionalVisList.forEach(function(condVis){if(!activeChart){var condVal=condVis.condition?+condVis.condition:-1;isNaN(condVal)&&(condVal="true"===condVis.condition.toLowerCase()?-1:0),0!==condVal&&(activeChart=condVis.conditionalMasterObject.split("|")[1])}}),activeChart}return{getActiveVisID:getActiveVisID,initialProperties:{conditionalVis:[]},support:{snapshot:!1,export:!1,exportData:!1},definition:properties,template:'<style>.qv-object-qlik-show-hide-container .qv-inner-object {overflow:visible;}.qv-mode-edit .qv-object-qlik-show-hide-container .qv-inner-object {overflow:hidden;}</style><div ng-style="interactStyle()" style="display:block;  width:100%; height:100%; overflow:visible;"></div>',controller:function($scope,$element){function renderChart(){null==$scope.currentVisualization||($scope.selectionsApi.confirm(),destroyObject()),createObject()}function createObject(){var objectOptions={noInteraction:!$scope.canInteract,noSelections:$scope.noSelections};$scope.app.getObjectProperties($scope.currentChart).then(function(chartModel){var newChartId=$scope.layout.qInfo.qId+"_"+$scope.currentChart;return $scope.app.getObjectProperties(newChartId).then(function(newChartModel){return newChartModel}).catch(function(){return $scope.app.createGenericObject({qInfo:{qId:newChartId}}).then(function(newChartModel){return newChartModel.copyFrom($scope.currentChart).then(function(){return newChartModel.getProperties().then(function(){return newChartModel})})})}).then(function(newChartModel){return chartModel.properties.qStateName||(newChartModel.properties.qStateName=$scope.layout.qStateName||""),newChartModel.setProperties(newChartModel.properties).then(function(){return $scope.app.visualization.get(newChartId).then(function(visualization){return($scope.currentVisualization=visualization).show($element.find("div"),objectOptions).then(function(){$element.find(".qv-object-wrapper").scope().options.zoomEnabled=$scope.canZoom})})})})})}function destroyObject(){$element.find(".qv-object-wrapper").remove(),$scope.currentVisualization&&($scope.currentVisualization.close(),$scope.currentVisualization=null)}$scope.canInteract=$scope.options&&1===$scope.options.interactionState,$scope.noSelections=$scope.options&&!0===$scope.options.noSelections,$scope.canZoom=$scope.options&&!0===$scope.options.zoomEnabled,$scope.interactStyle=function(){return{"pointer-events":$scope.object&&1===$scope.object.getInteractionState()?"auto":"none"}},$scope.$watch("options.interactionState",function(newVal){var canInteract=1===newVal;$scope.canInteract!==canInteract&&($scope.canInteract=canInteract,renderChart())}),$scope.$watch("options.noSelections",function(newVal){var noSelections=!0===newVal;$scope.noSelections!==noSelections&&($scope.noSelections=noSelections,renderChart())}),$scope.app=qlik.currApp(this),$scope.currentChart=getActiveVisID($scope.component.model.layout.conditionalVis),$scope.currentVisualization=null,$scope.currentChart&&renderChart(),$scope.component.model.Validated.bind(function(){$element.find(".qv-object .qv-inner-object").css("overflow","visible");var chart=getActiveVisID($scope.component.model.layout.conditionalVis);chart&&chart!==$scope.currentChart?($scope.currentChart=chart,renderChart()):chart||chart===$scope.currentChart?chart||chart!==$scope.currentChart||($scope.currentVisualization=null):$scope.currentVisualization&&($scope.currentChart=null,destroyObject())})}}});