2 lines
9.5 KiB
JavaScript
2 lines
9.5 KiB
JavaScript
import{at as X,cL as ee,r as c,k as te,aA as y,I as ie,cM as x,cB as ae,G as le,P as b,cN as re,cO as ne}from"./CqMGdh3i.js";import{u as j}from"./CfQT9Pjy.js";import{g as V,a as se}from"./BzSahvFr.js";import{S as oe,c as ue}from"./DLaLWwro.js";import{a as ce,b as q}from"./Ck7xxMC2.js";import{b as de}from"./BdzjWEBr.js";import{b as Z,l as H,i as I}from"./CJ_Iy2ET.js";import{i as fe}from"./BFKVSULU.js";function ve(e,t,i){for(var s=-1,o=e==null?0:e.length;++s<o;)if(i(t,e[s]))return!0;return!1}var pe=200;function J(e,t,i,s){var o=-1,r=ce,u=!0,n=e.length,_=[],v=t.length;if(!n)return _;i&&(t=X(t,ee(i))),s?(r=ve,u=!1):t.length>=pe&&(r=ue,u=!1,t=new oe(t));e:for(;++o<n;){var d=e[o],l=i==null?d:i(d);if(d=s||d!==0?d:0,u&&l===l){for(var p=v;p--;)if(t[p]===l)continue e;_.push(d)}else r(t,l,s)||_.push(d)}return _}var _e=Z(function(e,t){var i=H(t);return I(i)&&(i=void 0),I(e)?J(e,q(t,1,I,!0),de(i)):[]}),he=Z(function(e,t){var i=H(t);return I(i)&&(i=void 0),I(e)?J(e,q(t,1,I,!0),void 0,i):[]});const F=c(),Y=c(),G=c(!1),D=c(1);let w=[];const C=c(),U=c([]),S=c([]),h=c([]),P=c({url:void 0,index:0}),me=c(),Oe=c(),Q=c([]),A=c([]),je=c([]),ge=c([]),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:[],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=[],A.value=[],D.value=1,P.value={url:void 0,index:0},h.value=[],C.value=void 0,F.value=void 0,me.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,P.value),e=e?.map((t,i)=>t),console.log(e),e});function xe(e){const t=Y.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 s=x(e,ye(N.value[t])),o=URL.createObjectURL(s);N.value[t]=s,h.value[t]=o,F.value=o}function De(e){const t=k(),i=Y.value;if(!e?.startsWith("data:"))return;if(i===-2||F.value===P.value?.url){const v=P.value?.url,d=t.originalFile?.file instanceof File&&t.originalFile.file.name||B(v,"original.jpg"),l=x(e,d),p=URL.createObjectURL(l);P.value.url=p,t.originalFile={file:l,index:0},W(v),F.value=p;return}let s=typeof i=="number"&&i>=0&&i<h.value.length?i:-1;if(s<0&&(s=h.value.findIndex(v=>(typeof v=="string"?v:v?.url)===F.value)),s<0)return;const o=h.value[s],r=typeof o=="string"?o:o.url,u=B(r,`image-${s}.jpg`),n=x(e,u),_=URL.createObjectURL(n);if(console.log(n,e,1111111),typeof o=="string"?h.value[s]=_:h.value[s]={...o,url:_},Array.isArray(t.mediaFiles))if(typeof o=="string")t.mediaFiles[s]&&"file"in t.mediaFiles[s]?t.mediaFiles[s].file=n:t.mediaFiles[s]=n;else{const v=t.mediaFiles.findIndex(d=>d.index===o.index);v>=0?t.mediaFiles[v].file=n:(t.mediaFiles.push({file:n,index:o.index}),t.mediaFiles.sort((d,l)=>(d.index??0)-(l.index??0)))}W(r),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 Ce(e,t){let i=e;D.value=1,S.value=[],U.value=[];let s=0,o=[];e?.forEach((r,u)=>{r.show_in_variable?(D.value=2,E.variants.length===0&&we(),S.value.push({attributeId:r.id,name:r.name,values:r.new_feature,show:r.show_in_variable,valueKey:u}),r.new_feature?.forEach((n,_)=>{U.value.push({attributeName:r.name,attributeId:r.id,valueId:n.id,valueName:n.value,valueKey:s})})):(o.push(u),A.value?.forEach(n=>{i.length>n.attribute_values.length&&n.attribute_values[u]!=null&&(n.attribute_values.length!=u?(n.attribute_values.splice(u,0,null),n.attribute_values_name.splice(u,0,null)):(n.attribute_values=[...n.attribute_values||[],null],n.attribute_values_name=[...n.attribute_values_name||[],null]))})),s++,A.value?.forEach((n,_)=>{i.length>n.attribute_values.length&&n.attribute_values[u]!=null&&(n.attribute_values=[...n.attribute_values||[],null],n.attribute_values_name=[...n.attribute_values_name||[],null])})}),Fe(A.value),o.length!=0&&A.value?.forEach(r=>{o.forEach(u=>{r.attribute_values[u]=null,r.attribute_values_name[u]=null})})}function Fe(e){e?.forEach((t,i)=>{w=y(t.attribute_values),S.value.length<=w.length&&w?.forEach((s,o)=>{U.value?.forEach((r,u)=>{w.find(n=>n==r?.valueId)&&t.attribute_values[r.valueKey]!=w.find(n=>n==r.valueId)&&(t.attribute_values[r.valueKey]=w.find(n=>n==r.valueId),t.attribute_values_name[r.valueKey]=r.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,s)=>{if(i)return t+=(s!==0?" - ":"")+i}),t}async function Ne(){try{G.value=!0,ge.value=await se()}catch(e){console.log(e)}finally{G.value=!1}}async function ke(e){let t;e[e.length-1]!=null&&(t=await V(e[e.length-1]));let i;i=t[0],Object.assign(i,{new_feature:[],show_in_product:!1,show_in_variable:!1}),Q.value.push(i)}async function ze(e){const t=await V(e),i=Q.value.find(s=>s.id===e);i&&(i.attribute_values=[],i.attribute_values=t[0]?.attribute_values?y(t[0]?.attribute_values):[])}const Te=e=>e.split("-")[0]==="edit",L=c([]),N=c([]),k=ae("product",()=>{const e=c(),t=c([]),i=c(0),s=c(!1),o=c(!1),r=le();let u=0;const n=async(d,l=-1)=>{s.value=!0,u=y(100/(t.value.length+3)),i.value=u,l!=-1?await v(d,l):await _(d)},_=async d=>{let l=y(d);const p=await j(e.value.file,{endpoint:r.STORAGE_UPLOAD});i.value=u;try{const a=t.value.map(async(m,f)=>{const g=await j(m.file,{endpoint:r.STORAGE_UPLOAD});return i.value=u*(f+1),{url:g.data?.fullPath,type:m.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.variants?.forEach(m=>{m.discount=m.discount?parseInt(m.discount):void 0})}catch(a){throw b.error(r.useString("theProductImageDidNotLoadCorrectly")),i.value=0,console.error(a),new Error(r.useString("theProductImageDidNotLoadCorrectly"))}if(l.original_image&&!p.error)try{await re(l),R(),i.value=100,b.success(r.useString("addedSuccessfully"))}catch(a){throw console.error(a),b.error(a),i.value=0,new Error(a)}finally{s.value=!1}else throw b.error(r.useString("theProductImageDidNotLoadCorrectly")),s.value=!1,i.value=0,new Error(r.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 m;if(p.length!=0){e.value&&t.value.splice(0,0,e.value);const f=t.value.map(async(g,O)=>{const T=await j(g.file,{endpoint:r.STORAGE_UPLOAD});if(i.value=u*(O+1),T.error)throw b.error(r.useString("theProductImageDidNotLoadCorrectly")),i.value=0,new Error(r.useString("theProductImageDidNotLoadCorrectly"));return{value:{url:T.data?.fullPath,type:g.file.type.includes("video")?"video":"image"},id:p[O]?.index}});m=await Promise.all(f),a.files||(a.files=[]),m.forEach((g,O)=>{a.files[g.id]=g.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(r.useString("updatedSuccessfully"))}catch(f){throw console.error(f),i.value=0,b.error(f),new Error(f)}finally{s.value=!1}t.value=[],e.value=void 0,R()};return{originalFile:e,percentUpload:i,mediaFiles:t,fileUpLoader:n,isFileChanged:o,loading:s}});export{C as A,$ as B,U as C,De as D,me as E,L as a,K as b,E as c,N as d,F as e,Te as f,Y as g,xe as h,Q as i,ge as j,Ne as k,G as l,h as m,ze as n,P as o,Oe as p,ke as q,R as r,je as s,_e as t,k as u,A as v,Ce as w,S as x,D as y,we as z};
|