1import{f as m}from"./index-DeUI6rSe.js";function de(e){return/^https?:\/\//i.test(e.trim())}function C(e){return{id:e.id,name:e.name??"",brand:e.brand??void 0,vendor_sku:e.vendor_sku??void 0,wb_sku:e.wb_sku??void 0,public_article:e.public_article!=null?Number(e.public_article):void 0,barcode:e.barcode??void 0,promocode:e.promocode??void 0,description:e.description??void 0,category_id:e.category_id??void 0,category:e.category??void 0,subject:e.subject??void 0,characteristics:Array.isArray(e.characteristics)?e.characteristics:[],photos:Array.isArray(e.photos)?e.photos.filter(n=>typeof n=="string"&&!/\/storage\/v1\/object\/sign\/funnel-media\//i.test(n)):[],documents:Array.isArray(e.documents)?e.documents:[],extra:Array.isArray(e.extra)?e.extra:[],publish_error:Array.isArray(e.extra)&&e.extra.find(n=>n?.key==="_publishError")?.value||void 0,price:e.price!=null?Number(e.price):void 0,cost:e.cost!=null?Number(e.cost):void 0,stock:e.stock!=null?Number(e.stock):void 0,length_cm:e.length_cm!=null?Number(e.length_cm):void 0,width_cm:e.width_cm!=null?Number(e.width_cm):void 0,height_cm:e.height_cm!=null?Number(e.height_cm):void 0,weight_kg:e.weight_kg!=null?Number(e.weight_kg):void 0,tnved:e.tnved??void 0,source:e.source??void 0,published:e.published??void 0,funnel_id:e.funnel_id??null,created_at:e.created_at}}function F(e){const n={},t=(i,o=i)=>{if(!(i in e))return;const s=e[i];n[o]=s===void 0||s===""?null:s};return t("name"),t("brand"),t("vendor_sku"),t("wb_sku"),t("barcode"),t("promocode"),t("description"),t("category_id"),t("category"),t("subject"),t("price"),t("cost"),t("stock"),t("length_cm"),t("width_cm"),t("height_cm"),t("weight_kg"),t("tnved"),t("source"),e.photos!==void 0&&(n.photos=e.photos.filter(de)),e.characteristics!==void 0&&(n.characteristics=e.characteristics),e.documents!==void 0&&(n.documents=e.documents),e.extra!==void 0&&(n.extra=e.extra),n}function oe(e){return{photos:[],characteristics:[],documents:[],extra:[],...e}}async function Ee(){const{data:e}=await m.auth.getUser(),n=e.user?.id;if(!n)return[];const t=1e3,i=[];for(let o=0;;o+=t){const{data:s,error:a}=await m.from("products").select("*").eq("user_id",n).order("updated_at",{ascending:!1,nullsFirst:!1}).order("created_at",{ascending:!1}).range(o,o+t-1);if(a)throw new Error(a.message);const c=s??[];for(const f of c)i.push(C(f));if(c.length<t)break}return i}const V={name:"name",brand:"brand",vendor_sku:"vendor_sku",wb_sku:"wb_sku",barcode:"barcode",promocode:"promocode",category:"category",tnved:"tnved",source:"source",description:"description"},Z={price:"price",cost:"cost",stock:"stock",dimensions:"length_cm"},D={...V,...Z,created_at:"created_at"};function X(e){return e.replace(/([\\%_,()])/g,"\\$1")}function xe(e){return e==="photo"||e==="status"||e in V||e in Z}function Se(e){return e in D}function U(e,n){let t=e;const i=n.search.trim();if(i){const o=X(i);t=t.or(["name","vendor_sku","wb_sku","barcode","promocode","tnved"].map(s=>`${s}.ilike.%${o}%`).join(","))}for(const{colId:o,filter:s}of n.filters)if(s.type==="photo")s.mode==="has"?t=t.eq("has_photo",!0):s.mode==="no"&&(t=t.eq("has_photo",!1));else if(s.type==="text"){const a=V[o];if(!a)continue;for(const c of s.rules){const f=c.value.trim();if(f)if(c.exact){const u=X(f);c.mode==="contains"?t=t.ilike(a,u):t=t.not(a,"ilike",u)}else{const u=X(f);c.mode==="contains"?t=t.ilike(a,`%${u}%`):t=t.not(a,"ilike",`%${u}%`)}}}else if(s.type==="number"){const a=Z[o];if(!a)continue;const c=s.min.trim()===""?null:Number(s.min),f=s.max.trim()===""?null:Number(s.max);c!==null&&!Number.isNaN(c)&&(t=t.gte(a,c)),f!==null&&!Number.isNaN(f)&&(t=t.lte(a,f))}else if(s.type==="set"){const a=s.values.filter(u=>u&&u.length>0),c=(s.exclude??[]).filter(u=>u&&u.length>0);if(a.length===0&&c.length===0)continue;if(o==="status"){const u=a.includes("ÐпÑбликовано"),p=a.includes("ЧеÑновик"),k=a.includes("ÐÑибка");if(k&&!u&&!p){t=t.filter("extra","cs",'[{"key":"_publishError"}]');continue}if(u&&p&&!k)continue;u&&!p&&!k?t=t.or("published.eq.true,and(wb_sku.not.is.null,wb_sku.neq.,or(source.ilike.*wildberries*,source.ilike.*валбеÑиÑ*,source.ilike.WB*))"):p&&!u&&!k&&(t=t.eq("published",!1),t=t.or("wb_sku.is.null,wb_sku.eq.,source.is.null,and(source.not.ilike.*wildberries*,source.not.ilike.*валбеÑиÑ*,source.not.ilike.WB*)"));continue}const f=V[o];if(!f)continue;a.length>0&&o!=="source"&&(t=t.in(f,a)),c.length>0&&(t=t.not(f,"in",`(${c.map(u=>`"${u.replace(/"/g,'\\"')}"`).join(",")})`))}return t}async function G(e,n){const i=[];for(let o=0;;o+=1e3){const{data:s,error:a}=await m.from("product_folder_items").select("product_id").eq("user_id",e).eq("folder_id",n).range(o,o+1e3-1);if(a)throw new Error(a.message);const c=s??[];for(const f of c)i.push(f.product_id);if(c.length<1e3)break}return i}async function T(e,n){if(!e||n.length===0)return[];const t=new Set,i=1e3;for(let o=0;;o+=i){const{data:s,error:a}=await m.from("products").select("vendor_sku").eq("user_id",e).in("source",n).not("vendor_sku","is",null).range(o,o+i-1);if(a)throw new Error(a.message);const c=s??[];for(const f of c){const u=f.vendor_sku;if(!u)continue;t.add(u),u.endsWith("_vbb")&&t.add(u.slice(0,-4));const p=u.match(/^(.*)_k_\d+$/u);p&&t.add(p[1])}if(c.length<i)break}return Array.from(t)}const ne=4e3,ie=200,ce=5e3,W=200;function ae(e){return`(${e.map(n=>`"${n.replace(/"/g,'\\"')}"`).join(",")})`}function ee(e,n){if(n.length===0)return{q:e,excludedSet:null};const t=ae(n);return t.length<=ne?{q:e.not("vendor_sku","in",t),excludedSet:null}:{q:e,excludedSet:new Set(n)}}function K(e){const n=e.filters.find(t=>t.colId==="source");return!n||n.filter.type!=="set"?[]:(n.filter.exclude??[]).filter(t=>t&&t.length>0)}function le(e){const n=e.filters.find(t=>t.colId==="source");return!n||n.filter.type!=="set"?[]:(n.filter.values??[]).filter(t=>t&&t.length>0)}function fe(e,n){const t=[...e],i=n?.colId?D[n.colId]:null,o=n?.dir==="asc"?1:-1;return t.sort((s,a)=>{const c=i?s?.[i]:s?.updated_at??s?.created_at,f=i?a?.[i]:a?.updated_at??a?.created_at;return c==null&&f==null?0:c==null?1:f==null?-1:typeof c=="number"&&typeof f=="number"?(c-f)*o:String(c).localeCompare(String(f),"ru",{numeric:!0})*o}),t}async function he(e,n,t,i,o){const s=[];for(let u=0;u<n.length;u+=W){const p=n.slice(u,u+W);let k=m.from("products").select(i).eq("user_id",e).in("id",p);k=U(k,t);const{data:g,error:_}=await k;if(_)throw new Error(_.message);for(const v of g??[]){const E=v.vendor_sku;o&&E&&o.has(E)||s.push(v)}}const a=fe(s,t.sort),c=t.to-t.from+1;return{rows:a.slice(t.from,t.to+1).map(C),hasMore:a.length>t.from+c}}async function me(e,n,t,i){let o=0;for(let s=0;s<n.length;s+=W){const a=n.slice(s,s+W);if(i){let c=m.from("products").select("id,vendor_sku").eq("user_id",e).in("id",a);c=U(c,{...t});const{data:f,error:u}=await c;if(u)throw new Error(u.message);for(const p of f??[]){const k=p.vendor_sku;k&&i.has(k)||o++}}else{let c=m.from("products").select("*",{count:"exact",head:!0}).eq("user_id",e).in("id",a);c=U(c,{...t});const{count:f,error:u}=await c;if(u)throw new Error(u.message);o+=f??0}}return o}async function we(e){if(e.length===0)return[];const{data:n,error:t}=await m.rpc("list_my_product_ids_by_sources",{_sources:e});if(t)return console.warn("list_my_product_ids_by_sources failed",t.message),[];const i=[];for(const o of n??[])o.id&&i.push(o.id);return i}const pe="id,name,brand,vendor_sku,wb_sku,public_article,barcode,promocode,description,category_id,category,subject,photos,price,cost,stock,length_cm,width_cm,height_cm,weight_kg,tnved,source,published,funnel_id,created_at";async function Ae(e,n="*"){const{data:t}=await m.auth.getUser(),i=t.user?.id,o=n==="lite"?pe:"*";let s=m.from("products").select(o);i&&(s=s.eq("user_id",i));let a=null,c=e.restrictProductIds;if(e.folderId&&i){const l=await G(i,e.folderId);if(l.length===0)return{rows:[],hasMore:!1};if(c&&c.length>0){const w=new Set(l);if(c=c.filter(y=>w.has(y)),c.length===0)return{rows:[],hasMore:!1}}else c=l}const f=K(e);let u=null;if(f.length>0&&i){const l=await T(i,f),w=ee(s,l);s=w.q,u=w.excludedSet}
1const p=le(e);if(p.length>0&&i){const l=await we(p);if(l.length===0)return{rows:[],hasMore:!1};if(c&&c.length>0){const w=new Set(c);if(c=l.filter(y=>w.has(y)),c.length===0)return{rows:[],hasMore:!1}}else c=l}if(c!=null){if(c.length===0)return{rows:[],hasMore:!1};c.length<=ie?s=s.in("id",c):a=new Set(c)}if(s=U(s,e),a&&c&&c.length<=ce&&i)return he(i,c,e,o,u);e.sort&&D[e.sort.colId]?s=s.order(D[e.sort.colId],{ascending:e.sort.dir==="asc",nullsFirst:!1}):s=s.order("updated_at",{ascending:!1,nullsFirst:!1}).order("created_at",{ascending:!1});const k=e.to-e.from+1;if(!u&&!a){s=s.range(e.from,e.to);const{data:l,error:w}=await s;if(w)throw new Error(w.message);const y=(l??[]).map(C);return{rows:y,hasMore:y.length===k}}const g=[],_=1e3,v=e.from;let E=0,d=!1,x=0;for(let l=0;l<50;l++){const w=s.range(x,x+_-1),{data:y,error:r}=await w;if(r)throw new Error(r.message);const h=y??[];for(const b of h){const A=C(b);if(!(u&&A.vendor_sku&&u.has(A.vendor_sku))&&!(a&&!a.has(A.id))){if(E<v){E++;continue}if(g.length<k)g.push(A);else{d=!0;break}}}if(d)break;if(h.length<_){d=!1;break}x+=_}return{rows:g,hasMore:d}}async function qe(e){const n=new Map;if(e.length===0)return n;const t=200,i="id,extra,characteristics,documents";for(let o=0;o<e.length;o+=t){const s=e.slice(o,o+t),{data:a,error:c}=await m.from("products").select(i).in("id",s);if(c)throw new Error(c.message);for(const f of a??[]){const u=Array.isArray(f.extra)?f.extra:[];n.set(f.id,{extra:u,characteristics:Array.isArray(f.characteristics)?f.characteristics:[],documents:Array.isArray(f.documents)?f.documents:[],publish_error:u.find(p=>p?.key==="_publishError")?.value||void 0})}}return n}async function Ie(e){const{data:n}=await m.auth.getUser(),t=n.user?.id;let i=m.from("products").select("*",{count:"exact",head:!0});t&&(i=i.eq("user_id",t));let o=null,s=e.restrictProductIds;if(e.folderId&&t){const p=await G(t,e.folderId);if(p.length===0)return 0;if(s&&s.length>0){const k=new Set(p);if(s=s.filter(g=>k.has(g)),s.length===0)return 0}else s=p}const a=K(e);let c=null;if(a.length>0&&t){const p=await T(t,a),k=ee(i,p);i=k.q,c=k.excludedSet}if(s!=null){if(s.length===0)return 0;s.length<=ie?i=i.in("id",s):o=new Set(s)}if(i=U(i,{...e}),o&&s&&s.length<=ce&&t)return me(t,s,e,c);if(c||o){let k=0;for(let g=0;;g+=1e3){let _=m.from("products").select("id, vendor_sku").eq("user_id",t);_=U(_,{...e});const{data:v,error:E}=await _.range(g,g+1e3-1);if(E)throw new Error(E.message);const d=v??[];for(const x of d)c&&x.vendor_sku&&c.has(x.vendor_sku)||o&&!o.has(x.id)||k++;if(d.length<1e3)break}return k}const{count:f,error:u}=await i;if(u)throw new Error(u.message);return f??0}async function Pe(e){const{data:n}=await m.auth.getUser(),t=n.user?.id;if(!t)return[];const i=1e3,o=[];for(let s=0;;s+=i){let a=m.from("products").select("vendor_sku").eq("user_id",t);a=U(a,{...e}),a=a.range(s,s+i-1);const{data:c,error:f}=await a;if(f)throw new Error(f.message);const u=c??[];for(const p of u)o.push(p.vendor_sku??null);if(u.length<i)break}return o}async function Ce(e){const n=Array.from(new Set(e.filter(f=>typeof f=="string"&&f.length>0)));if(n.length===0)return[];try{const{data:f,error:u}=await m.rpc("resolve_product_ids_by_norm_vendor_sku",{_norms:n});if(!u&&Array.isArray(f))return f.map(p=>typeof p=="string"?p:p?.id).filter(p=>typeof p=="string")}catch{}const{data:t}=await m.auth.getUser(),i=t.user?.id;if(!i)return[];const o=f=>(f??"").trim().replace(/(_k_\d+|_vbb)$/i,"").toLocaleLowerCase("ru-RU"),s=new Set(n.map(o)),a=1e3,c=[];for(let f=0;;f+=a){const{data:u,error:p}=await m.from("products").select("id, vendor_sku").eq("user_id",i).not("vendor_sku","is",null).range(f,f+a-1);if(p)throw new Error(p.message);const k=u??[];for(const g of k){const _=o(g.vendor_sku);_&&s.has(_)&&c.push(g.id)}if(k.length<a)break}return c}async function Re(){const{data:e}=await m.auth.getUser(),n=e.user?.id;if(!n)return[];const{data:t,error:i}=await m.rpc("product_extra_keys",{_user_id:n});return i?(console.warn("product_extra_keys rpc failed",i.message),[]):(t??[]).map(o=>typeof o=="string"?o:o?.key).filter(o=>typeof o=="string"&&o.length>0)}async function Ne(){const{data:e}=await m.auth.getUser();if(!e.user?.id)return[];const{data:n,error:t}=await m.rpc("list_my_distinct_sources");if(t)return console.warn("listDistinctSources failed",t.message),[];const i=new Set;for(const o of n??[])o.source&&o.source.trim()&&i.add(o.source);return Array.from(i).sort((o,s)=>o.localeCompare(s,"ru"))}async function Ue(e){const{data:n}=await m.auth.getUser(),t=n.user?.id;if(!t)return[];const{data:i,error:o}=await m.rpc("product_vendor_sku_group",{_user_id:t,_vendor_sku:e}).limit(1e6);if(o)throw new Error(o.message);return(i??[]).map(C)}async function ue(e,n=500){if(!e.length)return 0;const{data:t,error:i}=await m.auth.getUser();if(i||!t.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const o=t.user.id;let s=0;for(let a=0;a<e.length;a+=n){const c=e.slice(a,a+n),f=c.map(k=>oe({...F(k),user_id:o})),{error:u,count:p}=await m.from("products").insert(f,{count:"exact"});if(u)throw new Error(u.message);s+=p??c.length}return s}async function Me(e){const{data:n,error:t}=await m.from("products").select("*").eq("id",e).maybeSingle();if(t)throw new Error(t.message);return n?C(n):void 0}async function Oe(e){const{data:n,error:t}=await m.auth.getUser();if(t||!n.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const i=oe({...F(e),user_id:n.user.id}),{data:o,error:s}=await m.from("products").insert(i).select("*").single();if(s)throw new Error(s.message);return C(o)}async function Be(e,n){const{data:t,error:i}=await m.from("products").update(F(n)).eq("id",e).select("*").single();if(i)throw new Error(i.message);const o=C(t);
1if(Object.prototype.hasOwnProperty.call(n,"vendor_sku")&&o.vendor_sku)try{await be(o.vendor_sku)}catch(s){console.warn("auto-merge after vendor_sku change failed",s)}return o}async function _e(e){if(e.length===0)return;const{error:n}=await m.from("products").delete().in("id",e);if(n)throw new Error(n.message)}async function $e(e){const{data:n}=await m.auth.getUser(),t=n.user?.id;if(!t)throw new Error("Ðе авÑоÑизован");let i=m.from("products").delete({count:"exact"}).eq("user_id",t);if(e.folderId){const f=await G(t,e.folderId);if(f.length===0)return 0;i=i.in("id",f)}const o=K(e);let s=null;if(o.length>0){const f=await T(t,o),u=ee(i,f);i=u.q,s=u.excludedSet}if(s){const f=await ge(e);return f.length===0?0:(await _e(f),f.length)}i=U(i,{...e});const{error:a,count:c}=await i;if(a)throw new Error(a.message);return c??0}async function ge(e){const{data:n}=await m.auth.getUser(),t=n.user?.id;if(!t)return[];let i=null;if(e.folderId&&(i=await G(t,e.folderId),i.length===0))return[];const o=K(e);let s=[];o.length>0&&(s=await T(t,o));const a=s.length?ae(s):"",c=a.length>ne,f=c?new Set(s):null,u=1e3,p=[];for(let k=0;;k+=u){let g=m.from("products").select("id, vendor_sku").eq("user_id",t);i&&(g=g.in("id",i)),s.length>0&&!c&&(g=g.not("vendor_sku","in",a)),g=U(g,{...e}),g=g.order("updated_at",{ascending:!1,nullsFirst:!1}).order("created_at",{ascending:!1}),g=g.range(k,k+u-1);const{data:_,error:v}=await g;if(v)throw new Error(v.message);const E=_??[];for(const d of E)f&&d.vendor_sku&&f.has(d.vendor_sku)||p.push(d.id);if(E.length<u)break}return p}const L="vababay";async function je(e,n,t={}){if(e.length===0||n.length===0)return{created:0,updated:0};const{data:i,error:o}=await m.auth.getUser();if(o||!i.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const s=i.user.id,a=n[0];if(a===L){const{data:p,error:k}=await m.from("products").select("*").eq("user_id",s).in("id",e);if(k)throw new Error(k.message);let g=0,_=0;const v=[];for(const E of p??[]){let d=E;if(d.source===L){const I=(Array.isArray(d.extra)?d.extra:[]).find(Q=>Q&&typeof Q=="object"&&Q.key==="_publishedFrom"),O=I?String(I.value??""):"";if(!O){_+=1;continue}const{data:N}=await m.from("products").select("*").eq("user_id",s).eq("id",O).limit(1),re=(N??[])[0];if(!re){_+=1;continue}d=re}const x=String(d.id),l=d.vendor_sku??null,w=l?`${l}_vbb`:null;let y=null,r=null;if(l){const{data:S}=await m.from("products").select("id, price, cost, source").eq("user_id",s).eq("vendor_sku",l);for(const I of S??[])I.source!==L&&(y==null&&I.cost!=null&&(y=Number(I.cost)),r==null&&I.price!=null&&(r=Number(I.price)))}const{data:h}=await m.from("products").select("id, extra").eq("user_id",s).eq("source",L);let b=null;for(const S of h??[]){const O=(Array.isArray(S.extra)?S.extra:[]).find(N=>N&&typeof N=="object"&&N.key==="_publishedFrom");if(O&&String(O.value??"")===x){b=S.id;break}}const q=(Array.isArray(d.extra)?[...d.extra]:[]).filter(S=>!(S&&typeof S=="object"&&(S.key==="_publishedFrom"||S.key==="_publishError")));q.push({key:"_publishedFrom",value:x});const{data:P}=await m.from("product_funnel_photos").select("funnel_id, slide_id, storage_path, caption, sort_order").eq("user_id",s).eq("product_id",x).order("sort_order",{ascending:!0}).order("created_at",{ascending:!0}),R=[],z=3600*24*365*10;for(const S of P??[])try{const{data:I}=await m.storage.from("funnel-media").createSignedUrl(S.storage_path,z);I?.signedUrl&&R.push(I.signedUrl)}catch{}const te=Array.isArray(d.photos)?d.photos.filter(S=>typeof S=="string"):[],$=new Set,J=[...R,...te].filter(S=>$.has(S)?!1:($.add(S),!0)),j={user_id:s,name:d.name,brand:d.brand,barcode:d.barcode,promocode:d.promocode,description:d.description,category_id:d.category_id,category:d.category,subject:d.subject,price:(()=>{const S=d.cost!=null?Number(d.cost):y??NaN;if(Number.isFinite(S)&&S>0)return Math.round(S*1.15*100)/100;const I=d.price!=null?Number(d.price):r??NaN;return Number.isFinite(I)?I:null})(),cost:d.cost??y,stock:d.stock,length_cm:d.length_cm,width_cm:d.width_cm,height_cm:d.height_cm,weight_kg:d.weight_kg,tnved:d.tnved,photos:J,characteristics:d.characteristics??[],documents:d.documents??[],extra:q,source:L,published:!0};let B=b;if(b){const{error:S}=await m.from("products").update(j).eq("id",b);if(S)throw new Error(S.message);_+=1}else{const S={...j,vendor_sku:w,wb_sku:null},{data:I,error:O}=await m.from("products").insert(S).select("*").limit(1);if(O)throw new Error(O.message);g+=1;const N=(I??[])[0];N&&(v.push(C(N)),B=String(N.id))}if(B&&t.warehouseId){const S=Math.max(0,Math.floor(t.quantity??1));try{await m.from("warehouse_stocks").upsert({user_id:s,warehouse_id:t.warehouseId,product_id:B,quantity:S},{onConflict:"warehouse_id,product_id"})}catch{}}if(B){await m.rpc("assign_public_article",{_id:B});try{await m.from("product_funnel_photos").delete().eq("user_id",s).eq("product_id",B);const S=(P??[]).map(I=>({user_id:s,product_id:B,funnel_id:I.funnel_id,slide_id:I.slide_id,storage_path:I.storage_path,caption:I.caption,sort_order:I.sort_order}));S.length>0&&await m.from("product_funnel_photos").insert(S)}catch{}}}return{created:g,updated:_,createdProducts:v}}let c=0;const{data:f,error:u}=await m.from("products").select("id, source").eq("user_id",s).in("id",e);if(u)throw new Error(u.message);for(const p of f??[]){const k={published:!0};(!p.source||!p.source.trim())&&(k.source=a);const{error:g}=await m.from("products").update(k).eq("id",p.id);if(g)throw new Error(g.message);c+=1}return{created:0,updated:c}}async function Le(e,n,t,i=1,o){if(e.length===0)return{created:0,updated:0};const s=o?.keepVendorSku===!0,a=o?.inheritSource??s,{data:c,error:f}=await m.auth.getUser();if(f||!c.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const u=c.user.id,{data:p,error:k}=await m.from("products").select("*").in("id",e);if(k)throw new Error(k.message);const g=p??[];let _=0;const v=[],E=new Map;async function d(l){if(!l)return null;const w=l.replace(/_k_\d+$/u,"");let y=E.get(w);if(!y){y=new Set;const h=w.replace(/([\\%_])/g,"\\$1"),{data:b}=await m.from("products").select("vendor_sku").eq("user_id",u).like("vendor_sku",`${h}\\_k\\_%`),A=new RegExp(`^${w.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}_k_(\\d+)$`);for(const q of b??[]){const P=q.vendor_sku;if(!P)continue;const R=A.exec(P);R&&y.add(Number(R[1]))}E.set(w,y)}let r=1;for(;y.has(r);)r+=1;return y.add(r),`${w}_k_${r}`}const x=Math.max(1,Math.min(50,Math.floor(i)||1));for(const l of g){String(l.id);const w=l.vendor_sku??null,r=(Array.isArray(l.extra)?[...l.extra]:[]).filter(h=>!(h&&typeof h=="object"&&(h.key==="_publishedFrom"||h.key==="_publishError")));for(let h=0;h<x;h++){const b=s?w:await d(w),A={user_id:u,name:l.name,brand:l.brand,vendor_sku:b,barcode:l.barcode,promocode:l.promocode,description:l.description,category_id:l.category_id,category:l.category,subject:l.subject,price:l.price,cost:l.cost,stock:l.stock,length_cm:l.length_cm,width_cm:l.width_cm,height_cm:l.height_cm,weight_kg:l.weight_kg,tnved:l.tnved,photos:l.photos??[],characteristics:l.characteristics??[],documents:l.documents??[],extra:r,source:a?l.source??null:null,published:!1,wb_sku:null},{data:q,error:P}=await m.from("products").insert(A).select("*").limit(1);if(P)throw new Error(P.message);_+=1;const R=(q??[])[0];if(!s&&R&&b){const{data:z}=await m.from("products").select("id, created_at").eq("user_id",u).eq("vendor_sku",b);if((z??[]).length>1){E.delete(b.replace(/_k_\d+$/u,""));const $=await d(w);if($&&$!==b){const{data:J}=await m.from("products").update({vendor_sku:$}).eq("id",String(R.id)).select("*").limit(1),j=(J??[])[0];if(j){v.push(C(j));continue}}}}R&&v.push(C(R))}}return{created:_,updated:0,createdProducts:v}}const ke=["name","brand","wb_sku","barcode","promocode","description","category_id","category","subject","price","cost","stock","length_cm","width_cm","height_cm","weight_kg","tnved","source"];function Y(e){return e==null?!1:typeof e=="string"?e.trim()!=="":!0}function se(e,n){if(!n.length)return e;const t=new Map;for(const i of e)t.set(i.key,i);for(const i of n){const o=t.get(i.key);t.set(i.key,o?{...o,value:i.value,meta:i.meta??o.meta}:i)}return Array.from(t.values())}function M(e,n,t,i){const o=new Map;for(const a of e)o.set(a.key,a);
1for(const a of n)a.key==="_publishedOn"||a.key==="_platforms"||o.has(a.key)||o.set(a.key,a);const s=(i??"").trim();if(s){const a=o.get("_publishedOn");let c=[];if(a)try{const u=typeof a.value=="string"?JSON.parse(a.value):a.value;Array.isArray(u)&&(c=u.filter(p=>typeof p=="string"))}catch{}const f=u=>u.trim().toLowerCase();c.some(u=>f(u)===f(s))||c.push(s),o.set("_publishedOn",{id:"_publishedOn",key:"_publishedOn",value:JSON.stringify(c)})}{const a=o.get("_platforms");let c=[];if(a)try{const f=typeof a.value=="string"?JSON.parse(a.value):a.value;Array.isArray(f)&&(c=f.filter(u=>typeof u=="string"))}catch{}c.includes(t)||c.push(t),o.set("_platforms",{id:"_platforms",key:"_platforms",value:JSON.stringify(c)})}return Array.from(o.values())}async function ye(e){const n=Array.from(new Set(e.filter(u=>Number.isFinite(u)&&u>0))),t=new Map;if(!n.length)return t;const{data:i,error:o}=await m.from("wb_categories").select("id,parent_id").in("id",n);if(o)return t;const s=Array.from(new Set((i??[]).map(u=>u.parent_id).filter(u=>typeof u=="number")));if(!s.length)return t;const{data:a,error:c}=await m.from("wb_categories").select("id,name").in("id",s);if(c)return t;const f=new Map((a??[]).map(u=>[u.id,u.name]));for(const u of i??[]){if(typeof u.parent_id!="number")continue;const p=f.get(u.parent_id);p&&t.set(u.id,p)}return t}function H(e,n){if(!n.length)return e;const t=new Set(e),i=[...e];for(const o of n){const s=o.trim();s&&!t.has(s)&&(t.add(s),i.push(s))}return i}async function Fe(e,n){if(!e.length)return{updated:0,created:0,skipped:0};const{data:t,error:i}=await m.auth.getUser();if(i||!t.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const o=t.user.id,s=[];let a=0;for(const d of e)d.vendor_sku&&d.vendor_sku.trim()?s.push(d):a+=1;if(!s.length)return{updated:0,created:0,skipped:a};const c=Array.from(new Set(s.map(d=>d.vendor_sku.trim()))),f=new Map,u=500;for(let d=0;d<c.length;d+=u){const x=c.slice(d,d+u),{data:l,error:w}=await m.from("products").select("*").eq("user_id",o).in("vendor_sku",x);if(w)throw new Error(w.message);for(const y of l??[]){const r=C(y);r.vendor_sku&&f.set(r.vendor_sku.trim(),r)}}const p=[],k=[];for(const d of s){const x=d.vendor_sku.trim(),l=f.get(x);if(!l){const y=d.name&&d.name.trim()?d.name:x;p.push({...d,name:y});continue}const w={};if(n?.cabinetSoft){if(d.extra&&d.extra.length?w.extra=M(l.extra,d.extra,n.cabinetSoft.platform,n.cabinetSoft.sourceSlug):w.extra=M(l.extra,[],n.cabinetSoft.platform,n.cabinetSoft.sourceSlug),d.characteristics&&d.characteristics.length){const y=new Map(l.characteristics.map(r=>[r.key,r]));for(const r of d.characteristics)y.has(r.key)||y.set(r.key,r);w.characteristics=Array.from(y.values())}}else{for(const y of ke){const r=d[y];Y(r)&&(w[y]=r)}d.photos&&d.photos.length&&(w.photos=H(l.photos,d.photos)),d.extra&&d.extra.length&&(w.extra=se(l.extra,d.extra)),d.characteristics&&d.characteristics.length&&(w.characteristics=se(l.characteristics,d.characteristics)),d.documents&&d.documents.length&&(w.documents=d.documents)}Object.keys(w).length>0&&k.push({id:l.id,existing:l,requiredName:l.name||x,patch:w})}let g=0;const _=500,v=new Date().toISOString();for(let d=0;d<k.length;d+=_){const x=k.slice(d,d+_),l=x.map(({id:y,existing:r,requiredName:h,patch:b})=>({id:y,user_id:o,name:h,photos:r.photos,characteristics:r.characteristics,documents:r.documents,extra:r.extra,...F(b),updated_at:v})),{error:w}=await m.from("products").upsert(l,{onConflict:"id"});if(w)throw new Error(w.message);g+=x.length}let E=0;return p.length&&(E=await ue(p)),{updated:g,created:E,skipped:a}}async function Ve(e){if(!e.length)return{updated:0,created:0,skipped:0};const{data:n,error:t}=await m.auth.getUser();if(t||!n.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const i=n.user.id,o=[];let s=0;for(const l of e)l.wb_sku&&String(l.wb_sku).trim()?o.push(l):s+=1;if(!o.length)return{updated:0,created:0,skipped:s};const a=Array.from(new Set(o.map(l=>String(l.wb_sku).trim()))),c=new Map,f=500;for(let l=0;l<a.length;l+=f){const w=a.slice(l,l+f),{data:y,error:r}=await m.from("products").select("*").eq("user_id",i).in("wb_sku",w);if(r)throw new Error(r.message);for(const h of y??[]){const b=C(h);b.wb_sku&&c.set(String(b.wb_sku).trim(),b)}}const u=Array.from(new Set(o.filter(l=>!c.has(String(l.wb_sku).trim())).map(l=>(l.vendor_sku??"").trim()).filter(l=>l.length>0))),p=new Map;for(let l=0;l<u.length;l+=f){const w=u.slice(l,l+f),{data:y,error:r}=await m.from("products").select("*").eq("user_id",i).in("vendor_sku",w);if(r)throw new Error(r.message);for(const h of y??[]){const b=C(h);b.vendor_sku&&p.set(b.vendor_sku.trim(),b)}}const k=[],g=[],_=await ye(o.filter(l=>!Y(l.category)&&typeof l.category_id=="number").map(l=>l.category_id));for(const l of o){const w={...l};if(!Y(w.category)&&typeof w.category_id=="number"){const q=_.get(w.category_id);q&&(w.category=q)}const y=String(w.wb_sku).trim();let r=c.get(y),h=!1;if(!r){const q=(w.vendor_sku??"").trim();if(q){const P=p.get(q);P&&(r=P,h=!0)}}if(!r){const q=w.name&&w.name.trim()?w.name:`WB ${y}`;k.push({...w,name:q});continue}const b=(w.source??"").trim()||"Wildberries · кабинеÑ",A={};
1if(h&&!(r.wb_sku&&String(r.wb_sku).trim())&&(A.wb_sku=y),A.extra=M(r.extra,w.extra??[],"wb",b),w.characteristics&&w.characteristics.length){const q=new Map(r.characteristics.map(P=>[P.key,P]));for(const P of w.characteristics)q.has(P.key)||q.set(P.key,P);A.characteristics=Array.from(q.values())}Object.keys(A).length>0&&g.push({id:r.id,existing:r,requiredName:r.name||y,patch:A})}let v=0;const E=500,d=new Date().toISOString();for(let l=0;l<g.length;l+=E){const w=g.slice(l,l+E),y=w.map(({id:h,existing:b,requiredName:A,patch:q})=>({id:h,user_id:i,name:A,photos:b.photos,characteristics:b.characteristics,documents:b.documents,extra:b.extra,...F(q),updated_at:d})),{error:r}=await m.from("products").upsert(y,{onConflict:"id"});if(r)throw new Error(r.message);v+=w.length}let x=0;return k.length&&(x=await ue(k)),{updated:v,created:x,skipped:s}}async function De(){const{data:e,error:n}=await m.auth.getUser();if(n||!e.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const t=e.user.id,{data:i,error:o}=await m.from("products").select("*").eq("user_id",t).not("vendor_sku","is",null);if(o)throw new Error(o.message);const s=_=>_.trim().toLowerCase().replace(/(_k_\d+|_vbb)$/i,""),a=new Map;for(const _ of i??[]){const v=C(_),E=(v.vendor_sku??"").trim();if(!E)continue;const d=s(E);if(!d)continue;const x=a.get(d);x?x.push(v):a.set(d,[v])}const c=_=>{const v=(_??"").trim().toLowerCase();if(!v)return null;const E=v.split("·")[0].trim();return E.startsWith("wildberries")||E==="wb"?"wb":E.startsWith("ozon")?"ozon":null},f=_=>!!(_&&_.includes("·")),u=_=>{let v=0;return f(_.source)||(v+=100),typeof _.cost=="number"&&(v+=10),typeof _.stock=="number"&&_.stock>0&&(v+=8),typeof _.price=="number"&&_.price>0&&(v+=4),_.photos?.length&&(v+=2),_.name&&_.name.trim()&&_.name.trim()!==(_.vendor_sku??"").trim()&&(v+=1),v};let p=0,k=0,g=0;for(const[,_]of a){if(_.length<2)continue;p+=1,_.sort((h,b)=>u(b)-u(h));const v=_[0],E=_.slice(1);let d=v.extra;const x=new Map(v.characteristics.map(h=>[h.key,h]));let l=v.photos;for(const h of E){const b=c(h.source)??c(v.source),A=(h.source??"").trim();b?d=M(d,h.extra,b,A):A&&(d=M(d,h.extra,"wb",A).filter(q=>q.key!=="_platforms"||(()=>{try{const P=typeof q.value=="string"?JSON.parse(q.value):q.value;return Array.isArray(P)&&P.length>0}catch{return!0}})()));for(const q of h.characteristics)x.has(q.key)||x.set(q.key,q);h.photos?.length&&(l=H(l,h.photos))}const w={extra:d,characteristics:Array.from(x.values()),photos:l};if(!v.wb_sku){const h=E.find(b=>b.wb_sku&&b.wb_sku.trim());h&&(w.wb_sku=h.wb_sku)}const{error:y}=await m.from("products").update(w).eq("id",v.id);if(y)throw new Error(y.message);k+=1;const r=E.map(h=>h.id);if(r.length){const{error:h}=await m.from("products").delete().in("id",r);if(h)throw new Error(h.message);g+=r.length}}return{groups:p,merged:k,deleted:g}}async function be(e){const n=(e??"").trim();if(!n)return{merged:!1,deleted:0};const{data:t,error:i}=await m.auth.getUser();if(i||!t.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const o=t.user.id,s=r=>r.trim().toLowerCase().replace(/(_k_\d+|_vbb)$/i,""),a=s(n);if(!a)return{merged:!1,deleted:0};const{data:c,error:f}=await m.from("products").select("*").eq("user_id",o).not("vendor_sku","is",null);if(f)throw new Error(f.message);const u=(c??[]).map(C).filter(r=>s(r.vendor_sku??"")===a);if(u.length<2)return{merged:!1,deleted:0};const p=r=>{const h=(r??"").trim().toLowerCase();if(!h)return null;const b=h.split("·")[0].trim();return b.startsWith("wildberries")||b==="wb"?"wb":b.startsWith("ozon")?"ozon":null},k=r=>!!(r&&r.includes("·")),g=r=>{let h=0;return k(r.source)||(h+=100),typeof r.cost=="number"&&(h+=10),typeof r.stock=="number"&&r.stock>0&&(h+=8),typeof r.price=="number"&&r.price>0&&(h+=4),r.photos?.length&&(h+=2),r.name&&r.name.trim()&&r.name.trim()!==(r.vendor_sku??"").trim()&&(h+=1),h};u.sort((r,h)=>g(h)-g(r));const _=u[0],v=u.slice(1);let E=_.extra;const d=new Map(_.characteristics.map(r=>[r.key,r]));let x=_.photos;for(const r of v){const h=p(r.source)??p(_.source),b=(r.source??"").trim();h?E=M(E,r.extra,h,b):b&&(E=M(E,r.extra,"wb",b));for(const A of r.characteristics)d.has(A.key)||d.set(A.key,A);
1r.photos?.length&&(x=H(x,r.photos))}const l={extra:E,characteristics:Array.from(d.values()),photos:x};if(!_.wb_sku){const r=v.find(h=>h.wb_sku&&h.wb_sku.trim());r&&(l.wb_sku=r.wb_sku)}const{error:w}=await m.from("products").update(l).eq("id",_.id);if(w)throw new Error(w.message);const y=v.map(r=>r.id);if(y.length){const{error:r}=await m.from("products").delete().in("id",y);if(r)throw new Error(r.message)}return{merged:!0,deleted:y.length}}async function We(e){const n=Array.from(new Set((e??[]).filter(Boolean)));if(n.length<2)throw new Error("ÐÑжно вÑбÑаÑÑ Ð¼Ð¸Ð½Ð¸Ð¼Ñм 2 ÑоваÑа");const{data:t,error:i}=await m.auth.getUser();if(i||!t.user)throw new Error("ÐÑжна авÑоÑизаÑиÑ");const o=t.user.id,{data:s,error:a}=await m.from("products").select("*").eq("user_id",o).in("id",n);if(a)throw new Error(a.message);const c=(s??[]).map(C);if(c.length<2)throw new Error("Ðе ÑдалоÑÑ Ð·Ð°Ð³ÑÑзиÑÑ Ð²ÑбÑаннÑе ÑоваÑÑ");const f=r=>{const h=(r??"").trim().toLowerCase();if(!h)return null;const b=h.split("·")[0].trim();return b.startsWith("wildberries")||b==="wb"?"wb":b.startsWith("ozon")?"ozon":null},u=r=>!!(r&&r.includes("·")),p=r=>{const h=(r??"").trim().toLowerCase();return h.startsWith("excel")||h.includes("csv")||h.includes("1c")||h.includes("1Ñ")},k=r=>{let h=0;return p(r.source)&&(h+=1e3),u(r.source)||(h+=100),typeof r.cost=="number"&&(h+=10),typeof r.stock=="number"&&r.stock>0&&(h+=8),typeof r.price=="number"&&r.price>0&&(h+=4),r.photos?.length&&(h+=2),r.name&&r.name.trim()&&r.name.trim()!==(r.vendor_sku??"").trim()&&(h+=1),h};c.sort((r,h)=>k(h)-k(r));const g=c[0],_=c.slice(1);let v=g.extra;const E=new Map(g.characteristics.map(r=>[r.key,r]));let d=g.photos;for(const r of _){const h=f(r.source)??f(g.source),b=(r.source??"").trim();h?v=M(v,r.extra,h,b):b&&(v=M(v,r.extra,"wb",b));for(const A of r.characteristics)E.has(A.key)||E.set(A.key,A);r.photos?.length&&(d=H(d,r.photos))}const x={extra:v,characteristics:Array.from(E.values()),photos:d};if(!g.wb_sku){const r=_.find(h=>h.wb_sku&&h.wb_sku.trim());r&&(x.wb_sku=r.wb_sku)}if(typeof g.cost!="number"){const r=_.find(h=>typeof h.cost=="number");r&&(x.cost=r.cost)}if(!g.stock||g.stock===0){const r=_.find(h=>typeof h.stock=="number"&&h.stock>0);r&&(x.stock=r.stock)}const{error:l}=await m.from("products").update(x).eq("id",g.id);if(l)throw new Error(l.message);const w=_.map(r=>r.id),{error:y}=await m.from("products").delete().in("id",w);if(y)throw new Error(y.message);return{primaryId:g.id,deleted:w.length}}export{L as V,Ee as a,Re as b,Le as c,Ne as d,Se as e,qe as f,Ie as g,Pe as h,xe as i,ge as j,Ce as k,Ae as l,Ue as m,We as n,$e as o,je as p,_e as q,De as r,Be as s,Me as t,Fe as u,Ve as v,Oe as w,de as x,ue as y};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.