@picked/client 0.0.12-next.5 → 0.0.12-next.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- "use strict";const a=require("effector"),q=require("@withease/factories"),C=require("./supports-YvpQZxbh.cjs"),I=require("./logger-BsmHUUw1.cjs"),fe=require("./object-BbFTsGh3.cjs");function le(e){return typeof e=="number"&&!isNaN(e)}function R(e){return typeof e=="string"}function N(e){return Array.isArray(e)}function Z(e){return e instanceof Promise}function J(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Object.keys(e).length===1}function m(e){if(le(e))return e;const t=parseFloat(`${e}`);return isNaN(t)?0:t}function O(e){return`${e}`}const de={"==":(e,t)=>e==t,"===":(e,t)=>e===t,"!=":(e,t)=>e!=t,"!==":(e,t)=>e!==t,">":(e,t)=>m(e)>m(t),">=":(e,t)=>m(e)>=m(t),"<":(e,t,r)=>{const n=m(e),s=m(t);if(r===void 0)return n<s;const c=m(r);return n<s&&s<c},"<=":(e,t,r)=>{const n=m(e),s=m(t);if(r===void 0)return n<=s;const c=m(r);return n<=s&&s<=c},"!!":async e=>await A(e),"!":async e=>!await A(e),"%":(e,t)=>m(e)%m(t),log:e=>(console.log(e),e),in:(e,t)=>t?R(t)?t.indexOf(O(e))!==-1:N(t)?t.includes(e):!1:!1,cat:(...e)=>e.map(O).join(""),substr:(e,t,r)=>{const n=O(e),s=m(t);if(r===void 0)return n.substr(s);const c=m(r);if(c<0){const o=n.substr(s);return o.substr(0,o.length+c)}return n.substr(s,c)},"+":(...e)=>e.reduce((t,r)=>m(t)+m(r),0),"*":(...e)=>e.reduce((t,r)=>m(t)*m(r),1),"-":(e,t)=>{const r=m(e);return t===void 0?-r:r-m(t)},"/":(e,t)=>m(e)/m(t),min:(...e)=>Math.min(...e.map(m)),max:(...e)=>Math.max(...e.map(m)),merge:(...e)=>e.reduce((t,r)=>N(r)?t.concat(r):t.concat([r]),[]),var(e,t){const r=arguments.length>1?t:null;if(e===""||e==="."||e===null||e===void 0)return this;if(typeof e=="number"&&Array.isArray(this))return e>=0&&e<this.length?this[e]:r;const n=O(e);if(n===""||n===".")return this;const s=n.split(".");let c=this;for(const o of s){if(c==null)return r;if(Array.isArray(c)){const u=parseInt(o,10);isNaN(u)?c=c[o]:c=c[u]}else c=c[o]}return c!==void 0?c:r},async missing(...e){const t=N(e[0])?e[0]:e,r=[];for(const n of t){const s=await g({var:n},this);(s===null||s==="")&&r.push(`${n}`)}return r},async missing_some(e,t){const r=await g({missing:t},this);if(!N(r)||!N(t))return[];const n=r.filter(R);return t.filter(R).length-n.length>=m(e)?[]:n},invoke(e){return typeof e!="function"?null:e()},regexp_matches(e,t){let r=e,n="";if(N(e)){const[c,o]=e;r=c,n=o}return!R(r)||!R(n)?!1:new RegExp(r,n).test(O(t))},throw(e){throw new Error(O(e))}};async function g(e,t){if(!e)return e;if(Z(e))return g(await e,t);if(N(e))return Promise.all(e.map(o=>g(o,t)));if(!J(e))return e;const r=pe(e);let n=r?e[r]??[]:[];switch(N(n)||(n=[n]),r){case"if":case"?:":for(let o=0;o<n.length-1;o+=2)if(await A(await g(n[o],t)))return g(n[o+1],t);return n.length%2===1?g(n[n.length-1],t):null;case"and":{let o=null;for(const u of n)if(o=await g(u,t),!await A(o))return o;return o}case"or":{let o=null;for(const u of n)if(o=await g(u,t),await A(o))return o;return o}case"map":{const o=await g(n[0],t);return N(o)?await Promise.all(o.map(u=>g(n[1],u))):[]}case"filter":{const o=await g(n[0],t);return N(o)?(await Promise.all(o.map(async d=>{const y=await A(await g(n[1],d));return{item:d,shouldInclude:y}}))).filter(({shouldInclude:d})=>d).map(({item:d})=>d):[]}case"reduce":{const o=await g(n[0],t);if(!N(o))return n.length>2?await g(n[2],t):null;const u=n[1];let y=n.length>2?await g(n[2],t):null;for(const b of o)y=await g(u,{current:b,accumulator:y});return y}case"all":{const o=await g(n[0],t);if(!N(o)||o.length===0)return!1;for(const u of o)if(!await A(await g(n[1],u)))return!1;return!0}case"some":{const o=await g(n[0],t);if(!N(o))return!1;for(const u of o)if(await A(await g(n[1],u)))return!0;return!1}case"none":{const o=await g(n[0],t);if(!N(o))return!0;for(const u of o)if(await A(await g(n[1],u)))return!1;return!0}case"catch":try{return await g(n[0],t)}catch(o){return J(o)?await g(o,t):n[1]?await g(n[1],o):o instanceof Error?o.message:o}}const s=await Promise.all(n.map(o=>g(o,t))),c=r?de[r]:void 0;if(!c)throw new Error(`Unrecognized operation ${r}`);return c.apply(t,s)}async function A(e){return Z(e)?A(await e):!(Array.isArray(e)&&e.length===0)&&!!e}function pe(e){return Object.keys(e)[0]}function ge(e){return!fe.isEmpty(e)}async function ee(e,t,r){const n=e.getStateSnapshot(),s=e.predicates.listStore.getState().reduce((u,d)=>({...u,[d.name]:()=>d.predicate(n)}),{}),c=e.features.listStore.getState().reduce((u,d)=>({...u,[d.id]:async()=>{const{bucket:y}=await d.getBucket();return y}}),{}),o={...r,responses:e.responses.getAllStore.getState(),location:typeof n.location=="object"?{hash:n.location.hash,host:n.location.host,hostname:n.location.hostname,href:n.location.href,origin:n.location.origin,pathname:n.location.pathname,port:n.location.port,protocol:n.location.protocol,search:n.location.search}:void 0,predicates:s,features:c};return await g(t,o)}function me(e){return!C.supportsWindow()||!C.supportsDocument()?[]:Array.from(window.document.querySelectorAll(e)).filter(t=>t instanceof HTMLElement)}function he(e){if(!C.supportsShadowRoot())throw new Error("Shadow DOM is not supported in this environment");return e.attachShadow({mode:"closed"})}function Se(){return new Promise((e,t)=>{if(!C.supportsDocument()){t();return}document.readyState!=="loading"?e():document.addEventListener("readystatechange",()=>e(),{once:!0})})}function ye(e,t){const r=new MutationObserver(n=>{document.contains(e)||(r.disconnect(),t())});r.observe(document.body,{childList:!0,subtree:!0})}const H="picked-root",j="picked-target",we="picked-root";function Ee(e,t){return[...e,{selector:`${we}[experience-id="${t}"]`,placement:"replace"}].flatMap(n=>{const s=me(n.selector).filter(c=>!xe(c,t)&&!te(c,t));return{...n,elements:s}})}function Ne(e,t){return e.flatMap(r=>r.elements.map(n=>be(n,r.placement,t)))}function Ie(e,t){e.classList.add(j),e.classList.add(`${j}-${t}`)}function xe(e,t){return t?e.classList.contains(`${j}-${t}`):e.classList.contains(`${j}`)}function te(e,t){return t?e.classList.contains(`${H}-${t}`):e.classList.contains(H)}function ke(e){const t=document.createElement("div");return t.style.display="block",t.classList.add(H),t.classList.add(`${H}-${e}`),t}function be(e,t,r){let n;const s=ke(r);if(e instanceof Document)n=e.body.appendChild(s);else{switch(t){case"replace":e.replaceWith(s);break;case"before":e.before(s);break;case"after":e.after(s);break;case"prepend":e.prepend(s);break;default:e.append(s);break}n=s}return n}function ve(e){return e.type==="conditional"}function T(e){return e.type==="step"}function Ae(e){return e.type==="experience"}function Q(e){return e.type==="origin"}function Y(e){return e.type==="action"}const ne=(e,t,r,n,s=new Set)=>s.has(r.id)?[]:(s.add(r.id),T(r)&&!n?[r]:t.filter(o=>o.sourceNodeId===r.id).flatMap(o=>{const u=e.find(d=>d.id===o.destinationNodeId);return u?ne(e,t,u,!1,s):[]})),$e=(e,t,r)=>ne(e,t,r,!0,new Set),re=(e,t,r,n,s=new Set)=>{if(s.has(r.id))return[-1,-1];s.add(r.id);const c=t.filter(d=>d.sourceNodeId===r.id);if(c.length===0)return[0,0];let o=1/0,u=-1/0;for(const d of c){const y=e.find(_=>_.id===d.destinationNodeId);if(!y)continue;const[b,w]=re(e,t,y,!1,new Set(s));b!==-1&&(o=Math.min(o,b+1),u=Math.max(u,w+1))}return o===1/0?[-1,-1]:[o,u]},Fe=(e,t,r)=>re(e,t,r,!0,new Set),oe=(e,t,r,n=new Set)=>{if(n.has(r.id))return[-1,-1];if(n.add(r.id),r.type==="origin")return[0,0];const s=t.filter(u=>u.destinationNodeId===r.id);if(s.length===0)return[-1,-1];let c=1/0,o=-1/0;for(const u of s){const d=e.find(w=>w.id===u.sourceNodeId);if(!d)continue;const[y,b]=oe(e,t,d,new Set(n));y!==-1&&(c=Math.min(c,y+1),o=Math.max(o,b+1))}return c===1/0?[-1,-1]:[c,o]},Be=(e,t,r)=>oe(e,t,r,new Set),ie=e=>e.find(r=>r.type==="origin")||null,V=(e,t,r)=>{if(!e)return ie(t);const n=r.find(c=>c.sourceNodeId===e.id);if(!n)return null;const s=t.find(c=>c.id===n.destinationNodeId);return s||null};async function _e(e,t,r,n){const s=e?"true":"false",c=n.find(o=>o.sourceNodeHandle===s&&o.sourceNodeId===t);return r.find(o=>o.id===(c==null?void 0:c.destinationNodeId))}const W=a.createDomain("experience-instance"),Le=q.createFactory(({client:e,experience:t,target:r})=>{const n=a.createEvent(),s=a.createEvent(),c=a.createEvent(),o=a.createEvent(),u=t.componentStore,d=W.store("transitioning"),y=e.history.entriesStore.map(i=>i.filter(f=>f.experienceId===t.id)),b=y.map(i=>i.filter(f=>T(f.node))),w=y.map(i=>{var f;return((f=i[i.length-1])==null?void 0:f.node)||null}),_=a.attach({source:t.treeStore,effect:async({nodes:i,edges:f},l)=>{if(!l||!Y(l))return;const E=l.action;if(!E){I.logger("warn",`Action node ${l.id} has no action name`);return}const L=e.actions.getByName(E).getState();if(!L){I.logger("warn",`Action '${E}' not registered on client`);const x=f.find(S=>S.sourceNodeId===l.id&&S.sourceNodeHandle==="false");return x?i.find(S=>S.id===x.destinationNodeId):null}if(typeof L.action!="function"){I.logger("warn",`Action '${E}' is not a function`);const x=f.find(S=>S.sourceNodeId===l.id&&S.sourceNodeHandle==="false");return x?i.find(S=>S.id===x.destinationNodeId):null}try{I.logger("info",`Executing action: ${E}`),await L.action();const x=f.find(k=>k.sourceNodeId===l.id&&k.sourceNodeHandle==="true");if(!x)return I.logger("warn",`No 'true' edge found for action node ${l.id}`),null;const S=i.find(k=>k.id===x.destinationNodeId);return I.logger("info",`Action '${E}' succeeded, moving to node ${S==null?void 0:S.id}`),S||null}catch(x){I.logger("warn",`Action '${E}' failed:`,x);const S=f.find(P=>P.sourceNodeId===l.id&&P.sourceNodeHandle==="false");if(!S)return I.logger("warn",`No 'false' edge found for action node ${l.id}`),null;const k=i.find(P=>P.id===S.destinationNodeId);return I.logger("info",`Action '${E}' failed, moving to node ${k==null?void 0:k.id}`),k||null}}}),B=a.combine(t.treeStore,w,({steps:i},f)=>i.find(l=>f&&T(f)&&l.id===f.stepId)||null),p=a.combine(t.treeStore,w,({nodes:i,edges:f},l)=>l?$e(i,f,l):[]),h=a.combine(t.treeStore,w,({nodes:i,edges:f},l)=>l?Be(i,f,l):[-1,-1]),v=a.combine(t.treeStore,w,({nodes:i,edges:f},l)=>l?Fe(i,f,l):[-1,-1]),$=W.store(!0),F=a.createApi($,{open:()=>!0,close:()=>!1,toggle:i=>!i,set:(i,f)=>f});a.sample({source:$,filter:Boolean,target:e.eventBus.prepend(()=>({type:"experience_open",experience:t}))}),a.sample({source:$,filter:i=>!i,target:e.eventBus.prepend(()=>({type:"experience_close",experience:t}))});const M=a.createEffect(async()=>{e.history.back({experienceId:t.id})}),ce=a.attach({source:t.treeStore,effect:async(i,f)=>{const l=i.nodes.find(E=>T(E)&&E.stepId===f);l&&e.history.push({experienceId:t.id,node:l})}}),z=a.attach({source:w,effect:i=>{}}),G=a.attach({source:t.treeStore,effect:async({nodes:i})=>ie(i)}),K=a.attach({source:t.treeStore,effect:async({nodes:i,edges:f},l)=>V(l,i,f)}),U=a.attach({source:t.treeStore,effect:async({nodes:i,edges:f,conditionals:l},E)=>{if(!E)return;const L=l.find(k=>k.id===E.conditionalId);if(!L)return;const x=E.id,S=await ee(e,L.condition);return _e(!!S,x,i,f)}}),ae=a.createEffect(i=>{if(i)return e.launchExperienceById(i.experienceId)}),X=a.attach({source:[w,t.treeStore],effect:async([i,{nodes:f,edges:l}])=>V(i,f,l)}),D=a.attach({source:[w,t.treeStore],effect:async([i,{nodes:f,edges:l}])=>V(i,f,l)}),ue=a.createEffect(i=>{e.history.push({experienceId:t.id,node:i})});return a.split({source:w,match:i=>i?ve(i)?"condition":T(i)?"step":Ae(i)?"launchExperience":Q(i)?"origin":Y(i)?"action":"other":"none",cases:{none:G,origin:K,condition:U,step:z,launchExperience:ae,action:_,__:a.createEvent()}}),a.sample({clock:[U.doneData,K.doneData,G.doneData,D.doneData,X.doneData,_.doneData],filter:Boolean,target:ue}),a.sample({clock:s,target:D}),a.sample({clock:s,source:B,filter:Boolean,target:e.eventBus.prepend(i=>({type:"step_submit",step:i,experience:t}))}),a.sample({clock:D.done,source:B,filter:Boolean,target:e.eventBus.prepend(i=>({type:"step_submit_success",step:i,experience:t}))}),a.sample({clock:D.fail,source:B,filter:Boolean,target:e.eventBus.prepend(i=>({type:"step_submit_error",step:i,experience:t}))}),a.sample({clock:c,target:M}),a.sample({clock:o,target:ce}),a.sample({source:a.combine([U.pending,K.pending,G.pending,D.pending]),fn:i=>i.some(Boolean)?"transitioning":"ready",target:d}),a.sample({clock:z.doneData,source:B,filter:Boolean,target:e.eventBus.prepend(i=>({type:"step_enter",step:i,experience:t}))}),a.sample({source:w,clock:n,filter:i=>i?Q(i):!0,target:X}),n(),{id:Math.random().toString(36).substring(7),experienceId:t.id,configStore:t.configStore,target:r,componentStore:u,stateStore:d,isOpenStore:$,open:F.open,close:F.close,toggleOpen:F.toggle,setOpen:F.set,treeStore:t.treeStore,currentNodeStore:w,currentStepStore:B,nextReachableStepNodesStore:p,stepsToOriginStore:h,stepsToLeafStore:v,goToNextStep:s,goToPreviousStep:c,goToStepById:o,stepsHistoryStore:b}});function se(e){return q.invoke(Le,e)}const Oe=q.createFactory(({client:e})=>{const t=W.store({}),r=a.createEvent(),n=a.createEvent(),s=a.createEvent(),c=a.createApi(t,{register(p,h){const v=[...p[h.experienceId]??[]];return v.push(h),{...p,[h.experienceId]:v}},unregister(p,h){var F;const v=h.experienceId,$=(F=p[v])==null?void 0:F.filter(M=>M!==h);return{...p,[v]:$??[]}}}),o=a.createEffect(({experience:p,target:h})=>se({client:e,experience:p,target:h})),u=a.createEffect(p=>{c.register(p)}),d=a.createEffect(p=>{p.target&&ye(p.target,()=>{s(p)})}),y=a.createEffect(p=>{c.unregister(p)}),b=a.createEffect(p=>{const h=p.componentStore.getState();if(!h){I.logger("warn","No app component registered. Skipping init.");return}const v=h.frameworkStore.getState();if(!v){I.logger("error",`No framework registered for component '${h.name}'. Skipping rendering.`);return}const $=p.target;if(!$){I.logger("error",`No target element registered for experience '${p.experienceId}'. Skipping rendering.`);return}const F=he($);try{v.render(F,h.component,e,p)}catch(M){I.logger("error",M)}});a.sample({clock:r,target:o}),a.sample({clock:s,target:y}),a.sample({clock:o.doneData,target:[u,d,n]}),a.sample({clock:o.doneData,target:e.eventBus.prepend(p=>({type:"experience_initialized",experienceInstance:p}))}),a.sample({clock:n,target:b});function w(p,h){r({experience:p,target:h})}const _=t.map(p=>Object.values(p).flat());function B(p){return t.map(h=>h[p]||[],{skipVoid:!1})}return{spawn:w,getByExperienceId:B,listStore:_}});function Te(e){return q.invoke(Oe,e)}exports.createAppHosts=Ne;exports.createExperienceInstance=se;exports.createExperienceInstanceIndex=Te;exports.domInteractive=Se;exports.evaluateRuleLogic=ee;exports.getTargetElements=Ee;exports.isPickedRoot=te;exports.isRulesLogic=ge;exports.isStepNode=T;exports.tagTargetElement=Ie;