/**
 * @license
 * Copyright (c) 2015 Erik Wetterberg. All rights reserved.
 * 
 * Copyrights licensed under the terms of the MIT license.
 * Original source <https://github.com/erikwett/qsVariable>
 */
define(["qlik","./util","./properties","text!./style.css"],function(f,h,y,e){"use strict";$("<style>").html(e).appendTo("head");var l,n,i=!0,r=100;function E(e,t,a){i?(i=!1,l=a,n=null,f.currApp(e).variable.setStringValue(t,a),setTimeout(function(){i=!0,n&&n!=l&&E(e,t,n)},r)):n=a}function w(e){var t=typeof e.variableValue;return"object"==t||"undefined"==t?"":e.variableValue}function g(e,t){var a=parseFloat(e);return isNaN(a)?t:a}function N(e,t,a){switch(e){case"material":case"bootstrap":if(a)return"selected";break;default:switch(t){case"button":return"lui-button "+(a?" lui-active":"");case"select":return"qui-select lui-select";case"input":return"qui-input lui-input";case"button_text":return"lui-button__text item-title"}}}function C(e){return"l"===e.render?"98%":"b"!==e.render?"100%":"colfill"===e.buttonMode?"100%":"calc( "+100/Math.max(1,("d"===(t=e).valueType?V(t.dynamicvalues):t.alternatives).length)+"% - 3px)";var t}function x(e){if(e.label){var t=$(e.label).outerWidth()/$(e.label.parentElement).width()*100,a=100*((l=e).value-l.min)/(l.max-l.min);104<a+t&&(a=104-t),e.label.style.left=a+"%",e.label.textContent=e.value}else e.title=e.value;var l}function V(e){return e.split("|").map(function(e){var t=e.split("~");return{value:t[0],label:1<t.length?t[1]:t[0]}})}return{initialProperties:y.initialProperties,definition:y.definition,support:y.support,paint:function(e,l){var t=1===this._interactionState;if(h.setPointerEvents(e[0],t),l.thinHeader?e.closest(".qv-object-variable").addClass("thin-header"):e.closest(".qv-object-variable").removeClass("thin-header"),this.oldSetup&&!this.oldSetup.changed(l))return function(e,t){for(var a=e.querySelectorAll("input, button, option"),l=0;l<a.length;l++){var n=a[l];switch(n.tagName){case"INPUT":n.value=w(t),x(n);break;case"OPTION":n.selected=n.value===t.variableValue;break;case"BUTTON":n.className=N(t.style,"button",n.dataset.value===t.variableValue);break;default:console.log("showValue",n)}}}(e[0],l),f.Promise.resolve();this.oldSetup=y.cloneSetup(l);var n=h.createElement("div",l.style||"qlik"),i=C(l),a="d"===l.valueType?V(l.dynamicvalues):l.alternatives,r=this;if("b"===l.render)a.forEach(function(e){var t=h.createElement("button",N(l.style,"button",e.value===l.variableValue),""),a=h.createElement("span",N(l.style,"button_text",!1),e.label);t.appendChild(a),t.onclick=function(){E(r,l.variableName,e.value)},t.dataset.value=e.value,t.style.width=i,n.appendChild(t)});else if("s"===l.render){var u=h.createElement("select",N(l.style,"select"));u.style.width=i,a.forEach(function(e){var t=h.createElement("option",void 0,e.label);t.value=e.value,t.selected=e.value===l.variableValue,u.appendChild(t)}),u.onchange=function(){E(r,l.variableName,this.value)},n.appendChild(u)}else if("l"===l.render){var s=h.createElement("input");s.style.width=i,s.type="range";var o=g(l.min,0),v=g(l.max,100),c=Math.abs(g(l.step,1));if(v<o){var d=v;v=o,o=d}s.min=o,s.max=v,s.step=c,s.value=l.variableValue;s.addEventListener("keydown",function(){window.requestAnimationFrame(function(){x(s),E(r,l.variableName,s.value)})});var p=function(){window.requestAnimationFrame(function(){x(s),l.updateondrag&&E(r,l.variableName,s.value)})};if(s.addEventListener("mousedown",function(){x(s),p(),s.addEventListener("mousemove",p)}),s.addEventListener("mouseup",function(){x(s),E(r,l.variableName,s.value),s.removeEventListener("mousemove",p)}),n.appendChild(s),l.rangelabel){var b=h.createElement("div","labelwrap");s.label=h.createElement("div","rangelabel",l.variableValue),b.appendChild(s.label),n.appendChild(b)}x(s)}else{var m=h.createElement("input",N(l.style,"input"));m.style.width=i,m.type="text",m.value=w(l),m.onchange=function(){E(r,l.variableName,this.value)},n.appendChild(m)}return h.setChild(e[0],n),f.Promise.resolve()}}});