// COPYRIGHT © 2021 Esri
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// This material is licensed for use under the Esri Master License
// Agreement (MLA), and is bound by the terms of that agreement.
// You may redistribute and use this code without modification,
// provided you adhere to the terms of the MLA and include this
// copyright notice.
//
// See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
//
// For additional information, contact:
// Environmental Systems Research Institute, Inc.
// Attn: Contracts and Legal Services Department
// 380 New York Street
// Redlands, California, USA 92373
// USA
//
// email: contracts@esri.com
//
// See http://js.arcgis.com/3.37/esri/copyright.txt for details.

define(["dojo/_base/declare","dojo/_base/lang","dojo/_base/connect","dojo/_base/array","dojo/_base/kernel","dojo/has","dojo/query","dojo/io-query","dojo/dom-attr","dijit/_Widget","dijit/_Templated","dijit/ProgressBar","../../kernel","../../lang","../../domUtils","dojo/text!./templates/AttachmentEditor.html","dojo/i18n!../../nls/jsapi","dojo/NodeList-dom"],(function(t,e,i,a,n,s,h,r,d,o,c,_,l,u,m,p,f){var y=t([o,c],{declaredClass:"esri.dijit.editing.AttachmentEditor",widgetsInTemplate:!0,templateString:p,_listHtml:"<span id='node_${oid}_${attid}' style='display: flex;'><a href='${href}' target='_blank'>${name}</a>",_deleteBtnHtml:"<span style='cursor:pointer;color:red;font-weight:bold;padding:0 2px;' class='deleteAttachment' id='${attid}');'>X</span>",_endHtml:"<br/></span>",_aeConnects:[],_layerEditingCapChecked:{},_layerEditingCap:{},constructor:function(t,i){e.mixin(this,f.widgets.attachmentEditor)},startup:function(){this.inherited(arguments),this._uploadField_connect=i.connect(this._uploadField,"onchange",this,(function(){this._uploadField.value.length>0&&this._addAttachment()})),this._uploadFieldFocus_connect=i.connect(this._uploadField,"onfocus",e.hitch(this,(function(t){m.hide(this._attachmentError)})))},destroy:function(){a.forEach(this._aeConnects,i.disconnect),i.disconnect(this._uploadField_connect),i.disconnect(this._uploadFieldFocus_connect),this.inherited(arguments)},showAttachments:function(t,e){if(this._attachmentList.innerHTML=this.NLS_none,this._uploadField.value="",a.forEach(this.domNode.children,(function(t,e){m.show(t)})),m.hide(this._attachmentError),t&&(this._featureLayer=t.getLayer()||e,this._featureLayer)){if("esri.layers.FeatureLayer"!==this._featureLayer.declaredClass||!this._featureLayer.getEditCapabilities)return e&&e.getEditCapabilities()?void 0:(m.hide(this._uploadForm),void a.forEach(this.domNode.children,(function(t,e){m.hide(t)})));this._currentLayerId=this._featureLayer.id,this._layerEditingCapChecked[this._currentLayerId]||(this._layerEditingCap[this._currentLayerId]=this._featureLayer.getEditCapabilities(),this._layerEditingCapChecked[this._currentLayerId]=!0),this._featureCanUpdate=this._featureLayer.getEditCapabilities({feature:t}).canUpdate,this._oid=t.attributes[this._featureLayer.objectIdField],this._getAttachments(t)}},_getAttachments:function(t){this._featureLayer&&this._featureLayer.queryAttachmentInfos&&this._featureLayer.queryAttachmentInfos(this._oid,e.hitch(this,"_onQueryAttachmentInfosComplete"))},_addAttachment:function(){m.hide(this._attachmentError),this._featureLayer&&this._featureLayer.addAttachment?(m.show(this._attachmentProgress),this._featureLayer.addAttachment(this._oid,this._uploadForm,e.hitch(this,"_onAddAttachmentComplete"),e.hitch(this,"_onAddAttachmentError"))):this._tempUpload=this._uploadForm},_chainAttachment:function(t,i){this._tempUpload&&(m.show(this._attachmentProgress),i.addAttachment(t,this._tempUpload,e.hitch(this,"_onAddAttachmentComplete"),e.hitch(this,"_onAddAttachmentError"))),this._tempUpload=null},_deleteAttachment:function(t,i){m.show(this._attachmentProgress),this._featureLayer.deleteAttachments(t,[i],e.hitch(this,"_onDeleteAttachmentComplete"))},_onQueryAttachmentInfosComplete:function(t){var i=this._listHtml+this._deleteBtnHtml+this._endHtml;this._uploadForm.style.display="block",!this._featureCanUpdate&&this._layerEditingCap[this._currentLayerId].canUpdate||!this._layerEditingCap[this._currentLayerId].canCreate&&!this._layerEditingCap[this._currentLayerId].canUpdate?(i=this._listHtml+this._endHtml,this._uploadForm.style.display="none"):this._layerEditingCap[this._currentLayerId].canCreate&&!this._layerEditingCap[this._currentLayerId].canUpdate&&(i=this._listHtml+this._endHtml);var n=this._attachmentList,s=a.map(t,e.hitch(this,(function(t){return u.substitute({href:t.url,name:t.name,oid:t.objectId,attid:t.id},i)})));n.innerHTML=s.join("")||this.NLS_none,this._updateConnects()},_onAddAttachmentComplete:function(t){m.hide(this._attachmentProgress.domNode);var e=this._attachmentList,i=this._uploadField,a=i.value,n=a.lastIndexOf("\\");n>-1&&(a=a.substring(n+1,a.length)),a=a.replace(/\ /g,"_");var s=r.objectToQuery({gdbVersion:this._featureLayer.gdbVersion,token:this._featureLayer._getToken()}),h=this._listHtml+this._deleteBtnHtml+this._endHtml;this._layerEditingCap[this._currentLayerId].canCreate&&!this._layerEditingCap[this._currentLayerId].canUpdate&&(h=this._listHtml+this._endHtml);var d=u.substitute({href:this._featureLayer._url.path+"/"+t.objectId+"/attachments/"+t.attachmentId+(s?"?"+s:""),name:a,oid:t.objectId,attid:t.attachmentId},h);e.innerHTML=e.innerHTML==this.NLS_none?d:e.innerHTML+d,this._updateConnects(),i.value=""},_onAddAttachmentError:function(t){if(m.hide(this._attachmentProgress.domNode),t){var e=this.NLS_error,i=this._attachmentError;u.isDefined(t.code)&&400===t.code&&(e=this.NLS_fileNotSupported),d.set(i,"innerHTML",e),m.show(i)}},_onDeleteAttachmentComplete:function(t){m.hide(this._attachmentProgress.domNode);var e=this._attachmentList;a.every(t,(function(t){return t.success}))&&(n.query("#node_"+t[0].objectId+"_"+t[0].attachmentId).orphan(),e.children&&e.children.length||(e.innerHTML=this.NLS_none))},_updateConnects:function(){a.forEach(this._aeConnects,i.disconnect),n.query(".deleteAttachment").forEach((function(t){this._aeConnects.push(i.connect(t,"onclick",e.hitch(this,"_deleteAttachment",this._oid,t.id)))}),this)}});return s("extend-esri")&&e.setObject("dijit.editing.AttachmentEditor",y,l),y}));