import{__decorate as t}from"tslib";import{s as i}from"./nile-file-preview.css.esm.js";import{N as s}from"../internal/nile-element.esm.js";import{property as e,customElement as h}from"lit/decorators.js";import{i as a,a as r}from"./utils/nile-file-preview.util.esm.js";import{F as l,a as o,b as n,c as p}from"./types/nile-file-preview.enums.esm.js";import{html as c}from"lit";import{g as m,a as u,b as y,c as d,d as f,e as v,f as w,h as b}from"./nile-file-preview.template.esm.js";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";let j=class extends s{constructor(){super(...arguments),this.errorMessage="",this.fileUrl="",this.uploadStatus=0,this.state=l.DEFAULT,this.variant=o.HORIZONTAL,this.inputFileName="",this.showFileType=!1,this.inputFileHtml=null,this.originalUrl="",this.isStringTruncated=!1}static get styles(){return[i]}connectedCallback(){super.connectedCallback(),this.emit(n.NILE_INIT)}firstUpdated(t){super.firstUpdated(t)}updated(t){t.has("uploadStatus")&&this.uploadStatus?this.uploadStatus<100?this.createUploadState(this.inputFile,this.uploadStatus):this.createState(this.inputFile):t.has("inputFile")&&this.inputFile?(this.createState(this.inputFile),this.emit(n.NILE_RECEIVE)):t.has("fileUrl")&&this.fileUrl?(this.handleFileUrl(this.fileUrl),this.emit(n.NILE_RECEIVE)):t.has("errorMessage")&&this.createState(this.inputFile)}createUploadState(t,i){const s=this.variant===o.HORIZONTAL;this.inputFileHtml=s?m(t,i,this):u(t,i,this),this.requestUpdate()}async handleFileUrl(t){this.originalUrl=t;try{const i=await fetch(t),s=await i.blob(),e=this.inputFileName||t.split("/").pop()||p.UNKNOWN_FILE_NAME,h=new File([s],e,{type:s.type});this.createState(h)}catch(t){const i=new File([],this.inputFileName||p.UNKNOWN_FILE_NAME,{type:"application/octet-stream"});this.errorMessage=p.FAILED_TO_LOAD,console.error(this.errorMessage),this.inputFileHtml=this.variant===o.HORIZONTAL?y(i,this.errorMessage,this.originalUrl,this):d(i,this.errorMessage,this.originalUrl,this),this.requestUpdate()}}async createState(t){const i=this.variant===o.HORIZONTAL;if(this.errorMessage)this.inputFileHtml=i?y(t,this.errorMessage,this.originalUrl,this):d(t,this.errorMessage,this.originalUrl,this);else{let s=!1;try{s=t&&await a(t)}catch(t){s=!1}if(s)try{const s=await r(t);this.inputFileHtml=i?f(s,t,this.originalUrl,this.showFileType):v(s,t,this.originalUrl,this.showFileType)}catch(s){this.inputFileHtml=i?w(t,this.originalUrl,this.showFileType):b(t,this.originalUrl,this.showFileType)}else this.inputFileHtml=i?w(t,this.originalUrl,this.showFileType):b(t,this.originalUrl,this.showFileType)}this.requestUpdate()}render(){return c`${this.inputFileHtml??c``}`}disconnectedCallback(){super.disconnectedCallback(),this.emit(n.NILE_DESTROY)}};t([e({type:String})],j.prototype,"errorMessage",void 0),t([e({type:Object})],j.prototype,"inputFile",void 0),t([e({type:String})],j.prototype,"fileUrl",void 0),t([e({type:Number})],j.prototype,"uploadStatus",void 0),t([e({type:String})],j.prototype,"state",void 0),t([e({type:String})],j.prototype,"variant",void 0),t([e({type:String})],j.prototype,"inputFileName",void 0),t([e({type:Boolean,reflect:!0,attribute:!0})],j.prototype,"showFileType",void 0),j=t([h("nile-file-preview")],j);export{j as N};
