@picked/client 0.0.10-next.1 → 0.0.10-next.2

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.
package/dist/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var T=Object.defineProperty;var U=(t,e,s)=>e in t?T(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var h=(t,e,s)=>U(t,typeof e!="symbol"?e+"":e,s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("effector"),S=require("@withease/factories"),F=require("../experience-instance-D4dHslLZ.cjs"),x=require("../logger-BsmHUUw1.cjs"),w=require("../storage-LI3UdcCr.cjs"),V=require("../id-BKIWbSTy.cjs"),N=require("../object-BbFTsGh3.cjs"),v=require("../supports-YvpQZxbh.cjs"),L=f.createDomain("experience"),z=S.createFactory(({client:t,experience:e})=>{const s=L.store(e),o=f.createApi(s,{merge(n,p){return{...n,...p.experience}}}),i=s.map(n=>({type:n.type,name:n.name,decisionLogic:n.decide,targetSelectors:n.targets})),l=t.components.listStore.map(n=>n.find(p=>p.name===`experience:${e.id}`)||n.find(p=>p.name==="experience"),{skipVoid:!1}),r=s.map(n=>({steps:n.steps,nodes:n.nodes,edges:n.edges,conditionals:n.conditionals}));return{id:e.id,configStore:i,treeStore:r,componentStore:l,merge:o.merge}});function q(t){return S.invoke(z,t)}const K=S.createFactory(({client:t})=>{const e=L.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.id]:n}}});function o(r){const n=e.getState()[r.id];if(n){n.merge({experience:r,strategy:"override"});return}const p=q({client:t,experience:r});s.register(p)}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getById:l,listStore:i}});function _(t){return S.invoke(K,t)}const H=(t,e)=>{e.register(t)},O=(t,e)=>{const{targetSelectors:s}=e.configStore.getState(),o=F.getTargetElements(s,e.id);o.forEach(({elements:l})=>l.forEach(r=>F.tagTargetElement(r,e.id))),F.createAppHosts(o,e.id).forEach(l=>{t.experienceInstances.spawn(e,l)})},$=async t=>{await Promise.all(t.experiences.listStore.getState().map(async e=>{await W(t,e)&&O(t,e)}))},W=async(t,e)=>{const{type:s,decisionLogic:o,targetSelectors:i}=e.configStore.getState();return F.getTargetElements(i,e.id).length<1?!1:s==="funnel"?o?await F.evaluateRuleLogic(t,o)===!0:!1:i.length===0?!1:F.isRulesLogic(o)?await F.evaluateRuleLogic(t,o)===!0:!0},G=(t,e,s)=>{const o=e.name,i=e.setup(t);if(i){const l=s[o]??{};s[o]=Object.freeze({...l,...i})}},J=f.createDomain("component"),Y=S.createFactory(({client:t,component:e,name:s})=>{const o=t.frameworks.getByName(e.frameworkName);return{name:s,component:e,frameworkStore:o}});function Q(t){return S.invoke(Y,t)}const X=S.createFactory(({client:t})=>{const e=J.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.name]:n}}});function o(r,n){e.getState()[r]&&x.logger("warn",`Component '${r}' already registered. Registering again will override the previous registration.`);const k=Q({client:t,name:r,component:n});s.register(k)}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getByName:l,listStore:i}});function Z(t){return S.invoke(X,t)}const A=(t,e,s)=>{s.register(t,e)},ee=f.createDomain("framework"),te=S.createFactory(({client:t})=>{const e=ee.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.name]:n.framework}}});function o(r,n){e.getState()[r]&&x.logger("warn",`Framework '${r}' already registered. Registering again will override the previous registration.`),s.register({name:r,framework:n})}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getByName:l,listStore:i}});function re(t){return S.invoke(te,t)}const se=(t,e)=>{e.register(t.name,t)},ne=6e3,B=w.createStorageKey("last_ping","global"),oe=t=>`${t}/api/v1/ping`,ie=async t=>{try{const e=Date.now(),s=localStorage.getItem(B);if(s&&e-parseInt(s)<ne)return!0;const o=oe(t.apiUrl),i={projectId:t.projectId,path:window.location.pathname},l=JSON.stringify(i),r={"Content-Type":"application/json"},n=document.getElementById("picked_script"),p=n==null?void 0:n.dataset.verceltoken;p&&(r["x-vercel-protection-bypass"]=p);const c=(await fetch(o,{method:"POST",headers:r,body:l})).ok;return c&&localStorage.setItem(B,e.toString()),c}catch(e){return x.logger("error",e),!1}};function ae(t){const e=typeof window<"u",s={client:t,responses:t.responses.listStore.getState(),fields:t.fields.listStore.getState(),location:e?window.location:void 0,document:e?window.document:void 0,sessionStorage:e?window.sessionStorage:void 0,localStorage:e?window.localStorage:void 0};return Object.freeze(s)}const ce=f.createDomain("predicate"),ue=S.createFactory(({client:t})=>{const e=ce.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.name]:{name:n.name,predicate:n.predicate}}}});function o(r,n){e.getState()[r]&&x.logger("warn",`Predicate '${r}' already registered. Registering again will override the previous registration.`),s.register(Object.freeze({name:r,predicate:n}))}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getByName:l,listStore:i}});function le(t){return S.invoke(ue,t)}const pe=(t,e,s)=>{s.register(t,e)},j=f.createDomain("feature"),ge=S.createFactory(()=>{const t=j.store({}),e=j.store([]),s=f.createApi(t,{register(c,m){return{...c,[m.id]:m.feature}}});function o(c,m){t.getState()[c]&&x.logger("warn",`Feature '${c}' already registered. Registering again will override the previous registration.`);const g={id:c,feature:{...m,id:c}};s.register(Object.freeze({id:c,feature:g}))}const i=f.createEffect(async({id:c,key:m,storage:a="local"})=>{const g=w.createStorageKey(`feature:${c}:${m}`,"global"),y=(a==="session"?w.sessionStore:w.localStore).get(g).boolean();if(y!==null)return{id:c,bucket:y}}),l=f.createEffect(c=>{if(!c)return null;const m=w.createStorageKey(`feature:${c.id}:${c.key}`,"global");(c.storage==="session"?w.sessionStore:w.localStore).set(m,c.bucket)}),r=new Map,n=f.attach({source:{allFeaturesStore:t,resolvedFeaturesStore:e},effect:async({allFeaturesStore:c,resolvedFeaturesStore:m},{id:a,decide:g,key:d})=>{const y=m.find(E=>E.id===a);if(y)return y;const u=c[a];if(!u)throw new Error(`Feature with id '${a}' not found`);const b=r.get(`${a}:${d}`);if(b){const E=await b;return{id:u.id,bucket:E,storage:u.feature.storage}}const I=g();r.set(`${a}:${d}`,I);const P=await I;return{id:u.id,bucket:P,storage:u.feature.storage}}});f.sample({clock:s.register,fn:c=>{const{id:m,feature:a}=c;return{id:m,key:a.feature.key,storage:a.feature.storage}},target:i}),f.sample({source:e,clock:[i.doneData,n.doneData],fn:(c,m)=>!m||c.some(a=>a.id===m.id)?c:[...c,m],target:e}),f.sample({source:t,clock:n.doneData,fn:(c,m)=>{var u;const{id:a,bucket:g,storage:d}=m,y=(u=c[a])==null?void 0:u.feature.key;return y?{key:y,id:a,bucket:g,storage:d}:null},target:l});const p=f.combine({allFeaturesStore:t,resolvedFeaturesStore:e}).map(({allFeaturesStore:c,resolvedFeaturesStore:m})=>Object.values(c).map(a=>{const g=m.find(d=>d.id===a.id);return g?{id:a.id,name:a.feature.name,key:a.feature.key,getBucket:()=>({id:a.id,bucket:g.bucket}),isResolved:!0}:{id:a.id,name:a.feature.name,key:a.feature.key,getBucket:async()=>await n(a.feature),isResolved:!1}}));function k(c){return t.map(m=>m[c],{skipVoid:!1})}return{register:o,getByName:k,listStore:p}});function de(){return S.invoke(ge)}const fe=(t,e,s)=>{s.register(t,e)},R=f.createDomain("history");function C(t,e){return{id:V.generateId(`history-entry-${Date.now()}-${Math.random()}}`),node:e,at:Date.now(),experienceId:t}}const me=S.createFactory(()=>{const t=R.store({entries:[]}),e=R.event(),s=R.event(),o=R.event(),i=R.event();t.on(e,(r,n)=>({entries:[...r.entries,C(n.experienceId,n.node)]})),t.on(s,(r,n)=>({entries:[...r.entries.slice(0,-1),C(n.experienceId,n.node)]})),t.on(i,(r,n)=>n);function l(r,n){const p={lastStepIndex:-1,secondLastStepIndex:-1};for(let k=r.length-1;k>=0;k--){const c=r[k],m=c==null?void 0:c.node;if(!(!m||!F.isStepNode(m))&&!(n&&(c==null?void 0:c.experienceId)!==n))if(p.lastStepIndex===-1)p.lastStepIndex=k;else{p.secondLastStepIndex=k;break}}return p}return t.on(o,(r,n)=>{const{secondLastStepIndex:p}=l(r.entries,n.experienceId),k=p!==-1?r.entries.slice(0,p+1):r.entries;return{...r,entries:k}}),{push:e,replace:s,back:o,restore:i,entriesStore:t.map(r=>r.entries)}});function ye(){return S.invoke(me)}const he=()=>f.createEvent(),Se=f.createDomain("field"),ve=S.createFactory(()=>{const t=Se.store({}),e=f.createApi(t,{register(r,n){return{...r,[n.field.id]:n.field}}});function s(r){e.register({field:r})}const o=t.map(r=>Object.values(r));function i(r){return t.map(n=>n[r]||null)}function l(r){return t.map(n=>Object.values(n).find(p=>p.name===r)||null)}return{register:s,getById:i,listStore:o,getByName:l}});function ke(){return S.invoke(ve)}const we=(t,e)=>{e.register(t)},Fe=f.createDomain("response"),be=S.createFactory(({client:t})=>{const e=Fe.store({}),s=f.createEvent(),o=f.createEvent(),i=f.createApi(e,{register(a,g){return{...a,[g.fieldId]:g.response}},registerBatch(a,g){return{...a,...g}},clear(a,g){const{[g]:d,...y}=a;return y},clearAll(){return{}}});function l(a,g){i.register({fieldId:a,response:g})}async function r(a){const g=[];return await Promise.allSettled(Object.entries(a).map(async([d,y])=>{let u=y;const b=t.fields.getById(d).getState();if(!b){g.push({fieldId:d,userMessage:`Field '${d}' not found`,systemMessage:`No field with ID '${d}' exists. Available field IDs are: ${Object.keys(t.fields.listStore.getState()).join(",")}.`,value:u});return}if(b.multiple&&(typeof u=="string"||typeof u=="number"||typeof u=="boolean")&&(u=[u]),b.validation&&!N.isEmpty(b.validation)){const I=await F.evaluateRuleLogic(t,b.validation,{$input:u});if(typeof I=="string"){g.push({fieldId:d,userMessage:I,systemMessage:`Field validation resulted in an error: ${I}.`,value:u});return}if(I===!1){g.push({fieldId:d,userMessage:"This field is invalid",systemMessage:"Field validation resulted in an unspecified error.",value:u});return}}if(b.required&&(typeof u>"u"||u===null||u===""||Array.isArray(u)&&!u.length)){g.push({fieldId:d,userMessage:"This field is required",systemMessage:"Field is required.",value:u});return}if(!b.multiple&&Array.isArray(u)&&u.length>1){g.push({fieldId:d,userMessage:"This field expects a single response",systemMessage:"Field expects a single response.",value:u});return}if(b.options){const I=b.options.map(E=>E.value),P=Array.isArray(u)?u:[u];Array.isArray(u)&&(P.every(E=>I.includes(E))||g.push({fieldId:d,userMessage:"This field expects a valid option",systemMessage:`Field expects a valid option. Valid options are: ${I.join(", ")}`,value:u}))}})),{responsesToCommit:a,validationErrors:g}}async function n(a){const{responsesToCommit:g,validationErrors:d}=await r(a);return d.length?{errors:d}:(i.registerBatch(g),{errors:[]})}const p=e.map(a=>a),k=e.map(a=>Object.keys(a).map(d=>({fieldId:d,value:a[d]})));function c(a){const g=t.fields.getById(a),d=e.map(y=>y[a]||null);return f.combine({f:g,r:d},({f:y,r:u})=>u||(y!=null&&y.defaultValue?y.defaultValue:null))}function m(a){const g=t.fields.getByName(a);return f.combine({f:g,s:e},({f:d,s:y})=>{const u=d&&y[d.id]||null;return u||(d!=null&&d.defaultValue?d.defaultValue:null)})}return f.sample({clock:s,target:i.clearAll}),f.sample({clock:o,target:i.clear}),{register:l,clearAll:s,clear:o,commit:n,getResponsesToCommit:r,listStore:k,getAllStore:p,getByFieldId:c,getByFieldName:m}});function Ie(t){return S.invoke(be,t)}const xe=()=>"0.0.10-next.1";function Ee(){if(v.supportsDocument()){const t=[document.currentScript,document.getElementById("picked_script"),document.getElementById("picked"),document.getElementById("cvly"),document.getElementById("cvly-x")];for(const e of t)if(e!=null&&e.dataset.api)return e.dataset.api}return"https://api.picked.app"}function Re(){if(!v.supportsWindow())return{};const t=new URLSearchParams(window.location.search),e={},s=new Set;for(const[i]of t.entries())i.startsWith("picked-")&&s.add(i);for(const i of s){const l=i.replace("picked-","");e[l]=t.getAll(i)}const o=w.createStorageKey("s");if(s.size>0)w.sessionStore.set(o,e);else{const i=w.sessionStore.get(o);if(i)return i.json()||{}}return e}function Pe(){if(v.supportsCryptoRandomUUID())return crypto.randomUUID();const t=Date.now().toString(36),e=Math.random().toString(36).substring(2,15),s=Math.random().toString(36).substring(2,15),o=Math.random().toString(36).substring(2,15);return`${t}-${e}-${s}-${o}`}const De=()=>({id:Pe()}),$e=()=>{if(!v.supportsLocalStorage())return null;const t=w.localStore.get("session");let e=t.isSet()?t.json():null;return e||(e=De(),t.set(e)),e},Ae=t=>{const e=$e();return e&&t.eventBus({type:"session",...e}),e};class D{constructor(e){h(this,"options");h(this,"isReady",!1);h(this,"project");h(this,"plugins",{});h(this,"eventBus",he());h(this,"organizationId");h(this,"projectId");h(this,"apiUrl");h(this,"session");h(this,"searchParams",Re());h(this,"history",ye());h(this,"components",Z({client:this}));h(this,"experienceInstances",F.createExperienceInstanceIndex({client:this}));h(this,"experiences",_({client:this}));h(this,"frameworks",re({client:this}));h(this,"predicates",le({client:this}));h(this,"features",de());h(this,"responses",Ie({client:this}));h(this,"fields",ke());x.logger("info","Picked client initializing."),this.organizationId=e.organizationId,this.projectId=e.projectId,this.options=Object.freeze(e),this.apiUrl=e.apiUrl||Ee(),this.session=Ae(this),this.setupPlugins(),this.setupFeatures(),this.setupPredicates(),this.eventBus({type:"initialized",version:xe()}),x.logger("info","Picked client initialized.")}start(){ie(this),(async()=>{v.supportsDocument()&&(document.body&&await $(this),await F.domInteractive(),await $(this),this.eventBus({type:"ready"}),this.isReady=!0)})()}registerFramework(e){se(e,this.frameworks)}registerComponent(e,s){A(e,s,this.components)}registerPlugin(e){G(this,e,this.plugins)}registerFeature(e,s){fe(e,s,this.features)}registerPredicate(e,s){pe(e,s,this.predicates)}registerField(e){we(e,this.fields)}registerExperience(e){e.component&&A(`experience:${e.id}`,e.component,this.components),H(e,this.experiences)}getStateSnapshot(){return ae(this)}async launchExperienceById(e){const s=this.experiences.getById(e).getState();if(!s){x.logger("warn",`Experience with id '${e}' not found.`);return}O(this,s)}setupPlugins(){var e;(e=this.options.plugins)==null||e.forEach(s=>{if(Array.isArray(s)){s.forEach(i=>{this.registerPlugin(i)});return}const o=typeof s=="function"?s():s;if(Array.isArray(o)){o.forEach(i=>this.registerPlugin(i));return}this.registerPlugin(o)})}setupFeatures(){const e=this.options.features;e&&(Object.entries(e).forEach(([s,o])=>{const i=typeof o=="function"?o():o;this.registerFeature(s,i)}),this.searchParams.feature&&this.searchParams.feature.forEach(s=>{try{const[o,i]=s.split(":");if(!o||!i)return;let l=i;l==="true"&&(l=!0),l==="false"&&(l=!1),this.registerFeature(o,{key:`${l}`,name:o,decide:()=>l,storage:"session"})}catch{}}))}setupPredicates(){const e=this.options.predicates;e&&Object.entries(e).forEach(([s,o])=>{if(typeof o!="function"){x.logger("warn",`Predicate "${s}" is not a function.`);return}this.registerPredicate(s,o)})}getPluginConfig(e){return this.plugins[e]??{}}}h(D,"logger",x.logger);function M(t){var e;return((e=t.options)==null?void 0:e.some(s=>"condition"in s))??!1}function Be(t){return t.some(e=>e.condition||M(e))}async function je(t,e){return e.length===0||!Be(e)?e:(await Promise.all(e.map(async o=>{if(!(o.condition?await F.evaluateRuleLogic(t,o.condition):!0))return null;if(!o.options||!M(o))return o;const r=(await Promise.all(o.options.map(async p=>p.condition?await F.evaluateRuleLogic(t,p.condition)?p:null:p))).filter(p=>p!==null);return r.length===o.options.length?o:r.length===0?null:{...o,options:r}}))).filter(o=>o!==null)}const Ce=t=>{const e=new D(t);return v.bindClient(e),e};exports.evaluateRuleLogic=F.evaluateRuleLogic;exports.isPickedRoot=F.isPickedRoot;exports.createStorageKey=w.createStorageKey;exports.localStore=w.localStore;exports.sessionStore=w.sessionStore;exports.subscribe=w.subscribe;exports.supportsCookies=v.supportsCookies;exports.supportsCryptoRandomUUID=v.supportsCryptoRandomUUID;exports.supportsDocument=v.supportsDocument;exports.supportsHistory=v.supportsHistory;exports.supportsIntersectionObserver=v.supportsIntersectionObserver;exports.supportsLocalStorage=v.supportsLocalStorage;exports.supportsLocation=v.supportsLocation;exports.supportsSessionStorage=v.supportsSessionStorage;exports.supportsShadowRoot=v.supportsShadowRoot;exports.supportsWebComponents=v.supportsWebComponents;exports.supportsWindow=v.supportsWindow;exports.Picked=D;exports.createClient=Ce;exports.getStepFields=je;
1
+ "use strict";var T=Object.defineProperty;var U=(t,e,s)=>e in t?T(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var h=(t,e,s)=>U(t,typeof e!="symbol"?e+"":e,s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("effector"),S=require("@withease/factories"),F=require("../experience-instance-D4dHslLZ.cjs"),x=require("../logger-BsmHUUw1.cjs"),k=require("../storage-LI3UdcCr.cjs"),V=require("../id-BKIWbSTy.cjs"),N=require("../object-BbFTsGh3.cjs"),v=require("../supports-YvpQZxbh.cjs"),O=f.createDomain("experience"),K=S.createFactory(({client:t,experience:e})=>{const s=O.store(e),o=f.createApi(s,{merge(n,p){return{...n,...p.experience}}}),i=s.map(n=>({type:n.type,name:n.name,decisionLogic:n.decide,targetSelectors:n.targets})),l=t.components.listStore.map(n=>n.find(p=>p.name===`experience:${e.id}`)||n.find(p=>p.name==="experience"),{skipVoid:!1}),r=s.map(n=>({steps:n.steps,nodes:n.nodes,edges:n.edges,conditionals:n.conditionals}));return{id:e.id,configStore:i,treeStore:r,componentStore:l,merge:o.merge}});function z(t){return S.invoke(K,t)}const q=S.createFactory(({client:t})=>{const e=O.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.id]:n}}});function o(r){const n=e.getState()[r.id];if(n){n.merge({experience:r,strategy:"override"});return}const p=z({client:t,experience:r});s.register(p)}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getById:l,listStore:i}});function _(t){return S.invoke(q,t)}const H=(t,e)=>{e.register(t)},L=(t,e)=>{const{targetSelectors:s}=e.configStore.getState(),o=F.getTargetElements(s,e.id);o.forEach(({elements:l})=>l.forEach(r=>F.tagTargetElement(r,e.id))),F.createAppHosts(o,e.id).forEach(l=>{t.experienceInstances.spawn(e,l)})},A=async t=>{await Promise.all(t.experiences.listStore.getState().map(async e=>{await W(t,e)&&L(t,e)}))},W=async(t,e)=>{const{type:s,decisionLogic:o,targetSelectors:i}=e.configStore.getState();return F.getTargetElements(i,e.id).length<1?!1:s==="funnel"?o?await F.evaluateRuleLogic(t,o)===!0:!1:i.length===0?!1:F.isRulesLogic(o)?await F.evaluateRuleLogic(t,o)===!0:!0},G=(t,e,s)=>{const o=e.name,i=e.setup(t);if(i){const l=s[o]??{};s[o]=Object.freeze({...l,...i})}},Y=f.createDomain("component"),J=S.createFactory(({client:t,component:e,name:s})=>{const o=t.frameworks.getByName(e.frameworkName);return{name:s,component:e,frameworkStore:o}});function Q(t){return S.invoke(J,t)}const X=S.createFactory(({client:t})=>{const e=Y.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.name]:n}}});function o(r,n){e.getState()[r]&&x.logger("warn",`Component '${r}' already registered. Registering again will override the previous registration.`);const w=Q({client:t,name:r,component:n});s.register(w)}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getByName:l,listStore:i}});function Z(t){return S.invoke(X,t)}const $=(t,e,s)=>{s.register(t,e)},ee=f.createDomain("framework"),te=S.createFactory(({client:t})=>{const e=ee.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.name]:n.framework}}});function o(r,n){e.getState()[r]&&x.logger("warn",`Framework '${r}' already registered. Registering again will override the previous registration.`),s.register({name:r,framework:n})}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getByName:l,listStore:i}});function re(t){return S.invoke(te,t)}const se=(t,e)=>{e.register(t.name,t)},ne=6e3,B=k.createStorageKey("last_ping","global"),oe=t=>`${t}/api/v1/ping`,ie=async t=>{try{const e=Date.now(),s=localStorage.getItem(B);if(s&&e-parseInt(s)<ne)return!0;const o=oe(t.apiUrl),i={projectId:t.projectId,path:window.location.pathname},l=JSON.stringify(i),r={"Content-Type":"application/json"},n=document.getElementById("picked_script"),p=n==null?void 0:n.dataset.verceltoken;p&&(r["x-vercel-protection-bypass"]=p);const c=(await fetch(o,{method:"POST",headers:r,body:l})).ok;return c&&localStorage.setItem(B,e.toString()),c}catch(e){return x.logger("error",e),!1}};function ae(t){const e=typeof window<"u",s={client:t,responses:t.responses.listStore.getState(),fields:t.fields.listStore.getState(),location:e?window.location:void 0,document:e?window.document:void 0,sessionStorage:e?window.sessionStorage:void 0,localStorage:e?window.localStorage:void 0};return Object.freeze(s)}const ce=f.createDomain("predicate"),ue=S.createFactory(({client:t})=>{const e=ce.store({}),s=f.createApi(e,{register(r,n){return{...r,[n.name]:{name:n.name,predicate:n.predicate}}}});function o(r,n){e.getState()[r]&&x.logger("warn",`Predicate '${r}' already registered. Registering again will override the previous registration.`),s.register(Object.freeze({name:r,predicate:n}))}const i=e.map(r=>Object.values(r));function l(r){return e.map(n=>n[r],{skipVoid:!1})}return{register:o,getByName:l,listStore:i}});function le(t){return S.invoke(ue,t)}const pe=(t,e,s)=>{s.register(t,e)},j=f.createDomain("feature"),ge=S.createFactory(()=>{const t=j.store({}),e=j.store([]),s=f.createApi(t,{register(c,m){return{...c,[m.id]:m.feature}}});function o(c,m){t.getState()[c]&&x.logger("warn",`Feature '${c}' already registered. Registering again will override the previous registration.`);const g={id:c,feature:{...m,id:c}};s.register(Object.freeze({id:c,feature:g}))}const i=f.createEffect(async({id:c,key:m,storage:a="local"})=>{const g=k.createStorageKey(`feature:${c}:${m}`,"global"),y=(a==="session"?k.sessionStore:k.localStore).get(g).boolean();if(y!==null)return{id:c,bucket:y}}),l=f.createEffect(c=>{if(!c)return null;const m=k.createStorageKey(`feature:${c.id}:${c.key}`,"global");(c.storage==="session"?k.sessionStore:k.localStore).set(m,c.bucket)}),r=new Map,n=f.attach({source:{allFeaturesStore:t,resolvedFeaturesStore:e},effect:async({allFeaturesStore:c,resolvedFeaturesStore:m},{id:a,decide:g,key:d})=>{const y=m.find(E=>E.id===a);if(y)return y;const u=c[a];if(!u)throw new Error(`Feature with id '${a}' not found`);const b=r.get(`${a}:${d}`);if(b){const E=await b;return{id:u.id,bucket:E,storage:u.feature.storage}}const I=g();r.set(`${a}:${d}`,I);const P=await I;return{id:u.id,bucket:P,storage:u.feature.storage}}});f.sample({clock:s.register,fn:c=>{const{id:m,feature:a}=c;return{id:m,key:a.feature.key,storage:a.feature.storage}},target:i}),f.sample({source:e,clock:[i.doneData,n.doneData],fn:(c,m)=>!m||c.some(a=>a.id===m.id)?c:[...c,m],target:e}),f.sample({source:t,clock:n.doneData,fn:(c,m)=>{var u;const{id:a,bucket:g,storage:d}=m,y=(u=c[a])==null?void 0:u.feature.key;return y?{key:y,id:a,bucket:g,storage:d}:null},target:l});const p=f.combine({allFeaturesStore:t,resolvedFeaturesStore:e}).map(({allFeaturesStore:c,resolvedFeaturesStore:m})=>Object.values(c).map(a=>{const g=m.find(d=>d.id===a.id);return g?{id:a.id,name:a.feature.name,key:a.feature.key,getBucket:()=>({id:a.id,bucket:g.bucket}),isResolved:!0}:{id:a.id,name:a.feature.name,key:a.feature.key,getBucket:async()=>await n(a.feature),isResolved:!1}}));function w(c){return t.map(m=>m[c],{skipVoid:!1})}return{register:o,getByName:w,listStore:p}});function de(){return S.invoke(ge)}const fe=(t,e,s)=>{s.register(t,e)},R=f.createDomain("history");function C(t,e){return{id:V.generateId(`history-entry-${Date.now()}-${Math.random()}}`),node:e,at:Date.now(),experienceId:t}}const me=S.createFactory(()=>{const t=R.store({entries:[]}),e=R.event(),s=R.event(),o=R.event(),i=R.event();t.on(e,(r,n)=>({entries:[...r.entries,C(n.experienceId,n.node)]})),t.on(s,(r,n)=>({entries:[...r.entries.slice(0,-1),C(n.experienceId,n.node)]})),t.on(i,(r,n)=>n);function l(r,n){const p={lastStepIndex:-1,secondLastStepIndex:-1};for(let w=r.length-1;w>=0;w--){const c=r[w],m=c==null?void 0:c.node;if(!(!m||!F.isStepNode(m))&&!(n&&(c==null?void 0:c.experienceId)!==n))if(p.lastStepIndex===-1)p.lastStepIndex=w;else{p.secondLastStepIndex=w;break}}return p}return t.on(o,(r,n)=>{const{secondLastStepIndex:p}=l(r.entries,n.experienceId),w=p!==-1?r.entries.slice(0,p+1):r.entries;return{...r,entries:w}}),{push:e,replace:s,back:o,restore:i,entriesStore:t.map(r=>r.entries)}});function ye(){return S.invoke(me)}const he=()=>f.createEvent(),Se=f.createDomain("field"),ve=S.createFactory(()=>{const t=Se.store({}),e=f.createApi(t,{register(r,n){return{...r,[n.field.id]:n.field}}});function s(r){e.register({field:r})}const o=t.map(r=>Object.values(r));function i(r){return t.map(n=>n[r]||null)}function l(r){return t.map(n=>Object.values(n).find(p=>p.name===r)||null)}return{register:s,getById:i,listStore:o,getByName:l}});function ke(){return S.invoke(ve)}const we=(t,e)=>{e.register(t)},Fe=f.createDomain("response"),be=S.createFactory(({client:t})=>{const e=Fe.store({}),s=f.createEvent(),o=f.createEvent(),i=f.createApi(e,{register(a,g){return{...a,[g.fieldId]:g.response}},registerBatch(a,g){return{...a,...g}},clear(a,g){const{[g]:d,...y}=a;return y},clearAll(){return{}}});function l(a,g){i.register({fieldId:a,response:g})}async function r(a){const g=[];return await Promise.allSettled(Object.entries(a).map(async([d,y])=>{let u=y;const b=t.fields.getById(d).getState();if(!b){g.push({fieldId:d,userMessage:`Field '${d}' not found`,systemMessage:`No field with ID '${d}' exists. Available field IDs are: ${Object.keys(t.fields.listStore.getState()).join(",")}.`,value:u});return}if(b.multiple&&(typeof u=="string"||typeof u=="number"||typeof u=="boolean")&&(u=[u]),b.validation&&!N.isEmpty(b.validation)){const I=await F.evaluateRuleLogic(t,b.validation,{$input:u});if(typeof I=="string"){g.push({fieldId:d,userMessage:I,systemMessage:`Field validation resulted in an error: ${I}.`,value:u});return}if(I===!1){g.push({fieldId:d,userMessage:"This field is invalid",systemMessage:"Field validation resulted in an unspecified error.",value:u});return}}if(b.required&&(typeof u>"u"||u===null||u===""||Array.isArray(u)&&!u.length)){g.push({fieldId:d,userMessage:"This field is required",systemMessage:"Field is required.",value:u});return}if(!b.multiple&&Array.isArray(u)&&u.length>1){g.push({fieldId:d,userMessage:"This field expects a single response",systemMessage:"Field expects a single response.",value:u});return}if(b.options){const I=b.options.map(E=>E.value),P=Array.isArray(u)?u:[u];Array.isArray(u)&&(P.every(E=>I.includes(E))||g.push({fieldId:d,userMessage:"This field expects a valid option",systemMessage:`Field expects a valid option. Valid options are: ${I.join(", ")}`,value:u}))}})),{responsesToCommit:a,validationErrors:g}}async function n(a){const{responsesToCommit:g,validationErrors:d}=await r(a);return d.length?{errors:d}:(i.registerBatch(g),{errors:[]})}const p=e.map(a=>a),w=e.map(a=>Object.keys(a).map(d=>({fieldId:d,value:a[d]})));function c(a){const g=t.fields.getById(a),d=e.map(y=>y[a]||null);return f.combine({f:g,r:d},({f:y,r:u})=>u||(y!=null&&y.defaultValue?y.defaultValue:null))}function m(a){const g=t.fields.getByName(a);return f.combine({f:g,s:e},({f:d,s:y})=>{const u=d&&y[d.id]||null;return u||(d!=null&&d.defaultValue?d.defaultValue:null)})}return f.sample({clock:s,target:i.clearAll}),f.sample({clock:o,target:i.clear}),{register:l,clearAll:s,clear:o,commit:n,getResponsesToCommit:r,listStore:w,getAllStore:p,getByFieldId:c,getByFieldName:m}});function Ie(t){return S.invoke(be,t)}const xe=()=>"0.0.10-next.2";function Ee(){if(v.supportsDocument()){const t=[document.currentScript,document.getElementById("picked_script"),document.getElementById("picked"),document.getElementById("cvly"),document.getElementById("cvly-x")];for(const e of t)if(e!=null&&e.dataset.api)return e.dataset.api}return"https://api.picked.app"}function Re(){if(!v.supportsWindow())return{};const t=new URLSearchParams(window.location.search),e={},s=new Set;for(const[i]of t.entries())i.startsWith("picked-")&&s.add(i);for(const i of s){const l=i.replace("picked-","");e[l]=t.getAll(i)}const o=k.createStorageKey("s");if(s.size>0)k.sessionStore.set(o,e);else{const i=k.sessionStore.get(o);if(i)return i.json()||{}}return e}function Pe(){if(v.supportsCryptoRandomUUID())return crypto.randomUUID();const t=Date.now().toString(36),e=Math.random().toString(36).substring(2,15),s=Math.random().toString(36).substring(2,15),o=Math.random().toString(36).substring(2,15);return`${t}-${e}-${s}-${o}`}const De=()=>({id:Pe()}),Ae=k.createStorageKey("session","global"),$e=()=>{if(!v.supportsLocalStorage())return null;const t=k.localStore.get(Ae);let e=t.isSet()?t.json():null;return e||(e=De(),t.set(e)),e},Be=()=>$e();class D{constructor(e){h(this,"options");h(this,"isReady",!1);h(this,"project");h(this,"plugins",{});h(this,"eventBus",he());h(this,"organizationId");h(this,"projectId");h(this,"apiUrl");h(this,"session");h(this,"searchParams",Re());h(this,"history",ye());h(this,"components",Z({client:this}));h(this,"experienceInstances",F.createExperienceInstanceIndex({client:this}));h(this,"experiences",_({client:this}));h(this,"frameworks",re({client:this}));h(this,"predicates",le({client:this}));h(this,"features",de());h(this,"responses",Ie({client:this}));h(this,"fields",ke());x.logger("info","Picked client initializing."),this.organizationId=e.organizationId,this.projectId=e.projectId,this.options=Object.freeze(e),this.apiUrl=e.apiUrl||Ee(),this.session=Be(),this.setupPlugins(),this.setupFeatures(),this.setupPredicates(),this.eventBus({type:"initialized",version:xe()}),x.logger("info","Picked client initialized.")}start(){ie(this),(async()=>{v.supportsDocument()&&(document.body&&A(this),await F.domInteractive(),await A(this),this.eventBus({type:"ready"}),this.isReady=!0,this.session&&this.eventBus({type:"session",...this.session}))})()}registerFramework(e){se(e,this.frameworks)}registerComponent(e,s){$(e,s,this.components)}registerPlugin(e){G(this,e,this.plugins)}registerFeature(e,s){fe(e,s,this.features)}registerPredicate(e,s){pe(e,s,this.predicates)}registerField(e){we(e,this.fields)}registerExperience(e){e.component&&$(`experience:${e.id}`,e.component,this.components),H(e,this.experiences)}getStateSnapshot(){return ae(this)}async launchExperienceById(e){const s=this.experiences.getById(e).getState();if(!s){x.logger("warn",`Experience with id '${e}' not found.`);return}L(this,s)}setupPlugins(){var e;(e=this.options.plugins)==null||e.forEach(s=>{if(Array.isArray(s)){s.forEach(i=>{this.registerPlugin(i)});return}const o=typeof s=="function"?s():s;if(Array.isArray(o)){o.forEach(i=>this.registerPlugin(i));return}this.registerPlugin(o)})}setupFeatures(){const e=this.options.features;e&&(Object.entries(e).forEach(([s,o])=>{const i=typeof o=="function"?o():o;this.registerFeature(s,i)}),this.searchParams.feature&&this.searchParams.feature.forEach(s=>{try{const[o,i]=s.split(":");if(!o||!i)return;let l=i;l==="true"&&(l=!0),l==="false"&&(l=!1),this.registerFeature(o,{key:`${l}`,name:o,decide:()=>l,storage:"session"})}catch{}}))}setupPredicates(){const e=this.options.predicates;e&&Object.entries(e).forEach(([s,o])=>{if(typeof o!="function"){x.logger("warn",`Predicate "${s}" is not a function.`);return}this.registerPredicate(s,o)})}getPluginConfig(e){return this.plugins[e]??{}}}h(D,"logger",x.logger);function M(t){var e;return((e=t.options)==null?void 0:e.some(s=>"condition"in s))??!1}function je(t){return t.some(e=>e.condition||M(e))}async function Ce(t,e){return e.length===0||!je(e)?e:(await Promise.all(e.map(async o=>{if(!(o.condition?await F.evaluateRuleLogic(t,o.condition):!0))return null;if(!o.options||!M(o))return o;const r=(await Promise.all(o.options.map(async p=>p.condition?await F.evaluateRuleLogic(t,p.condition)?p:null:p))).filter(p=>p!==null);return r.length===o.options.length?o:r.length===0?null:{...o,options:r}}))).filter(o=>o!==null)}const Oe=t=>{const e=new D(t);return v.bindClient(e),e};exports.evaluateRuleLogic=F.evaluateRuleLogic;exports.isPickedRoot=F.isPickedRoot;exports.createStorageKey=k.createStorageKey;exports.localStore=k.localStore;exports.sessionStore=k.sessionStore;exports.subscribe=k.subscribe;exports.supportsCookies=v.supportsCookies;exports.supportsCryptoRandomUUID=v.supportsCryptoRandomUUID;exports.supportsDocument=v.supportsDocument;exports.supportsHistory=v.supportsHistory;exports.supportsIntersectionObserver=v.supportsIntersectionObserver;exports.supportsLocalStorage=v.supportsLocalStorage;exports.supportsLocation=v.supportsLocation;exports.supportsSessionStorage=v.supportsSessionStorage;exports.supportsShadowRoot=v.supportsShadowRoot;exports.supportsWebComponents=v.supportsWebComponents;exports.supportsWindow=v.supportsWindow;exports.Picked=D;exports.createClient=Oe;exports.getStepFields=Ce;
package/dist/esm/index.js CHANGED
@@ -1,18 +1,18 @@
1
- var W = Object.defineProperty;
2
- var G = (t, e, s) => e in t ? W(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
3
- var y = (t, e, s) => G(t, typeof e != "symbol" ? e + "" : e, s);
4
- import { createDomain as b, createApi as I, createEffect as O, attach as J, sample as P, combine as A, createEvent as C } from "effector";
5
- import { createFactory as k, invoke as x } from "@withease/factories";
6
- import { g as U, t as Y, c as Q, e as R, i as X, a as Z, b as ee, d as te } from "../experience-instance-BEKoIaYp.js";
7
- import { f as pt } from "../experience-instance-BEKoIaYp.js";
1
+ var G = Object.defineProperty;
2
+ var W = (t, e, s) => e in t ? G(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
3
+ var h = (t, e, s) => W(t, typeof e != "symbol" ? e + "" : e, s);
4
+ import { createDomain as x, createApi as I, createEffect as O, attach as Y, sample as P, combine as A, createEvent as C } from "effector";
5
+ import { createFactory as k, invoke as b } from "@withease/factories";
6
+ import { g as U, t as J, c as Q, e as R, i as X, a as Z, b as ee, d as te } from "../experience-instance-BEKoIaYp.js";
7
+ import { f as gt } from "../experience-instance-BEKoIaYp.js";
8
8
  import { l as v } from "../logger-C9a75KZn.js";
9
9
  import { c as $, s as B, l as D } from "../storage-BxBBl9Bw.js";
10
- import { a as ft } from "../storage-BxBBl9Bw.js";
10
+ import { a as mt } from "../storage-BxBBl9Bw.js";
11
11
  import { g as re } from "../id-BW-g3y32.js";
12
12
  import { i as se } from "../object-JwZ58NOH.js";
13
13
  import { s as z, a as ne, b as oe, c as ie, d as ae } from "../supports-BjhuLJ8z.js";
14
- import { h as yt, f as ht, j as St, e as wt, g as vt, i as kt, k as xt } from "../supports-BjhuLJ8z.js";
15
- const _ = b("experience"), ce = k(
14
+ import { h as yt, f as St, j as wt, e as vt, g as kt, i as bt, k as xt } from "../supports-BjhuLJ8z.js";
15
+ const _ = x("experience"), ce = k(
16
16
  ({ client: t, experience: e }) => {
17
17
  const s = _.store(e), o = I(s, {
18
18
  merge(n, d) {
@@ -45,7 +45,7 @@ const _ = b("experience"), ce = k(
45
45
  }
46
46
  );
47
47
  function ue(t) {
48
- return x(ce, t);
48
+ return b(ce, t);
49
49
  }
50
50
  const le = k(({ client: t }) => {
51
51
  const e = _.store({}), s = I(e, {
@@ -76,14 +76,14 @@ const le = k(({ client: t }) => {
76
76
  };
77
77
  });
78
78
  function de(t) {
79
- return x(le, t);
79
+ return b(le, t);
80
80
  }
81
81
  const pe = (t, e) => {
82
82
  e.register(t);
83
83
  }, H = (t, e) => {
84
84
  const { targetSelectors: s } = e.configStore.getState(), o = U(s, e.id);
85
85
  o.forEach(
86
- ({ elements: l }) => l.forEach((r) => Y(r, e.id))
86
+ ({ elements: l }) => l.forEach((r) => J(r, e.id))
87
87
  ), Q(o, e.id).forEach((l) => {
88
88
  t.experienceInstances.spawn(e, l);
89
89
  });
@@ -102,7 +102,7 @@ const pe = (t, e) => {
102
102
  const l = s[o] ?? {};
103
103
  s[o] = Object.freeze({ ...l, ...i });
104
104
  }
105
- }, me = b("component"), ye = k(
105
+ }, me = x("component"), he = k(
106
106
  ({ client: t, component: e, name: s }) => {
107
107
  const o = t.frameworks.getByName(e.frameworkName);
108
108
  return {
@@ -112,8 +112,8 @@ const pe = (t, e) => {
112
112
  };
113
113
  }
114
114
  );
115
- function he(t) {
116
- return x(ye, t);
115
+ function ye(t) {
116
+ return b(he, t);
117
117
  }
118
118
  const Se = k(({ client: t }) => {
119
119
  const e = me.store({}), s = I(e, {
@@ -129,8 +129,8 @@ const Se = k(({ client: t }) => {
129
129
  "warn",
130
130
  `Component '${r}' already registered. Registering again will override the previous registration.`
131
131
  );
132
- const h = he({ client: t, name: r, component: n });
133
- s.register(h);
132
+ const y = ye({ client: t, name: r, component: n });
133
+ s.register(y);
134
134
  }
135
135
  const i = e.map((r) => Object.values(r));
136
136
  function l(r) {
@@ -143,11 +143,11 @@ const Se = k(({ client: t }) => {
143
143
  };
144
144
  });
145
145
  function we(t) {
146
- return x(Se, t);
146
+ return b(Se, t);
147
147
  }
148
148
  const T = (t, e, s) => {
149
149
  s.register(t, e);
150
- }, ve = b("framework"), ke = k(({ client: t }) => {
150
+ }, ve = x("framework"), ke = k(({ client: t }) => {
151
151
  const e = ve.store({}), s = I(e, {
152
152
  register(r, n) {
153
153
  return {
@@ -172,10 +172,10 @@ const T = (t, e, s) => {
172
172
  listStore: i
173
173
  };
174
174
  });
175
- function xe(t) {
176
- return x(ke, t);
175
+ function be(t) {
176
+ return b(ke, t);
177
177
  }
178
- const be = (t, e) => {
178
+ const xe = (t, e) => {
179
179
  e.register(t.name, t);
180
180
  }, Ie = 6e3, V = $("last_ping", "global"), Fe = (t) => `${t}/api/v1/ping`, Ee = async (t) => {
181
181
  try {
@@ -211,7 +211,7 @@ function Pe(t) {
211
211
  };
212
212
  return Object.freeze(s);
213
213
  }
214
- const Re = b("predicate"), $e = k(({ client: t }) => {
214
+ const Re = x("predicate"), $e = k(({ client: t }) => {
215
215
  const e = Re.store({}), s = I(e, {
216
216
  register(r, n) {
217
217
  return {
@@ -240,11 +240,11 @@ const Re = b("predicate"), $e = k(({ client: t }) => {
240
240
  };
241
241
  });
242
242
  function Be(t) {
243
- return x($e, t);
243
+ return b($e, t);
244
244
  }
245
245
  const je = (t, e, s) => {
246
246
  s.register(t, e);
247
- }, N = b("feature"), Ae = k(() => {
247
+ }, N = x("feature"), Ae = k(() => {
248
248
  const t = N.store({}), e = N.store([]), s = I(t, {
249
249
  register(c, f) {
250
250
  return {
@@ -287,7 +287,7 @@ const je = (t, e, s) => {
287
287
  return null;
288
288
  const f = $(`feature:${c.id}:${c.key}`, "global");
289
289
  (c.storage === "session" ? B : D).set(f, c.bucket);
290
- }), r = /* @__PURE__ */ new Map(), n = J({
290
+ }), r = /* @__PURE__ */ new Map(), n = Y({
291
291
  source: { allFeaturesStore: t, resolvedFeaturesStore: e },
292
292
  effect: async ({ allFeaturesStore: c, resolvedFeaturesStore: f }, { id: a, decide: p, key: g }) => {
293
293
  const m = f.find((F) => F.id === a);
@@ -350,21 +350,21 @@ const je = (t, e, s) => {
350
350
  };
351
351
  })
352
352
  );
353
- function h(c) {
353
+ function y(c) {
354
354
  return t.map((f) => f[c], { skipVoid: !1 });
355
355
  }
356
356
  return {
357
357
  register: o,
358
- getByName: h,
358
+ getByName: y,
359
359
  listStore: d
360
360
  };
361
361
  });
362
362
  function Ce() {
363
- return x(Ae);
363
+ return b(Ae);
364
364
  }
365
365
  const De = (t, e, s) => {
366
366
  s.register(t, e);
367
- }, E = b("history");
367
+ }, E = x("history");
368
368
  function L(t, e) {
369
369
  return {
370
370
  // eslint-disable-next-line sonarjs/pseudo-random
@@ -386,23 +386,23 @@ const Oe = k(() => {
386
386
  })), t.on(i, (r, n) => n);
387
387
  function l(r, n) {
388
388
  const d = { lastStepIndex: -1, secondLastStepIndex: -1 };
389
- for (let h = r.length - 1; h >= 0; h--) {
390
- const c = r[h], f = c == null ? void 0 : c.node;
389
+ for (let y = r.length - 1; y >= 0; y--) {
390
+ const c = r[y], f = c == null ? void 0 : c.node;
391
391
  if (!(!f || !Z(f)) && !(n && (c == null ? void 0 : c.experienceId) !== n))
392
392
  if (d.lastStepIndex === -1)
393
- d.lastStepIndex = h;
393
+ d.lastStepIndex = y;
394
394
  else {
395
- d.secondLastStepIndex = h;
395
+ d.secondLastStepIndex = y;
396
396
  break;
397
397
  }
398
398
  }
399
399
  return d;
400
400
  }
401
401
  return t.on(o, (r, n) => {
402
- const { secondLastStepIndex: d } = l(r.entries, n.experienceId), h = d !== -1 ? r.entries.slice(0, d + 1) : r.entries;
402
+ const { secondLastStepIndex: d } = l(r.entries, n.experienceId), y = d !== -1 ? r.entries.slice(0, d + 1) : r.entries;
403
403
  return {
404
404
  ...r,
405
- entries: h
405
+ entries: y
406
406
  };
407
407
  }), {
408
408
  push: e,
@@ -413,9 +413,9 @@ const Oe = k(() => {
413
413
  };
414
414
  });
415
415
  function Me() {
416
- return x(Oe);
416
+ return b(Oe);
417
417
  }
418
- const Te = () => C(), Ve = b("field"), Ne = k(() => {
418
+ const Te = () => C(), Ve = x("field"), Ne = k(() => {
419
419
  const t = Ve.store({}), e = I(t, {
420
420
  register(r, n) {
421
421
  return {
@@ -442,11 +442,11 @@ const Te = () => C(), Ve = b("field"), Ne = k(() => {
442
442
  };
443
443
  });
444
444
  function Le() {
445
- return x(Ne);
445
+ return b(Ne);
446
446
  }
447
447
  const Ue = (t, e) => {
448
448
  e.register(t);
449
- }, ze = b("response"), _e = k(({ client: t }) => {
449
+ }, ze = x("response"), _e = k(({ client: t }) => {
450
450
  const e = ze.store({}), s = C(), o = C(), i = I(e, {
451
451
  register(a, p) {
452
452
  return {
@@ -543,7 +543,7 @@ const Ue = (t, e) => {
543
543
  const { responsesToCommit: p, validationErrors: g } = await r(a);
544
544
  return g.length ? { errors: g } : (i.registerBatch(p), { errors: [] });
545
545
  }
546
- const d = e.map((a) => a), h = e.map((a) => Object.keys(a).map((g) => ({
546
+ const d = e.map((a) => a), y = e.map((a) => Object.keys(a).map((g) => ({
547
547
  fieldId: g,
548
548
  value: a[g]
549
549
  })));
@@ -570,16 +570,16 @@ const Ue = (t, e) => {
570
570
  clear: o,
571
571
  commit: n,
572
572
  getResponsesToCommit: r,
573
- listStore: h,
573
+ listStore: y,
574
574
  getAllStore: d,
575
575
  getByFieldId: c,
576
576
  getByFieldName: f
577
577
  };
578
578
  });
579
579
  function He(t) {
580
- return x(_e, t);
580
+ return b(_e, t);
581
581
  }
582
- const Ke = () => "0.0.10-next.1";
582
+ const Ke = () => "0.0.10-next.2";
583
583
  function qe() {
584
584
  if (z()) {
585
585
  const t = [
@@ -595,7 +595,7 @@ function qe() {
595
595
  }
596
596
  return "https://api.picked.app";
597
597
  }
598
- function We() {
598
+ function Ge() {
599
599
  if (!ne())
600
600
  return {};
601
601
  const t = new URLSearchParams(window.location.search), e = {}, s = /* @__PURE__ */ new Set();
@@ -615,53 +615,50 @@ function We() {
615
615
  }
616
616
  return e;
617
617
  }
618
- function Ge() {
618
+ function We() {
619
619
  if (ie())
620
620
  return crypto.randomUUID();
621
621
  const t = Date.now().toString(36), e = Math.random().toString(36).substring(2, 15), s = Math.random().toString(36).substring(2, 15), o = Math.random().toString(36).substring(2, 15);
622
622
  return `${t}-${e}-${s}-${o}`;
623
623
  }
624
- const Je = () => ({
625
- id: Ge()
626
- }), Ye = () => {
624
+ const Ye = () => ({
625
+ id: We()
626
+ }), Je = $("session", "global"), Qe = () => {
627
627
  if (!oe()) return null;
628
- const t = D.get("session");
628
+ const t = D.get(Je);
629
629
  let e = t.isSet() ? t.json() : null;
630
- return e || (e = Je(), t.set(e)), e;
631
- }, Qe = (t) => {
632
- const e = Ye();
633
- return e && t.eventBus({ type: "session", ...e }), e;
634
- };
630
+ return e || (e = Ye(), t.set(e)), e;
631
+ }, Xe = () => Qe();
635
632
  class K {
636
633
  constructor(e) {
637
- y(this, "options");
638
- y(this, "isReady", !1);
639
- y(this, "project");
640
- y(this, "plugins", {});
641
- y(this, "eventBus", Te());
642
- y(this, "organizationId");
643
- y(this, "projectId");
644
- y(this, "apiUrl");
645
- y(this, "session");
646
- y(this, "searchParams", We());
647
- y(this, "history", Me());
648
- y(this, "components", we({ client: this }));
649
- y(this, "experienceInstances", ee({ client: this }));
650
- y(this, "experiences", de({ client: this }));
651
- y(this, "frameworks", xe({ client: this }));
652
- y(this, "predicates", Be({ client: this }));
653
- y(this, "features", Ce());
654
- y(this, "responses", He({ client: this }));
655
- y(this, "fields", Le());
656
- v("info", "Picked client initializing."), this.organizationId = e.organizationId, this.projectId = e.projectId, this.options = Object.freeze(e), this.apiUrl = e.apiUrl || qe(), this.session = Qe(this), this.setupPlugins(), this.setupFeatures(), this.setupPredicates(), this.eventBus({ type: "initialized", version: Ke() }), v("info", "Picked client initialized.");
634
+ h(this, "options");
635
+ h(this, "isReady", !1);
636
+ h(this, "project");
637
+ h(this, "plugins", {});
638
+ h(this, "eventBus", Te());
639
+ h(this, "organizationId");
640
+ h(this, "projectId");
641
+ h(this, "apiUrl");
642
+ h(this, "session");
643
+ h(this, "searchParams", Ge());
644
+ h(this, "history", Me());
645
+ h(this, "components", we({ client: this }));
646
+ h(this, "experienceInstances", ee({ client: this }));
647
+ h(this, "experiences", de({ client: this }));
648
+ h(this, "frameworks", be({ client: this }));
649
+ h(this, "predicates", Be({ client: this }));
650
+ h(this, "features", Ce());
651
+ h(this, "responses", He({ client: this }));
652
+ h(this, "fields", Le());
653
+ v("info", "Picked client initializing."), this.organizationId = e.organizationId, this.projectId = e.projectId, this.options = Object.freeze(e), this.apiUrl = e.apiUrl || qe(), this.session = Xe(), this.setupPlugins(), this.setupFeatures(), this.setupPredicates(), this.eventBus({ type: "initialized", version: Ke() }), v("info", "Picked client initialized.");
657
654
  }
658
655
  start() {
659
656
  Ee(this), (async () => {
660
- z() && (document.body && await M(this), await te(), await M(this), this.eventBus({ type: "ready" }), this.isReady = !0);
657
+ z() && (document.body && M(this), await te(), await M(this), this.eventBus({ type: "ready" }), this.isReady = !0, this.session && this.eventBus({ type: "session", ...this.session }));
661
658
  })();
662
659
  }
663
660
  registerFramework(e) {
664
- be(e, this.frameworks);
661
+ xe(e, this.frameworks);
665
662
  }
666
663
  registerComponent(e, s) {
667
664
  T(e, s, this.components);
@@ -755,16 +752,16 @@ class K {
755
752
  return this.plugins[e] ?? {};
756
753
  }
757
754
  }
758
- y(K, "logger", v);
755
+ h(K, "logger", v);
759
756
  function q(t) {
760
757
  var e;
761
758
  return ((e = t.options) == null ? void 0 : e.some((s) => "condition" in s)) ?? !1;
762
759
  }
763
- function Xe(t) {
760
+ function Ze(t) {
764
761
  return t.some((e) => e.condition || q(e));
765
762
  }
766
- async function ct(t, e) {
767
- return e.length === 0 || !Xe(e) ? e : (await Promise.all(
763
+ async function ut(t, e) {
764
+ return e.length === 0 || !Ze(e) ? e : (await Promise.all(
768
765
  e.map(async (o) => {
769
766
  if (!(o.condition ? await R(t, o.condition) : !0))
770
767
  return null;
@@ -777,29 +774,29 @@ async function ct(t, e) {
777
774
  })
778
775
  )).filter((o) => o !== null);
779
776
  }
780
- const ut = (t) => {
777
+ const lt = (t) => {
781
778
  const e = new K(t);
782
779
  return ae(e), e;
783
780
  };
784
781
  export {
785
782
  K as Picked,
786
- ut as createClient,
783
+ lt as createClient,
787
784
  $ as createStorageKey,
788
785
  R as evaluateRuleLogic,
789
- ct as getStepFields,
790
- pt as isPickedRoot,
786
+ ut as getStepFields,
787
+ gt as isPickedRoot,
791
788
  D as localStore,
792
789
  B as sessionStore,
793
- ft as subscribe,
790
+ mt as subscribe,
794
791
  yt as supportsCookies,
795
792
  ie as supportsCryptoRandomUUID,
796
793
  z as supportsDocument,
797
- ht as supportsHistory,
798
- St as supportsIntersectionObserver,
794
+ St as supportsHistory,
795
+ wt as supportsIntersectionObserver,
799
796
  oe as supportsLocalStorage,
800
- wt as supportsLocation,
801
- vt as supportsSessionStorage,
802
- kt as supportsShadowRoot,
797
+ vt as supportsLocation,
798
+ kt as supportsSessionStorage,
799
+ bt as supportsShadowRoot,
803
800
  xt as supportsWebComponents,
804
801
  ne as supportsWindow
805
802
  };
@@ -1 +1 @@
1
- {"version":3,"file":"picked.d.ts","sourceRoot":"","sources":["../../src/picked.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,UAAU,EAIhB,MAAM,kCAAkC,CAAA;AACzC,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,iBAAiB,EAAe,MAAM,0BAA0B,CAAA;AAC3F,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,UAAU,CAAA;AACnD,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EAGtB,MAAM,gCAAgC,CAAA;AACvC,OAAO,EAEL,KAAK,SAAS,EAEf,MAAM,gCAAgC,CAAA;AAGvC,OAAO,EAAuB,KAAK,aAAa,EAAE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AAmB;IAmBxC,KAAK,IAAI,IAAI;IAkCb,iBAAiB,CAAC,SAAS,EAAE,SAAS;IAItC,iBAAiB,CAAC,IAAI,EAAE,MAAM,mBAAmB,EAAE,SAAS,EAAE,gBAAgB;IAI9E,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIpC,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IAI9C,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS;IAIpD,aAAa,CAAC,KAAK,EAAE,KAAK;IAI1B,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI;IAQhD,gBAAgB,IAAI,aAAa;IAI3B,oBAAoB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB/D,OAAO,CAAC,YAAY;IA6BpB,OAAO,CAAC,aAAa;IA8CrB,OAAO,CAAC,eAAe;IAgBvB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,iBAAiB,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAGzF"}
@@ -1,6 +1,5 @@
1
- import { Picked } from '../picked';
2
1
  export interface Session {
3
2
  id: string;
4
3
  }
5
- export declare const initialiseSession: (client: Picked) => Session | null;
4
+ export declare const initialiseSession: () => Session | null;
6
5
  //# sourceMappingURL=user-session.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"user-session.d.ts","sourceRoot":"","sources":["../../../src/utils/user-session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAIvC,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAA;CACX;AAsCD,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,mBAQ/C,CAAA"}
1
+ {"version":3,"file":"user-session.d.ts","sourceRoot":"","sources":["../../../src/utils/user-session.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAA;CACX;AAwCD,eAAO,MAAM,iBAAiB,sBAE7B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@picked/client",
3
- "version": "0.0.10-next.1",
3
+ "version": "0.0.10-next.2",
4
4
  "dependencies": {
5
5
  "@withease/factories": "^1.0.5"
6
6
  },