import{a1 as u,T as h,a5 as n,O as c,Q as g,q as f}from"./app-13978987.js";import{u as l}from"./publishStore-a6d0eb83.js";const V=u({id:"publishAssetStore",state:()=>({assetForm:h({image:"",video:""}),imageSearchQuery:"",videoSearchQuery:"",generateVideoId:null,loading:{stockImages:!1,stockVideos:!1,aiImages:!1,aiVideo:!1},assetImageInput:null,assetVideoInput:null,stockImages:[],stockVideos:[],aiImages:[],aiVideos:[],isStockImagesFetched:!1,isStockVideosFetched:!1,isAiImageFetched:!1,isAiVideoFetched:!1,assetUploadProgress:0,assets:{uploadedImages:{data:[],currentPage:1,loadMore:!1},generatedImages:{data:[],currentPage:1,loadMore:!1},uploadedVideos:{data:[],currentPage:1,loadMore:!1},generatedVideos:{data:[],currentPage:1,loadMore:!1},loading:!1}}),actions:{setAssets(s,t,e,a){const i={generatedImages:{newData:s.data,loadMore:!!s.next_page_url},generatedVideos:{newData:e.data,loadMore:!!e.next_page_url},uploadedImages:{newData:t.data,loadMore:!!t.next_page_url},uploadedVideos:{newData:a.data,loadMore:!!a.next_page_url}};Object.entries(i).forEach(([o,{newData:d,loadMore:r}])=>{this.assets[o].data=[...this.assets[o].data,...d],this.assets[o].loadMore=r})},stockImageSearch(){this.loading.stockImages=!0,this.isStockImagesFetched=!1,n.get(route("user.generate-stock-image",{query:this.imageSearchQuery})).then(s=>this.stockImages=s.data.results).finally(()=>{setTimeout(()=>{this.isStockImagesFetched=!0,this.loading.stockImages=!1},2e3)})},generateImage(){const s=l();this.loading.aiImages=!0,c.post(route("user.generate-image"),{brand_id:s.form.brand_id,description:s.form.input},{only:["generatedAssetImages"],onSuccess:t=>{const e=t.props.generatedAssetImages.data[0];this.assets.generatedImages.data.find(i=>i.id===e.id)||this.assets.generatedImages.data.unshift(e)},onError:()=>{var t;(t=g().props.errors)!=null&&t.ai_error&&f.danger(g().props.errors.ai_error)},onFinish:()=>{setTimeout(()=>this.loading.aiImages=!1,3e3)}})},submitAsset(s,t){this.assetForm[t]=s.target.files[0],this.assetForm.post(route("user.assets.store"),{onProgress:e=>{this.assetUploadProgress=e.percentage},onSuccess:e=>{var a,i;if(t==="image"&&((a=e.props.uploadedAssetImages.data)==null?void 0:a.length)>0){const o=e.props.uploadedAssetImages.data[0];this.assets.uploadedImages.data.find(r=>r.id===o.id)||this.assets.uploadedImages.data.unshift(o)}if(t==="video"&&((i=e.props.uploadedAssetVideos.data)==null?void 0:i.length)>0){const o=e.props.uploadedAssetVideos.data[0];this.assets.uploadedVideos.data.find(r=>r.id===o.id)||this.assets.uploadedVideos.data.unshift(o)}this.assetForm.reset()},onFinish:()=>{setTimeout(()=>this.assetUploadProgress=0,1e3)}})},stockVideoSearch(){this.loading.stockVideos=!0,this.isStockVideosFetched=!1,n.get(route("user.stock-video",{query:this.videoSearchQuery})).then(s=>{const t=s.data.videos;this.stockVideos=t.map(e=>e.video_files[0])}).finally(()=>{setTimeout(()=>{this.isStockVideosFetched=!0,this.loading.stockVideos=!1},2e3)})},async processVideo(s,t,e=!1){const a=await n.post(s);if(a.data.status=="processing"&&(e&&(this.generateVideoId=a.data.id),await new Promise(i=>setTimeout(i,t)),this.fetchVideo()),a.data.status=="success"){this.aiVideos=a.data.output[0];const i=g().props.generatedAssetVideos.data[0];this.assets.generatedVideos.data.find(d=>d.id===i.id)||this.assets.generatedVideos.data.unshift(i),this.generateVideoId=null,setTimeout(()=>this.loading.aiVideo=!1,3e3)}a.data.status=="failed"&&(this.aiVideos=[],this.generateVideoId=null,setTimeout(()=>this.loading.aiVideo=!1,3e3))},fetchVideo(){this.processVideo(route("user.fetch-video",{id:this.generateVideoId}),3e4)},generateVideo(){const s=l();this.loading.aiVideo=!0,this.processVideo(route("user.generate-video",{brand_id:s.form.brand_id}),3e4,!0)},loadMoreAssets(s,t){const e=`${s}${t}`;this.assets[e].currentPage++,this.assets[e].loadMore=!0,this.assets.loading=!0,n.get(route("api-assets",{type:s,mime_type:t,page:this.assets[e].currentPage})).then(a=>{this.assets[e].data.push(...a.data.data),this.assets[e].currentPage=a.data.current_page,a.data.next_page_url||(this.assets[e].loadMore=!1)}).finally(()=>{this.assets.loading=!1})}}});export{V as u};
