(function(b){'use strict';var i=window.MinervaKB||{},e=i.settings||{};Array.prototype.includes||Object.defineProperty(Array.prototype,'includes',{value:function(e,f){var d,a,b,c;if(this==null)throw new TypeError('"this" is null or not defined');if(d=Object(this),a=d.length>>>0,a===0)return!1;b=f|0,c=Math.max(b>=0?b:a-Math.abs(b),0);function g(a,b){return a===b||typeof a=='number'&&typeof b=='number'&&isNaN(a)&&isNaN(b)}while(c<a){if(g(d[c],e))return!0;c++}return!1}});function c(a){return a?[].slice.apply(a):[]}function f(a){for(var b=0;a=a.previousElementSibling;)b++;return b}function g(a,d){var b=d?1e3:1024,e,c;if(Math.abs(a)<b)return a+'B';e=d?['kB','MB','GB','TB','PB','EB','ZB','YB']:['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'],c=-1;do a/=b,++c;while(Math.abs(a)>=b&&c<e.length-1)return a.toFixed(1)+''+e[c]}function h(a){var b=a.$form,c=e.tickets_create_use_standalone_upload_button,h=a.toolbarOptions||[[{header:[1,2,3,4,5,6,!1]}],['bold','italic','underline','strike'],[{list:'ordered'},{list:'bullet'}],['link'],['blockquote','code-block'],['clean']],f,g,i,j;return a.fileUpload&&!c&&h.push(['upload']),f=new Quill(a.editorSelector,{modules:{toolbar:h,history:{delay:2e3,maxStack:500,userOnly:!0}},placeholder:a.placeholder||'Write your message here...',theme:a.theme||'snow'}),f.clipboard.addMatcher(Node.ELEMENT_NODE,function(c,a){var b=[];return a.ops.forEach(function(a){var c=/https?:\/\/[^\s]+/g;a.insert&&typeof a.insert=='string'&&(a.attributes&&a.attributes.link?b.push({insert:a.insert.trim(),attributes:{link:a.attributes.link}}):b.push({insert:a.insert}))}),a.ops=b,a}),!a.fileUpload?f:(g=!1,i=c?b.find('.js-mkb-ticket-attach-files')[0]:b.find('.ql-upload')[0],j=b.find('.js-mkb-editor-attachments-section'),i.addEventListener('click',function(a){a.preventDefault(),c||i.classList.toggle('state--on'),j[g?'slideUp':'slideDown'](250),g=!g}),d({$dropArea:b.find('.js-mkb-editor-attachments-drop-area'),$form:b}),f)}function d(s){var d=s.$dropArea,q=s.$form,l,i,y,x,a,u,n,o,z,r,h,e,m;if(!d||!d.length||!q||!q.length){console.log('Could not initialize file uploader');return}l=d.get(0),i=d.find('.js-mkb-file-upload-preview'),y=d.find('.js-mkb-file-upload-clear'),x=d.find(".js-mkb-file-upload-store"),a=x,u=d.find('.js-mkb-file-upload-drop-errors'),n=a.attr('accept')||'',o=a.data('maxFiles'),z=a.data('maxFileSize'),q.attr('enctype','multipart/form-data'),n=n.split(',').map(function(a){return a.trim().replace('.','')}),['dragenter','dragover','dragleave','drop'].forEach(function(a){document.body.addEventListener(a,function(a){a.preventDefault(),a.stopPropagation()},!1)}),['dragenter','dragover'].forEach(function(a){l.addEventListener(a,function(){d.addClass('mkb-drop-highlight')},!1)}),['dragleave','drop'].forEach(function(a){l.addEventListener(a,function(){d.removeClass('mkb-drop-highlight')},!1)}),l.addEventListener('drop',function(a){v(a.dataTransfer.files,!0)},!1),x.get(0).addEventListener('change',function(){v(this.files)},!1);function t(){return new ClipboardEvent('').clipboardData||new DataTransfer}r=!1,h=!1,e=!1,m=!1;function v(l,i){var g,d,k,f,b;if(i=i||!1,g=i?c(a.prop('files')):[],d=c(l),k=g.map(function(a){return a.name}),r=!1,h=!1,e=!1,m=!1,g.length>=o){e=!0,w();return}console.log('adding files',d),d=d.filter(function(a){var e=a.name.split('.').pop().toLowerCase(),b=n.includes(e),c=a.size/(1024*1024)<=z,d=k.includes(a.name);return b||(r=!0),c||(h=!0),d&&(m=!0),b&&c&&!d}),f=g.concat(d),f.length>o&&(e=!0,f=f.slice(0,o)),b=t(),f.forEach(function(a){b.items.add(a)}),b.files.length?a.prop('files',b.files):p(),w(),j(b.files),console.log('[DEBUG]: Files in storage',a.prop('files'))}function w(){k();var a=[];r&&a.push('Some of the added files are not allowed'),h&&a.push('Some of the added files are too large'),e&&a.push('Maximum file limit is reached, some files were not added'),m&&a.push('Some of the files are already added'),a.length&&u.html(a.reduce(function(a,b){return a+'<div>'+b+'</div>'},''))}function k(){u.html('')}function j(a){i.html(''),a=c(a),a.forEach(function(c){var e=/^image\//.test(c.type),a=b('<div class="js-mkb-attachment-upload-preview-item mkb-attachment-upload-preview-item"><a href="#" class="js-mkb-attachment-preview-remove mkb-attachment-preview-remove fa fa-times-circle"></a></div>'),d;e?(d=new FileReader,d.readAsDataURL(c),d.onloadend=function(){var b=document.createElement('img');b.src=d.result,a.append(b)},a.addClass('type--image')):(a.append('<span>'+c.name+' ('+g(c.size,!0)+')'+'</span>'),a.addClass('type--file')),i.append(a)})}i.on('click','.js-mkb-attachment-preview-remove',function(d){var e,g,b;d.preventDefault(),e=f(d.currentTarget.parentNode),g=c(a.prop('files')),b=t(),g.filter(function(b,a){return a!==e}).forEach(function(a,c){b.items.add(a)}),b.files.length?(a.prop('files',b.files),j(b.files),console.log('[DEBUG]: Files in storage',a.prop('files'))):p(),k()});function p(){a.val(null),j([]),console.log('[DEBUG]: Files in storage',a.prop('files'))}y.on('click',function(a){a.preventDefault(),a.stopImmediatePropagation(),p(),k()})}function a(c,a){a=a||{},this._isLoading=!1,this.options=a,this.controls={},this.quillEditors=this.options.quillEditors||[],this.el=c,this.$el=b(c),this.$messagesEl=this.$el.find('.js-mkb-form-messages'),this.$submit=this.$el.find('.js-mkb-form-submit'),this.submitLabel=this.$submit.val(),this.submitLoadingLabel=this.$submit.data('progress-label'),this.findControls()}a.prototype.findControls=function(){var a=this.$el.find('input:not(.js-mkb-form-submit), select, textarea');this.controls=c(a).map(function(a){return{id:a.getAttribute('name'),el:a,$el:b(a)}})},a.prototype.isLoading=function(){return this._isLoading},a.prototype.startLoading=function(){this._isLoading=!0,this.disableControls(),this.disableControl(this.$submit),this.$submit.val(this.submitLoadingLabel),this.$el.addClass('state--loading'),this.$el.append('<div class="js-mkb-form-overlay mkb-form-overlay"></div>')},a.prototype.endLoading=function(){this._isLoading=!1,this.enableControls(),this.enableControl(this.$submit),this.$submit.val(this.submitLabel),this.$el.find('.js-mkb-form-overlay').remove(),this.$el.removeClass('state--loading')},a.prototype.disableControls=function(){this.controls.forEach(function(a){this.disableControl(a.$el)}.bind(this)),this.quillEditors.forEach(function(a){a.enable(!1)})},a.prototype.lock=function(){this._isLoading=!0,this.disableControls(),this.disableControl(this.$submit)},a.prototype.enableControls=function(){this.controls.forEach(function(a){this.enableControl(a.$el)}.bind(this)),this.quillEditors.forEach(function(a){a.enable(!0)})},a.prototype.disableControl=function(a){a.attr('disabled','disabled')},a.prototype.enableControl=function(a){a.attr('disabled',!1)},a.prototype.showMessages=function(c,a){a=a||'error',this.clearMessages(),this.$messagesEl.addClass('mkb-form-messages--'+a).html(c),b([document.documentElement,document.body]).animate({scrollTop:this.$messagesEl.offset().top-parseInt(e.global_scroll_offset.size,10)},100)},a.prototype.clearMessages=function(){this.$messagesEl.removeClass('mkb-form-messages--error mkb-form-messages--success').html('')},a.prototype.serialize=function(){return b(this.el).serializeArray().reduce(function(a,b){return a[b.name]=b.value,a},{})},window.MinervaCommonUI={Form:a,setupQuillEditor:h,setupFileUpload:d}})(window.jQuery)