2 lines
9.6 KiB
JavaScript
2 lines
9.6 KiB
JavaScript
import{au as X,cM as ee,r as o,l as te,aB as y,J as ie,cN as j,cC as ae,H as le,Q as b,cO as se,cP as ne}from"./CE4SaZgW.js";import{u as O}from"./CfQT9Pjy.js";import{g as H,a as re}from"./Ah7i0Akn.js";import{S as oe,c as ue}from"./0N7JoioX.js";import{a as ce,b as V}from"./BK6U06JP.js";import{b as de}from"./CxVR7ht-.js";import{b as q,l as J,i as I}from"./B0tUBnBT.js";import{i as fe}from"./DFL6LqjW.js";function ve(e,t,i){for(var r=-1,u=e==null?0:e.length;++r<u;)if(i(t,e[r]))return!0;return!1}var pe=200;function Z(e,t,i,r){var u=-1,s=ce,c=!0,n=e.length,_=[],v=t.length;if(!n)return _;i&&(t=X(t,ee(i))),r?(s=ve,c=!1):t.length>=pe&&(s=ue,c=!1,t=new oe(t));e:for(;++u<n;){var d=e[u],l=i==null?d:i(d);if(d=r||d!==0?d:0,c&&l===l){for(var p=v;p--;)if(t[p]===l)continue e;_.push(d)}else s(t,l,r)||_.push(d)}return _}var _e=q(function(e,t){var i=J(t);return I(i)&&(i=void 0),I(e)?Z(e,V(t,1,I,!0),de(i)):[]}),he=q(function(e,t){var i=J(t);return I(i)&&(i=void 0),I(e)?Z(e,V(t,1,I,!0),void 0,i):[]});const F=o(),Q=o(),G=o(!1),x=o(1),De=o(!1);let w=[];const C=o(),U=o([]),S=o([]),h=o([]),A=o({url:void 0,index:0}),Oe=o([]),je=o([]),ge=o(),xe=o(),Y=o([]),P=o([]),Ce=o([]),me=o([]),E=ie({title_fa:"",title:"",price:0,body:"",is_show:!0,is_active:!0,box_size:0,sell_type:"numeric",original_image:void 0,product_attributes:[],upload_files:[],files:[],price_type:"manual",auto_calculate:!1,is_exist:!0,created_at:void 0,updated_at:void 0,deleted_at:null,count_order:void 0,count_fav:void 0,count_view:void 0,min_price:void 0,max_price:void 0,type:"simple",owner_id:void 0,users:void 0,saved:[],variants:[],code:void 0,brand_id:void 0,collection_id:void 0,collections:{parent_id:void 0,id:void 0},discount:void 0,discount_type:void 0,id:void 0,count:void 0,display_price:void 0,meta:void 0,seo_title:void 0,description:void 0,product_type:void 0,weight:void 0,rial_fee:void 0,percent_fee:void 0}),be=y(E);function R(){Object.assign(E,be),S.value=[],U.value=[],P.value=[],x.value=1,A.value={url:void 0,index:0},h.value=[],C.value=void 0,F.value=void 0,ge.value=void 0,N.value=[],k().mediaFiles=[],k().originalFile=void 0,(E.count??0)<=0&&(E.count=void 0)}const K=te(()=>{let e=y(h.value);return e?.splice(0,0,A.value),e=e?.map((t,i)=>t),console.log(e),e});function Ne(e){const t=Q.value;if(!Array.isArray(h.value)||t==null||t<0||t>=h.value.length){console.warn("invalid index or list",t,h.value?.length);return}const i=h.value[t];if(i&&i?.startsWith("blob:"))try{URL.revokeObjectURL(i)}catch{}const r=j(e,ye(N.value[t])),u=URL.createObjectURL(r);N.value[t]=r,h.value[t]=u,F.value=u}function ke(e){const t=k(),i=Q.value;if(!e?.startsWith("data:"))return;if(i===-2||F.value===A.value?.url){const v=A.value?.url,d=t.originalFile?.file instanceof File&&t.originalFile.file.name||B(v,"original.jpg"),l=j(e,d),p=URL.createObjectURL(l);A.value.url=p,t.originalFile={file:l,index:0},W(v),F.value=p;return}let r=typeof i=="number"&&i>=0&&i<h.value.length?i:-1;if(r<0&&(r=h.value.findIndex(v=>(typeof v=="string"?v:v?.url)===F.value)),r<0)return;const u=h.value[r],s=typeof u=="string"?u:u.url,c=B(s,`image-${r}.jpg`),n=j(e,c),_=URL.createObjectURL(n);if(console.log(n,e,1111111),typeof u=="string"?h.value[r]=_:h.value[r]={...u,url:_},Array.isArray(t.mediaFiles))if(typeof u=="string")t.mediaFiles[r]&&"file"in t.mediaFiles[r]?t.mediaFiles[r].file=n:t.mediaFiles[r]=n;else{const v=t.mediaFiles.findIndex(d=>d.index===u.index);v>=0?t.mediaFiles[v].file=n:(t.mediaFiles.push({file:n,index:u.index}),t.mediaFiles.sort((d,l)=>(d.index??0)-(l.index??0)))}W(s),F.value=_}function W(e){if(e&&e.startsWith("blob:"))try{URL.revokeObjectURL(e)}catch{}}function B(e,t="image.jpg"){if(!e)return t;try{const i=new URL(e).pathname.split("/").pop()||"";if(/\.[a-z0-9]+$/i.test(i))return i}catch{}return t}function ye(e){if(e instanceof File)return M(e.name);const t="image.jpg";return e&&typeof e=="object"&&"name"in e&&typeof e.name=="string"?M(e.name):t}function M(e){return e&&/\.[a-zA-Z0-9]+$/.test(e)?e:`${e||"image"}.jpg`}function we(){let e=[],t=[];U.value?.forEach(i=>{i.valueId,e.push(i.valueId),t.push(i.valueName)}),console.log(e,U.value),E.variants?.push({id:void 0,attribute_values:[],attribute_values_name:[],price:0,weight:0,stock:0,auto_calculate:!1,discount_type:void 0,discount:void 0,is_default:!1,sku:""})}function ze(e,t){let i=e;x.value=1,S.value=[],U.value=[];let r=0,u=[];e?.forEach((s,c)=>{s.show_in_variable?(x.value=2,E.variants.length===0&&we(),S.value.push({attributeId:s.id,name:s.name,values:s.new_feature,show:s.show_in_variable,valueKey:c}),s.new_feature?.forEach((n,_)=>{U.value.push({attributeName:s.name,attributeId:s.id,valueId:n.id,valueName:n.value,valueKey:r})})):(u.push(c),P.value?.forEach(n=>{i.length>n.attribute_values.length&&n.attribute_values[c]!=null&&(n.attribute_values.length!=c?(n.attribute_values.splice(c,0,null),n.attribute_values_name.splice(c,0,null)):(n.attribute_values=[...n.attribute_values||[],null],n.attribute_values_name=[...n.attribute_values_name||[],null]))})),r++,P.value?.forEach((n,_)=>{i.length>n.attribute_values.length&&n.attribute_values[c]!=null&&(n.attribute_values=[...n.attribute_values||[],null],n.attribute_values_name=[...n.attribute_values_name||[],null])})}),Fe(P.value),u.length!=0&&P.value?.forEach(s=>{u.forEach(c=>{s.attribute_values[c]=null,s.attribute_values_name[c]=null})})}function Fe(e){e?.forEach((t,i)=>{w=y(t.attribute_values),S.value.length<=w.length&&w?.forEach((r,u)=>{U.value?.forEach((s,c)=>{w.find(n=>n==s?.valueId)&&t.attribute_values[s.valueKey]!=w.find(n=>n==s.valueId)&&(t.attribute_values[s.valueKey]=w.find(n=>n==s.valueId),t.attribute_values_name[s.valueKey]=s.valueName)})}),i==0&&(C.value=$(t.attribute_values_name)),t.is_default&&(C.value=$(t.attribute_values_name))})}function $(e){let t="";return e.forEach((i,r)=>{if(i)return t+=(r!==0?" - ":"")+i}),t}async function Te(){try{G.value=!0,me.value=await re()}catch(e){console.log(e)}finally{G.value=!1}}async function Ge(e){let t;e[e.length-1]!=null&&(t=await H(e[e.length-1]));let i;i=t?t[0]:null,i&&(Object.assign(i,{new_feature:[],show_in_product:!1,show_in_variable:!1}),Y.value.push(i))}async function Ke(e){const t=await H(e),i=Y.value.find(r=>r.id===e);i&&(i.attribute_values=[],i.attribute_values=t[0]?.attribute_values?y(t[0]?.attribute_values):[])}const We=e=>e.split("-")[0]==="edit",L=o([]),N=o([]),k=ae("product",()=>{const e=o(),t=o([]),i=o(0),r=o(!1),u=o(!1),s=le();let c=0;const n=async(d,l=-1)=>{r.value=!0,c=y(100/(t.value.length+3)),i.value=c,l!=-1?await v(d,l):await _(d)},_=async d=>{let l=y(d);const p=await O(e.value.file,{endpoint:s.STORAGE_UPLOAD});i.value=c;try{const a=t.value.map(async(g,f)=>{const m=await O(g.file,{endpoint:s.STORAGE_UPLOAD});return i.value=c*(f+1),{url:m.data?.fullPath,type:g.file.type.includes("video")?"video":"image"}});l.original_image=p.data?.fullPath,l.files.push({url:p.data?.fullPath,type:e.value.file.type.includes("video")?"video":"image"});const z=await Promise.all(a);l.files.push(...z),l.collection_id=l.collection_id??l.collections?.parent_id,l.price=l.price?parseInt(l.price):0,l.weight=l.weight?parseFloat(l.weight):0,l.percent_fee=l.percent_fee?parseFloat(l.percent_fee):0,l.rial_fee=l.rial_fee?parseInt(l.rial_fee):0,l.count=l.count?parseInt(l.count):void 0,l.discount=l.discount?parseInt(l.discount):void 0,l.box_size=parseInt(l.box_size),l.upload_files=d.upload_files,l.variants?.forEach(g=>{g.discount=g.discount?parseInt(g.discount):void 0})}catch(a){throw b.error(s.useString("theProductImageDidNotLoadCorrectly")),i.value=0,console.error(a),new Error(s.useString("theProductImageDidNotLoadCorrectly"))}if(l.original_image&&!p.error)try{await se(l),R(),i.value=100,b.success(s.useString("addedSuccessfully"))}catch(a){throw console.error(a),b.error(a),i.value=0,new Error(a)}finally{r.value=!1}else throw b.error(s.useString("theProductImageDidNotLoadCorrectly")),r.value=!1,i.value=0,new Error(s.useString("theProductImageDidNotLoadCorrectly"));R(),t.value=[],e.value=void 0},v=async(d,l)=>{let p,a=y(d);_e(L.value,K.value,"index").forEach(f=>{L.value.splice(f.index,1,null),a.files.splice(f.index,1,null)}),L.value=L.value.filter(f=>f!=null),p=he(K.value,L.value,fe);let g;if(p.length!=0){e.value&&t.value.splice(0,0,e.value);const f=t.value.map(async(m,D)=>{const T=await O(m.file,{endpoint:s.STORAGE_UPLOAD});if(i.value=c*(D+1),T.error)throw b.error(s.useString("theProductImageDidNotLoadCorrectly")),i.value=0,new Error(s.useString("theProductImageDidNotLoadCorrectly"));return{value:{url:T.data?.fullPath,type:m.file.type.includes("video")?"video":"image"},id:p[D]?.index}});g=await Promise.all(f),a.files||(a.files=[]),g.forEach((m,D)=>{a.files[m.id]=m.value})}a.files=a.files.filter(f=>f!=null),a.original_image=a.files[0].url,a.price=a.price?parseInt(a.price):void 0,a.weight=a.weight?parseFloat(a.weight):void 0,a.percent_fee=a.weight?parseFloat(a.percent_fee):void 0,a.rial_fee=a.rial_fee?parseInt(a.rial_fee):0,a.count=a.count?parseInt(a.count):void 0,a.discount=a.discount?parseInt(a.discount):void 0,a.collection_id=a.collection_id??a.collections?.parent_id,a.collection_id=a?.collection_id??null,delete a.collections,a.variants?.forEach(f=>{f.discount=f.discount?parseInt(f.discount):void 0}),a.box_size=parseInt(a.box_size),a.discount==null&&(a.discount=void 0);try{i.value=99.5,await ne(a,l),R(),i.value=100,b.success(s.useString("updatedSuccessfully"))}catch(f){throw console.error(f),i.value=0,b.error(f),new Error(f)}finally{r.value=!1}t.value=[],e.value=void 0,R()};return{originalFile:e,percentUpload:i,mediaFiles:t,fileUpLoader:n,isFileChanged:u,loading:r}});export{Oe as A,we as B,je as C,C as D,$ as E,U as F,ke as G,ge as H,L as a,K as b,E as c,N as d,F as e,We as f,Q as g,Ne as h,Y as i,me as j,De as k,G as l,h as m,Te as n,A as o,xe as p,Ke as q,R as r,Ce as s,Ge as t,k as u,_e as v,P as w,ze as x,S as y,x as z};
|