1import{j as x,ah as P,ai as M,aj as A,J as F,I as L,ak as U,C as E,al as H,E as u,am as d,D as b,an as I,ao as j,G as B,M as z}from"#entry";const S=/\.r2\.cloudflarestorage\.com$/i;function W(t){try{return S.test(new URL(t).hostname)}catch{return!1}}function _(t,r=""){const n=t.trim();if(!n)return null;if(n.startsWith("/api/r2/"))return decodeURIComponent(n.replace(/^\/api\/r2\//,""));if(!/^https?:\/\//i.test(n)){const s=n.replace(/^\/+/,"");return s.startsWith("repuestos/")?s:null}try{const s=new URL(n);if(S.test(s.hostname)){const l=s.pathname.replace(/^\/+/,"");return l.startsWith("repuestos/")?l:l||null}const i=r.replace(/\/+$/,"");if(i){const l=new URL(i);if(s.origin===l.origin)return s.pathname.replace(/^\/+/,"")||null}}catch{return null}return null}function $(t,r){if(!t?.trim())return"";const n=t.trim(),s=(r||"").trim().replace(/\/+$/,""),i=_(n,s);return i?s&&!W(s)?`${s}/${i}`:`/api/r2/${i.split("/").map(encodeURIComponent).join("/")}`:n}function N(t,r){return{...t,imagen:t.imagen?$(t.imagen,r):t.imagen,imagenes:t.imagenes?.map(n=>$(n,r)).filter(Boolean)}}function f(t){if(t==null||t==="")return null;const r=parseInt(String(t),10);return Number.isFinite(r)&&r>=1900&&r<=2100?r:null}function T(t){const r=f(t.anioDesde??t.anio),n=f(t.anioHasta??t.anio);return r==null||n==null?!1:n>r}function G(t){const r=f(t.anioDesde??t.anio),n=f(t.anioHasta??t.anio);return r==null?"":n==null||n===r?String(r):`${r} â ${n}`}function oe(t){const r=G(t);return r?T(t)?`Años ${r}`:`Año ${r}`:""}function J(t,r){const n=f(t.anioDesde??t.anio),s=f(t.anioHasta??t.anio);if(n==null)return!1;const i=s??n;return r>=n&&r<=i}function ae(t){if(t.usarRango){const n=f(t.anioDesde),s=f(t.anioHasta);if(n==null||s==null)throw new Error("Indica año desde y año hasta del rango");const i=Math.min(n,s),l=Math.max(n,s);return{anio:i,anioDesde:i,anioHasta:l}}const r=f(t.anio??t.anioDesde);if(r==null)throw new Error("Indica el año del repuesto");return{anio:r}}const re=()=>{const{$firestore:t}=x(),r=P(),n=String(r.public.r2PublicBaseUrl||"").trim(),s=(e,o)=>N({id:e,...o,createdAt:o.createdAt?.toDate?.()||new Date,updatedAt:o.updatedAt?.toDate?.()||new Date},n),i=()=>!!t,l=async(e={})=>{if(!i())return[];try{const o=E(t,"repuestos");let a=u(o,d("activo","==",!0));if(e.marca){const c=e.marca.toLowerCase();a=u(a,d("marca","==",c))}const g=e.modelo,p=e.anio,R=e.categoria?L(e.categoria):null;if(e.subcategoria){const c=e.subcategoria.toLowerCase();a=u(a,d("subcategoria","==",c))}e.stock!==void 0&&(a=u(a,d("stock","==",e.stock))),e.destacado!==void 0&&(a=u(a,d("destacado","==",e.destacado))),e.precioMin&&(a=u(a,d("precio",">=",e.precioMin))),e.precioMax&&(a=u(a,d("precio","<=",e.precioMax)));const y=e.ordenarPor||"relevancia",w=e.orden||"desc";y==="precio"?a=u(a,B("precio",w)):y==="nombre"?a=u(a,B("nombre",w)):y==="fecha"&&(a=u(a,B("createdAt",w)));const k=e.limite||(e.marca&&e.modelo&&e.anio?200:20);a=u(a,I(k));const q=await b(a),C=[];q.forEach(c=>{C.push(s(c.id,c.data()))});let m=C;if(e.modelo){const c=e.modelo.toLowerCase();m=C.filter(h=>(h.modelo||"").toLowerCase()===c)}if(e.marca&&m.length>0){const c=e.marca.toLowerCase();m=m.filter(h=>(h.marca||"").toLowerCase()===c)}if(e.categoria&&R&&(m=m.filter(c=>z(c.categoria||"",R))),p!=null&&(m=m.filter(c=>J(c,p))),e.busqueda){const c=e.busqueda.toLowerCase();return m.filter(h=>h.nombre.toLowerCase().includes(c)||h.descripcion.toLowerCase().includes(c)||h.searchTerms.some(D=>D.toLowerCase().includes(c)))}return m}catch(o){return console.error("Error al obtener repuestos:",o),[]}};return{getRepuestos:l,getRepuestoById:async e=>{if(!i())return null;try{const o=A(t,"repuestos",e),a=await j(o);return a.exists()?s(a.id,a.data()):null}catch(o){return console.error("Error al obtener repuesto:",o),null}},getRepuestoBySlug:async e=>{if(!i())return null;try{const o=E(t,"repuestos"),a=e.toLowerCase();let g=u(o,d("slug","==",e),d("activo","==",!0)),p=await b(g);if(p.empty&&e!==a&&(g=u(o,d("slug","==",a),d("activo","==",!0)),p=await b(g)),p.empty){g=u(o,d("activo","==",!0),I(100)),p=await b(g);const R=[];p.forEach(w=>{R.push(s(w.id,w.data()))});const y=R.find(w=>(w.slug||"").toLowerCase()===a);if(y)return y}if(!p.empty){const R=p.docs[0];return s(R.id,R.data())}return null}catch(o){return console.error("Error al obtener repuesto por slug:",o),null}},crearRepuesto:async e=>{try{const o=E(t,"repuestos"),a=new Date,g={...e,categoria:e.categoria?L(e.categoria):e.categoria,subcategoria:e.subcategoria?F(e.subcategoria):e.subcategoria,createdAt:a,updatedAt:a};return(await H(o,g)).id}catch(o){throw console.error("Error al crear repuesto:",o),o}},actualizarRepuesto:async(e,o)=>{try{const a=A(t,"repuestos",e),g={...o,...o.categoria?{categoria:L(o.categoria)}:{},...o.subcategoria?{subcategoria:F(o.subcategoria)}:{},updatedAt:new Date};await U(a,g)}catch(a){throw console.error("Error al actualizar repuesto:",a),a}},eliminarRepuesto:async e=>{if(!i())throw new Error("Firestore no disponible");try{await M(A(t,"repuestos",e))}catch(o){throw console.error("Error al eliminar repuesto:",o),o}},buscarRepuestos:async(e,o={})=>await l({...o,busqueda:e}),getRepuestosDestacados:async(e=10)=>
1await l({destacado:!0,limite:e}),getRepuestosByCategoria:async(e,o=20)=>await l({categoria:e,limite:o}),getRepuestosRelacionados:async(e,o=5)=>await l({marca:e.marca,modelo:e.modelo,anio:e.anio,categoria:e.categoria,limite:o})}};export{ae as b,G as f,oe as l,J as r,T as t,re as u};
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.