import{D as i,T as o,_ as d,s as a}from"./app-B4kV47qU.js";import{u as l}from"./modalStore-BgORG3Nn.js";const h=i("asset",{state:()=>({allAssets:{data:[],currentPage:1,loadMore:!1,loading:!1,newest:null},assetUploadProgress:0,assetForm:o({file:[]}),files:[],selectedAssets:[],events:{load:"image",button:void 0,caption:void 0,multiple:!1,selected:[],onOpen:()=>{},onSelect:s=>{},onSubmit:s=>{}}}),actions:{openModal(s){this.events={...this.events,...s},this.selectedAssets=s.selected??[],this.events.onOpen(),this.loadAssets(this.events.load),l().open("assetModal")},closeModal(){l().close("assetModal")},toggleSelect(s){if(!this.events.multiple){this.selectedAssets=[s],this.events.onSelect(s);return}this.isSelected(s)?this.selectedAssets=this.selectedAssets.filter(e=>e.id!==s.id):(this.selectedAssets.push(s),this.events.onSelect(s))},onSubmit(){this.events.onSubmit(this.selectedAssets,this.events),this.closeModal()},loadAssets(s=null){this.allAssets.loading=!0,s==="voice"&&(s="audio"),a.get(route("api-assets-all",{file_type:s,page:1})).then(e=>{var t;this.allAssets.data=e.data.data,((t=e.data.data)==null?void 0:t.length)>0&&(this.allAssets.newest=e.data.data[0].id),e.data.current_page!==e.data.last_page&&(this.allAssets.loadMore=!0),e.data.next_page_url||(this.allAssets.loadMore=!1)}).finally(()=>{this.allAssets.loading=!1,this.events.load=s})},loadMoreAssets(){this.allAssets.loadMore&&(this.allAssets.currentPage++,this.allAssets.loadMore=!0,this.allAssets.loading=!0,a.get(route("api-assets-all",{page:this.allAssets.currentPage,file_type:this.events.load})).then(s=>{this.allAssets.data.push(...s.data.data),this.allAssets.currentPage=s.data.current_page,s.data.next_page_url||(this.allAssets.loadMore=!1)}).finally(()=>{this.allAssets.loading=!1}))},setFileType(s){return s.type.startsWith("image/")?"image":s.type.startsWith("audio/")?"audio":s.type.startsWith("video/")?"video":"document"},assetSubmit(){const s=this.files.map(e=>({file:e,type:this.setFileType(e)}));d.post(route("user.assets.store"),{files:s},{onProgress:e=>{this.assetUploadProgress=e.percentage},onFinish:()=>{setTimeout(()=>this.assetUploadProgress=0,1e3)},onSuccess:()=>{this.files=[],a.get(route("api-assets-all",{newest:this.allAssets.newest,file_type:this.events.load})).then(e=>{var t;((t=e.data)==null?void 0:t.length)>0&&(this.allAssets.newest=e.data[0].id,this.allAssets.data.unshift(...e.data))})}})},isSelected(s){return this.selectedAssets.map(e=>e.path).includes(s.path)}}});export{h as u};
