/*!
 * OpenUI5
 * (c) Copyright 2009-2019 SAP SE or an SAP affiliate company.
 * Licensed under the Apache License, Version 2.0 - see LICENSE.txt.
 */
sap.ui.define(["jquery.sap.global","sap/ui/core/support/Plugin","sap/ui/core/support/controls/InteractionSlider","sap/ui/core/support/controls/InteractionTree","sap/ui/core/support/controls/TimelineOverview","sap/m/MessageToast","sap/ui/thirdparty/jszip","sap/ui/core/util/File","sap/ui/performance/trace/Interaction","sap/ui/performance/Measurement"],function(t,e,r,i,s,n,a,o,p,c){"use strict";var u=e.extend("sap.ui.core.support.plugins.Interaction",{constructor:function(t){e.apply(this,["sapUiSupportInteraction","Interaction",t]);this._oStub=t;if(this.runsAsToolPlugin()){this._aEventIds=[this.getId()+"SetMeasurements",this.getId()+"SetActive",this.getId()+"Export",this.getId()+"Import",this.getId()+"SetQueryString"];var n=function(t,e){return("000"+String(t)).slice(-e)};this._fnFormatTime=function(t){var e=new Date(t),r=Math.floor((t-Math.floor(t))*1e3);return n(e.getHours(),2)+":"+n(e.getMinutes(),2)+":"+n(e.getSeconds(),2)+"."+n(e.getMilliseconds(),3)+n(r,3)};this._oInteractionSlider=new r;this._oInteractionTree=new i({});this._oTimelineOverview=new s}else{this._aEventIds=[this.getId()+"Refresh",this.getId()+"Clear",this.getId()+"Start",this.getId()+"Stop",this.getId()+"Activate",this.getId()+"Export",this.getId()+"Import",this.getId()+"SetQueryString"]}}});u.prototype.init=function(t){e.prototype.init.apply(this,arguments);if(this.runsAsToolPlugin()){d.call(this,t)}else{h.call(this,t)}};u.prototype.exit=function(t){e.prototype.exit.apply(this,arguments)};function d(e){var r=sap.ui.getCore().createRenderManager();r.write('<div class="sapUiSupportToolbar">');r.write('<button id="'+this.getId()+'-record" class="sapUiSupportIntToggleRecordingBtn"></button>');r.write("<label class='sapUiSupportIntODataLbl'><input type='checkbox' id=\""+this.getId()+'-odata" > Enable OData Statistics</label>');r.write("<div class='sapUiSupportIntFupInputMask'>");r.write('<input id="'+this.getId()+"-fileImport\" tabindex='-1' size='1' accept='application/zip' type='file'/>");r.write("</div>");r.write('<button id="'+this.getId()+'-import" class="sapUiSupportIntImportExportBtn sapUiSupportIntImportBtn sapUiSupportRoundedButton ">Import</button>');r.write('<button id="'+this.getId()+'-export" class="sapUiSupportIntImportExportBtn sapUiSupportIntExportBtn sapUiSupportRoundedButton sapUiSupportIntHidden ">Export</button>');r.write('<span id="'+this.getId()+'-info" class="sapUiSupportIntRecordingInfo"></span>');r.write('</div><div class="sapUiSupportInteractionCntnt">');r.write("</div>");r.write('<div class="sapUiPerformanceStatsDiv sapUiSupportIntHidden">');r.write('<div class="sapUiPerformanceTimeline" style="height: 50px;"></div>');r.write('<div class="sapUiPerformanceTop">');r.write("</div>");r.write('<div class="sapUiPerformanceBottom">');r.write("</div>");r.write("</div>");r.flush(this.$().get(0));r.destroy();r=sap.ui.getCore().createRenderManager();this._oTimelineOverview.render(r);r.flush(this.$().find(".sapUiPerformanceStatsDiv .sapUiPerformanceTimeline").get(0));r.destroy();r=sap.ui.getCore().createRenderManager();this._oInteractionSlider.render(r);r.flush(this.$().find(".sapUiPerformanceStatsDiv .sapUiPerformanceTop").get(0));r.destroy();this._oInteractionSlider._registerEventListeners();var i=this;t(".sapUiPerformanceTop").on("InteractionSliderChange",{},function(t,e,r){i._oInteractionTree.setRange(e,r)});this.$("refresh").click(t.proxy(function(t){this._oStub.sendEvent(this.getId()+"Refresh")},this));this.$("clear").click(t.proxy(function(t){this._oStub.sendEvent(this.getId()+"Clear")},this));this.$("export").click(t.proxy(function(t){this.onsapUiSupportInteractionExport()},this));this.$("fileImport").change(t.proxy(function(t){this.onsapUiSupportInteractionImport()},this));this.$("active").click(t.proxy(function(t){var e=false;if(this.$("active").prop("checked")){e=true}this._oStub.sendEvent(this.getId()+"Activate",{active:e})},this));this.$("odata").attr("checked",this._bODATA_Stats_On).click(t.proxy(function(e){t.sap.statistics(!t.sap.statistics())},this));this.$("record").attr("data-state",!this._bFesrActive?"Start recording":"Stop recording");this.$("record").click(t.proxy(function(e){if(this.$("record").attr("data-state")==="Stop recording"){this._oStub.sendEvent(this.getId()+"Refresh");this._oStub.sendEvent(this.getId()+"Activate",{active:false});this.$("record").attr("data-state","Start recording");t(".sapUiPerformanceStatsDiv.sapUiSupportIntHidden").removeClass("sapUiSupportIntHidden");t(".sapUiSupportIntExportBtn.sapUiSupportIntHidden").removeClass("sapUiSupportIntHidden")}else if(this.$("record").attr("data-state")==="Start recording"){t(".sapUiPerformanceStatsDiv").addClass("sapUiSupportIntHidden");t(".sapUiSupportIntExportBtn").addClass("sapUiSupportIntHidden");this._oStub.sendEvent(this.getId()+"Clear");this._oStub.sendEvent(this.getId()+"Activate",{active:true});this.$("record").attr("data-state","Stop recording")}},this))}function h(e){var r=/sap-ui-xx-fesr=(true|x|X)/.test(window.location.search);var i=t.sap.statistics()||/sap-statistics=(true|x|X)/.test(window.location.search);this._oStub.sendEvent(this.getId()+"SetQueryString",{queryString:{bFesrActive:r,bODATA_Stats_On:i}});l.call(this)}function l(t,e){var r=p.getActive()||this._bFesrActive;var i=[];if(r||e){i=e||p.getAll(true);var s=window.performance.timing.fetchStart;for(var n=0;n<i.length;n++){var a=i[n];for(var o=0;o<a.requests.length;o++){var c=a.requests[o];a.requests[o]={connectEnd:c.connectEnd,connectStart:c.connectStart,domainLookupEnd:c.domainLookupEnd,domainLookupStart:c.domainLookupStart,duration:c.duration,entryType:c.entryType,fetchStart:c.fetchStart,initiatorType:c.initiatorType,name:c.name,redirectEnd:c.redirectEnd,redirectStart:c.redirectStart,requestStart:c.requestStart,responseEnd:c.responseEnd,responseStart:c.responseStart,secureConnectionStart:c.secureConnectionStart,startTime:c.startTime,workerStart:c.workerStart,fetchStartOffset:s}}}}this._oStub.sendEvent(this.getId()+"SetMeasurements",{measurements:i});this._oStub.sendEvent(this.getId()+"SetActive",{active:r})}u.prototype.onsapUiSupportInteractionSetQueryString=function(t){var e=t.getParameter("queryString");this._bFesrActive=e.bFesrActive;this._bODATA_Stats_On=e.bODATA_Stats_On;this.$("odata").attr("checked",this._bODATA_Stats_On);this.$("record").attr("data-state",!this._bFesrActive?"Start recording":"Stop recording")};u.prototype.onsapUiSupportInteractionSetMeasurements=function(t){this._setMeasurementsData(t.getParameter("measurements"))};u.prototype.onsapUiSupportInteractionSetActive=function(t){var e=t.getParameter("active");var r=this.$("active");if(e){r.attr("checked","checked")}else{r.removeAttr("checked")}};u.prototype.onsapUiSupportInteractionRefresh=function(t){l.call(this)};u.prototype.onsapUiSupportInteractionClear=function(t){p.clear();this._oStub.sendEvent(this.getId()+"SetMeasurements",{measurements:[]})};u.prototype.onsapUiSupportInteractionStart=function(t){c.start(this.getId()+"-perf","Measurement by support tool")};u.prototype.onsapUiSupportInteractionEnd=function(t){u.end(true)};u.prototype.onsapUiSupportInteractionActivate=function(t){var e=t.getParameter("active");if(p.getActive()!=e){p.setActive(e)}};u.prototype.onsapUiSupportInteractionExport=function(t){var e=this.measurements||[];if(e.length>0){var r=new a;r.file("InteractionsSteps.json",JSON.stringify(e).replace(/,"isExpanded":true/g,""));var i=r.generate({type:"blob"});this._openGeneratedFile(i)}};u.prototype.onsapUiSupportInteractionImport=function(t){var e=this.$().find("#"+this.getId()+"-fileImport").get(0).files;if(e.length===0){n.show("Select a file for import first!",{autoClose:true,duration:3e3});return}if(!window.FileReader){n.show("Use a modern browser which supports FileReader!",{autoClose:true,duration:3e3});return}var r=new window.FileReader,i=e[0],s=this;r.onload=function(t){return function(t){var e=new a(t.target.result);var r=e.files["InteractionsSteps.json"]&&e.files["InteractionsSteps.json"].asText();if(r){s._setMeasurementsData(JSON.parse(r.replace(/,"isExpanded":true/g,"")))}else{n.show("Imported data does not contain interaction measures",{autoClose:true,duration:3e3})}}}(i);r.readAsArrayBuffer(i)};u.prototype._openGeneratedFile=function(t){o.save(t,"InteractionSteps","zip","application/zip")};u.prototype._setMeasurementsData=function(e){var r=0,i=100,s=function(t){var e=function(t,e){var r=0;if(t.length===0){return r}for(var i=t.length-1;i>=0;i--){if(t[i].startTime<e.startTime){r=i+1;break}}return r},r=function(t,e){return t.filter(function(t){return t.timing.startTime===e})},s=function(t,e){var r=0;if(t.length===0){return r}for(var i=t.length-1;i>=0;i--){if(t[i].start<e.fetchStartOffset+e.startTime){r=i;break}}return r},n=0;t.forEach(function(t,a,o){var p=t.requests;for(var c=p.length-1;c>=0;c--){var u=p[c];if(a>0&&t.start-i>u.fetchStartOffset+u.startTime){var d=s(o,u);var h=o[d].requests;n=e(h,u);h.splice(n,0,u);p.splice(c,1);var l=r(t.sapStatistics,u.startTime);if(l.length>0){o[d].sapStatistics=o[d].sapStatistics.concat(l)}}}})};s(e);this.measurements=e;for(var n=0;n<e.length;n++){r+=e[n].requests.length}if(e.length>0){t(".sapUiPerformanceStatsDiv.sapUiSupportIntHidden").removeClass("sapUiSupportIntHidden");t(".sapUiSupportIntExportBtn.sapUiSupportIntHidden").removeClass("sapUiSupportIntHidden");this.$("info").text("Total "+r+" Requests in "+e.length+" Interactions")}else{t(".sapUiPerformanceStatsDiv").addClass("sapUiSupportIntHidden");t(".sapUiSupportIntExportBtn").addClass("sapUiSupportIntHidden");this.$("info").text("")}var a=this.$().find(".sapUiPerformanceStatsDiv .sapUiPerformanceTimeline").get(0);var o=sap.ui.getCore().createRenderManager();this._oTimelineOverview.setInteractions(e);this._oTimelineOverview.render(o);o.flush(a);o.destroy();this._oInteractionSlider._initSlider();this._oInteractionSlider.setDuration(e);var p=this.$().find(".sapUiPerformanceStatsDiv .sapUiPerformanceBottom").get(0);this._oInteractionTree.setInteractions(e);this._oInteractionTree.renderAt(p)};return u});