"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("vue"),V=require("viewerjs"),P=(e,n=!0,a,l)=>{const o=t.ref(),d=t.ref(""),s=t.ref(!1),i=t.ref(!1),w=t.ref(!1),u=t.ref(!1),c=t.ref(),g=()=>{c.value=new IntersectionObserver(f=>{f.forEach(m=>{const{intersectionRatio:y,target:p}=m;if(y>0&&!p.classList.contains("lazy-loaded")){const r=new Image;u.value=!0,r.addEventListener("load",v=>{p.classList.add("lazy-loaded"),d.value=e,w.value=!0,s.value=!0,i.value=!1,u.value=!1,l&&l(v)}),r.addEventListener("error",v=>{p.classList.add("lazy-loaded"),w.value=!0,i.value=!0,s.value=!1,u.value=!1,a&&a(v)}),r.src=e}})},{threshold:[1]}),o.value&&c.value.observe(o.value)};return t.onMounted(()=>{n?g():d.value=e}),t.onUnmounted(()=>{var f;o.value&&((f=c.value)==null||f.unobserve(o.value))}),{lazySrc:d,lazyloadSuccess:s,lazyloadError:i,lazyloaded:w,lazyloading:u,lazyloadTrigger:o}};function L(e){return typeof e[0]=="string"}const E=t.defineComponent({props:{modelValue:{type:Boolean,default:!1},currentPreviewIndex:{type:Number,default:0},previewSrcList:{type:Array,default:()=>[]},viewerOptions:{type:Object,default:()=>({})},viewerTitle:{type:Function,default:void 0},zIndex:{type:[Number,String],default:2015}},emits:["update:modelValue","update:currentPreviewIndex","switch"],setup(e,{emit:n}){const{modelValue:a,previewSrcList:l,currentPreviewIndex:o}=t.toRefs(e),d=t.computed(()=>l.value&&l.value.length>0),s=t.ref(),i=()=>{var r;(r=s.value)==null||r.view(u.value)};t.watch(a,r=>{r&&i()});const w=t.computed(()=>d.value?L(l.value)?l.value.map(r=>({src:r,alt:r})):l.value:[]),u=t.computed({get(){return o.value},set(r){n("update:currentPreviewIndex",r)}}),c=t.computed(()=>l.value.length||0),g=()=>{const r=document.createElement("div");return w.value.forEach(v=>{const h=new Image;Object.keys(v).forEach(z=>{h[z]=v[z]}),r.appendChild(h)}),r.addEventListener("view",v=>{const h=v;u.value=h.detail.index,n("switch",u.value,s.value)}),r.addEventListener("hidden",()=>{n("update:modelValue",!1)}),r},f=t.computed(()=>e.viewerTitle?r=>e.viewerTitle(r,{index:u.value,total:c.value}):r=>`${r.alt} (${u.value+1}/${c.value})`),m=t.computed(()=>Object.assign({title:f.value,zIndex:e.zIndex},e.viewerOptions)),y=()=>{d.value&&(s.value=new V(g(),m.value),a.value&&i())},p=()=>{y()};return t.watch([w,m],()=>{p()},{deep:!0}),t.onMounted(()=>{p()}),t.onUnmounted(()=>{var r;(r=s.value)==null||r.destroy()}),{hasPreviewList:d,PreviewListLength:c,finalPreviewSrcList:w,viewer:s,handleImgView:i}}}),S=(e,n)=>{const a=e.__vccOpts||e;for(const[l,o]of n)a[l]=o;return a},O={class:"image-viewer"};function k(e,n,a,l,o,d){return t.openBlock(),t.createElementBlock("div",O)}const b=S(E,[["render",k],["__scopeId","data-v-0c5bf984"]]),$=t.defineComponent({name:"PreviewableImage",components:{ImageViewer:b},props:{width:{type:String,default:void 0},height:{type:String,default:void 0},src:{type:String,default:""},alt:{type:String,default:""},referrerPolicy:{type:String,default:void 0},fit:{type:String,default:"fill"},zIndex:{type:[Number,String],default:2015},previewSrcList:{type:Array,default:()=>[]},currentPreviewIndex:{type:Number,default:0},viewerOptions:{type:Object,default:()=>({})},viewerTitle:{type:Function,default:void 0},lazy:{type:Boolean,default:!0}},emits:["switch","update:currentPreviewIndex","load","error"],setup(e,{emit:n}){const{previewSrcList:a,currentPreviewIndex:l}=t.toRefs(e),o=t.computed(()=>({"--img-object-fit":e.fit})),{lazySrc:d,lazyloadTrigger:s,lazyloading:i,lazyloadError:w,lazyloadSuccess:u}=P(e.src,e.lazy,r=>{n("load",r)},r=>{n("error",r)}),c=t.computed(()=>a.value&&a.value.length>0),g=t.computed(()=>!(!c.value||e.lazy&&!u.value)),f=t.ref(!1),m=()=>{c.value&&(f.value=!0)},y=t.computed({get(){return l.value},set(r){n("update:currentPreviewIndex",r)}});return{lazyloadTrigger:s,imgStyleVars:o,hasPreviewList:c,lazySrc:d,lazyloading:i,lazyloadError:w,lazyloadSuccess:u,showImageViewer:f,handleImgView:m,currentViewerIndex:y,handleSwitch:(r,v)=>{n("switch",r,v)},initViewer:g}}}),B={key:0,class:"previewable-image__placeholder"},T={key:1,class:"previewable-image__error"},N=["src","alt","referrerpolicy"];function j(e,n,a,l,o,d){const s=t.resolveComponent("ImageViewer");return t.openBlock(),t.createElementBlock("div",{ref:"lazyloadTrigger",class:"previewable-image",style:t.normalizeStyle([{width:e.width,height:e.height},e.imgStyleVars])},[e.lazy&&e.lazyloading?(t.openBlock(),t.createElementBlock("div",B,[t.renderSlot(e.$slots,"placeholder",{},()=>[t.createTextVNode("Loading...")])])):e.lazy&&e.lazyloadError?(t.openBlock(),t.createElementBlock("div",T,[t.renderSlot(e.$slots,"error",{},()=>[t.createTextVNode("Load Error")])])):(t.openBlock(),t.createElementBlock("img",{key:2,src:e.lazySrc,alt:e.alt,referrerpolicy:e.referrerPolicy,class:t.normalizeClass(["previewable-image__inner",{"previewable-image__preview":e.hasPreviewList}]),onClick:n[0]||(n[0]=(...i)=>e.handleImgView&&e.handleImgView(...i))},null,10,N)),e.initViewer?(t.openBlock(),t.createBlock(s,{key:3,modelValue:e.showImageViewer,"onUpdate:modelValue":n[1]||(n[1]=i=>e.showImageViewer=i),"current-preview-index":e.currentViewerIndex,"onUpdate:currentPreviewIndex":n[2]||(n[2]=i=>e.currentViewerIndex=i),"preview-src-list":e.previewSrcList,"viewer-options":e.viewerOptions,"viewer-title":e.viewerTitle,"z-index":e.zIndex,onSwitch:e.handleSwitch},null,8,["modelValue","current-preview-index","preview-src-list","viewer-options","viewer-title","z-index","onSwitch"])):t.createCommentVNode("",!0)],4)}const I=S($,[["render",j]]),_=(e,n)=>{const a=Object.assign({componentName:I.name,defaultViewerOptions:{}},n);e.component(a.componentName,I),V.setDefaults(a.defaultViewerOptions)},C={install:_};exports.Viewer=V;exports.ImageViewer=b;exports.PreviewableImage=I;exports.default=C;