/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../core/ReactiveMap.js";import{renderingSanitizer as n}from"../../core/sanitizerUtils.js";import{property as o,subclass as s}from"../../core/accessorSupport/decorators.js";import{isSupportedImage as a,getCalciteIconForAttachment as i}from"../Attachments/support/attachmentUtils.js";import l from"./Grid/Column.js";import{css as r}from"./Grid/ColumnCSS.js";const c="esri-attachments-column",h={contentContainer:`${c}__content`,showAttachmentsButton:`${c}__button`};let m=class extends l{constructor(t){super(t),this._buttonElements=new e,this._thumbnailElements=new e,this.attachmentsViewEnabled=!0,this.cellValueFormatFunction=({root:t,rowData:e})=>{const{_showAttachmentsViewEnabled:n,formatFunction:o}=this,s=this.getCellValue(e);if(o&&e){const{index:n,item:{attachments:a,feature:i,relatedRecords:l}}=e;return o({attachments:a,column:this,feature:i,index:n,relatedRecords:l,value:s,virtualIndex:this.getVirtualRowIndex(t),field:void 0})}if(!e)return s;const{index:a,item:{attachments:i,objectId:l}}=e,r=`(${s})`;if(!this.thumbnailsEnabled&&!n)return s;const c=document.createElement("span");c.textContent=c.title=r;let m=null;if(this.thumbnailsEnabled&&(m=this._createOrSyncThumbnails(l,i??[])),this._showAttachmentsViewEnabled){const t=document.createElement("div");m&&t.appendChild(m),t.appendChild(c);const e=this._createOrSyncShowAttachmentsButton(l,a);return e.appendChild(t),e}const u=document.createElement("div");return u.classList.add(h.contentContainer),m&&u.appendChild(m),u.appendChild(c),u},this.icon="attachment",this.layer=null,this.onShowAttachments=null,this.sortable=!1,this.store=null,this.textAlign="center",this.thumbnailAppearance="image",this.thumbnailCount=8,this.thumbnailIconScale="m",this.thumbnailsEnabled=!0}get _showAttachmentsViewEnabled(){return!(!this.attachmentsViewEnabled||!this.onShowAttachments)}get _supportsResize(){return this.store.supportsResizeAttachments}get effectiveLabel(){return n.sanitize(this.label??(this.messages?.attachments||this.fieldName))}getCellValue(t){return t?.item?.attachments?.length??0}_createOrSyncThumbnails(t,e){const n=document.createElement("div"),o=this._thumbnailElements.get(t);return o?.length&&o.length===e.length?o.forEach(t=>n.appendChild(t)):this._thumbnailElements.set(t,this._createThumbnailElements(e,n)),n}_createOrSyncShowAttachmentsButton(t,e){const n=this._buttonElements.get(t);if(n)return n.textContent="",n.onclick=n=>this._onShowAttachmentsClick({index:e,objectId:t,event:n}),n;const o=this.createCalciteButton({alignment:"icon-end-space-between",className:`${h.showAttachmentsButton} ${r.contentFull}`,iconEnd:"chevron-right",iconFlipRtl:"both",scale:this.thumbnailIconScale,textContent:"",title:this.messages?.viewAttachments,width:"full",onclick:n=>this._onShowAttachmentsClick({index:e,objectId:t,event:n})});return this._buttonElements.set(t,o),o}_createThumbnailAnchor({name:t,url:e},n){const o=document.createElement("a");return o.href=`${e}`,o.download=t,o.rel="noreferrer",o.target="_blank",o.appendChild(n),o}_createThumbnailElements(t,e){const{thumbnailAppearance:n,thumbnailCount:o,thumbnailIconScale:s}=this,l=[],r=Math.min(o,t.length),c=!this.onShowAttachments,h="image"===n&&this.store.supportsResizeAttachments;for(let m=0;m<r;m++){const n=t[m],{name:o,contentType:r}=n,u=a(r)&&h?this._createThumbnailImage(n):this.createCalciteIcon({icon:i(r),scale:s,textLabel:o}),p=c?this._createThumbnailAnchor(n,u):u;e&&e.appendChild(p),l.push(p)}return l}_createThumbnailImage(t){const{name:e,size:n,url:o}=t,s=document.createElement("img"),a=`${o}${o?.includes("?")?"&":"?"}${this._supportsResize?"w=24":""}&fs=${n}`;return s.alt=this.messages?.attachmentThumbnail??e,s.src=a,s.title=e,s}_onShowAttachmentsClick({index:t,objectId:e,event:n}){n.preventDefault(),n.stopPropagation(),this.onShowAttachments?.({index:t,objectId:e})}};t([o()],m.prototype,"_showAttachmentsViewEnabled",null),t([o()],m.prototype,"_buttonElements",void 0),t([o()],m.prototype,"_supportsResize",null),t([o()],m.prototype,"_thumbnailElements",void 0),t([o()],m.prototype,"attachmentsViewEnabled",void 0),t([o()],m.prototype,"cellValueFormatFunction",void 0),t([o()],m.prototype,"effectiveLabel",null),t([o()],m.prototype,"icon",void 0),t([o()],m.prototype,"layer",void 0),t([o()],m.prototype,"onShowAttachments",void 0),t([o({readOnly:!0})],m.prototype,"sortable",void 0),t([o()],m.prototype,"store",void 0),t([o()],m.prototype,"textAlign",void 0),t([o()],m.prototype,"thumbnailAppearance",void 0),t([o()],m.prototype,"thumbnailCount",void 0),t([o()],m.prototype,"thumbnailIconScale",void 0),t([o()],m.prototype,"thumbnailsEnabled",void 0),m=t([s("esri.widgets.FeatureTable.AttachmentsColumn")],m);const u=m;export{u as default};
