import{d as y,a as b}from"./dataSchema-liIE6AFZ.js";import"./locale-2oqXkJWO.js";import{h as C}from"./httpComponent-AiGk8DOD.js";import{C as v,I as R,B as S,g as D}from"./manifest--hA2W8VB.js";function L(o,e,r){var i;const t=Object.values((i=o.properties)!=null?i:{}).find(n=>{var a;const s=n.lookupOptions;if(!s||"/".concat(s.lookupDataSource,"/")!==e)return!1;const c=[...(a=s.lookupFields)!=null?a:[],s.lookupValue];return r.sort().join(",")===c.sort().join(",")});return t==null?void 0:t.lookupOptions}function w(o,e){var r;const i=(r=e.$and)!=null?r:[];for(const t of i){const n=t[o],a=n==null?void 0:n.$in;if(a&&Array.isArray(a))return a}return[]}function g(o){return o.reduce((e,r)=>(e[r]=D(),e),{})}function q(o,e,r){var i,t,n;const{pathname:a,searchParams:s}=o,c=a.replace(e,""),k=((i=s.get("_p"))!=null?i:"").split(","),h=L(r,c,k),{lookupValue:l,lookupFields:u}=h??{};if(!l)throw new S("no data source");const _=JSON.parse((t=s.get("_q"))!=null?t:"{}"),p=w(l,_).map(m=>({...g(u??[]),[l]:m})),f=Number.parseInt((n=s.get("_l"))!=null?n:"25");if(!isNaN(f)){const m=[l,...u??[]];for(let d=0;d<f-p.length;d++)p.push(g(m))}return new Response(JSON.stringify(p),{headers:{"Cache-Control":"no-cache","Content-Type":"application/json"},status:200})}const U=({dataSchema:o,basePath:e=""})=>{const r=e.charAt(e.length-1)==="/"?e:"".concat(e,"/");return[{handler:i=>{let t;try{t=q(i,e,o??{type:"object",properties:{}})}catch(n){const a=n instanceof v?n:new R;t=new Response(JSON.stringify(a.getResponse()),{headers:{"Cache-Control":"no-cache","Content-Type":"application/json"},status:a.statusCode})}return Promise.resolve(t)},method:"GET",url:{pathname:r}}]},x={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"},label:"CRUD lookup Client",mocks:{fetch:U},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 I=x;export{I as default};
