import{d as y,a as b}from"./dataSchema-VwIQYPJL.js";import"./locale-wDcg0qDg.js";import{h as C}from"./httpComponent-AiGk8DOD.js";import{C as v,I as S,B as L,g as R}from"./manifest--hA2W8VB.js";const D=`The Crud Lookup Client resolves lookups from a given data schema onto a given source.
`,w=`Il Crud Lookup Client risolve le ricerche da un determinato schema di dati su una determinata sorgente dati.
`;function q(a,e,r){var i;const t=Object.values((i=a.properties)!=null?i:{}).find(n=>{var o;const s=n.lookupOptions;if(!s||"/".concat(s.lookupDataSource,"/")!==e)return!1;const c=[...(o=s.lookupFields)!=null?o:[],s.lookupValue];return r.sort().join(",")===c.sort().join(",")});return t==null?void 0:t.lookupOptions}function T(a,e){var r;const i=(r=e.$and)!=null?r:[];for(const t of i){const n=t[a],o=n==null?void 0:n.$in;if(o&&Array.isArray(o))return o}return[]}function g(a){return a.reduce((e,r)=>(e[r]=R(),e),{})}function U(a,e,r){var i,t,n;const{pathname:o,searchParams:s}=a,c=o.replace(e,""),k=((i=s.get("_p"))!=null?i:"").split(","),h=q(r,c,k),{lookupValue:l,lookupFields:u}=h??{};if(!l)throw new L("no data source");const _=JSON.parse((t=s.get("_q"))!=null?t:"{}"),p=T(l,_).map(d=>({...g(u??[]),[l]:d})),f=Number.parseInt((n=s.get("_l"))!=null?n:"25");if(!isNaN(f)){const d=[l,...u??[]];for(let m=0;m<f-p.length;m++)p.push(g(d))}return new Response(JSON.stringify(p),{headers:{"Cache-Control":"no-cache","Content-Type":"application/json"},status:200})}const x=({dataSchema:a,basePath:e=""})=>{const r=e.charAt(e.length-1)==="/"?e:"".concat(e,"/");return[{handler:i=>{let t;try{t=U(i,e,a??{type:"object",properties:{}})}catch(n){const o=n instanceof v?n:new S;t=new Response(JSON.stringify(o.getResponse()),{headers:{"Cache-Control":"no-cache","Content-Type":"application/json"},status:o.statusCode})}return Promise.resolve(t)},method:"GET",url:{pathname:r}}]},A={docLink:"https://docs.mia-platform.eu/docs/microfrontend-composer/back-kit/components/crud_lookup_client",description:{en:"Client to resolve references between CRUD collections",it:"Client per risolvere referenze tra collezioni CRUD"},descriptionLong:{en:D,it:w},example:JSON.stringify({tag:"bk-crud-lookup-client",properties:{basePath:"/v2",dataSchema:{type:"object",properties:{riderId:{type:"string",format:"lookup",lookupOptions:{lookupDataSource:"riders",lookupFields:["name","surname"],lookupValue:"_id"}},_id:{type:"string"},__STATE__:{type:"string"}}}}}),label:"CRUD lookup Client",mocks:{fetch:x},definitions:{...y,dataSchema:b},properties:{...C,basePath:{type:"string",__mia_configuration:{description:"The URL base path used to reach the CRUD service",label:"CRUD Service base path",attribute:!0,priority:"high","schema-hint":"mia/endpoints/data-source"}},headers:{type:"object",__mia_configuration:{attribute:!1,label:"Headers",description:"Headers to add when a fetch request is performed"},additionalProperties:{type:"string"}},dataSchema:{$ref:"#/definitions/dataSchema",__mia_configuration:{"shared-key":"back-kit/data-schema",attribute:!1,label:"Data schema",priority:"high"}},maxQueryURLLength:{type:"number",default:1500,__mia_configuration:{attribute:"max-query-url-length",label:"URL query maximum length",description:"External lookups might require a long query parameter. According with your base path trim it to a maximum using this prop"}},lookupDataLimit:{type:"number",default:25,__mia_configuration:{label:"Lookup data limit",description:"limit data to require in a single lookup chunk",attribute:"lookup-data-limit"}},lookupDefaultState:{oneOf:[{type:"string",enum:["PUBLIC","DRAFT","TRASH"]},{type:"array",items:{type:"string",enum:["PUBLIC","DRAFT","TRASH"]}}],default:["PUBLIC","DRAFT","TRASH"],__mia_configuration:{attribute:"lookup-default-state",label:"Default state",description:"Default states to append on lookup queries. Lookup queries will overwrite this setting when required by the data schema"}},allowLiveSearchCache:{type:"boolean",default:!1,__mia_configuration:{attribute:"allow-live-share-cache",label:"Live search cache",description:"Allows to cache results of live-search queries. Cache lasts as long as the component lives"}},extraLookupKeys:{$ref:"#/definitions/dataSchema",__mia_configuration:{"shared-key":"back-kit/data-schema",attribute:!1,label:"Data schema"}},urlMask:{type:"string",default:"",__mia_configuration:{attribute:"url-mask",label:"URL mask",description:"Url mask to apply to the current path to extract dynamic parameters"}}},type:"connector"};var N=A;export{N as default};
