@spiffcommerce/core 16.4.0-rc.1 → 16.4.0

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/index.js CHANGED
@@ -8903,12 +8903,13 @@ class Ie {
8903
8903
  }
8904
8904
  async injectExperienceIntoPreviewService(A) {
8905
8905
  if (this.previewService) {
8906
+ const e = A.getWorkflowManager().injectIntoPreviewService(this.previewService);
8906
8907
  try {
8907
8908
  this.bundleStateManager.applyStateToWorkflowExperience(A);
8908
- } catch (e) {
8909
- console.error(`Unable to apply state to workflow experience: ${e}`);
8909
+ } catch (t) {
8910
+ console.error(`Unable to apply state to workflow experience: ${t}`);
8910
8911
  }
8911
- await A.getWorkflowManager().injectIntoPreviewService(this.previewService);
8912
+ await e;
8912
8913
  }
8913
8914
  }
8914
8915
  async loadExistingBundles(A, e) {
@@ -2245,7 +2245,7 @@ IfnI8vaNAAAAAElFTkSuQmCC" transform="matrix(0.13 0.0141 -0.0141 0.1301 104.926 1
2245
2245
  }
2246
2246
  }
2247
2247
  }
2248
- `;class en{constructor(e,t){this.bundleId=e,this.initPromise=this.getOrCreateGlobalPropertyState().then(A=>{this.globalPropertyState=A}),this.onGlobalPropertyStateChange=t}getInitializationPromise(){return this.initPromise}getGlobalPropertyState(){return this.globalPropertyState}getAspect(e){if(!this.globalPropertyState)throw new Error("Global property state not initialized");const t=this.globalPropertyState.aspects.find(A=>A.name===e);if(t)return t.value}async setAspect(e,t){if(!this.globalPropertyState)throw new Error("Global property state not initialized");const A=te(this.globalPropertyState),a=this.globalPropertyState.aspects.find(n=>n.name===e);a?a.value=t:this.globalPropertyState.aspects.push({name:e,value:t}),await this.updateGlobalPropertyState(),await this.onGlobalPropertyStateChange(A,this.globalPropertyState)}async updateGlobalPropertyState(){var t;if(!this.globalPropertyState)throw new Error("Global property state not initialized");const e=await P.getShadowGraphqlClient().mutate({mutation:$a,variables:{id:this.globalPropertyState.id,aspects:this.globalPropertyState.aspects}});if((t=e.data)!=null&&t.globalPropertyStateUpdate)this.globalPropertyState=e.data.globalPropertyStateUpdate;else throw new Error("Unable to update global property state")}async getOrCreateGlobalPropertyState(){var A;const e=await P.getShadowGraphqlClient().query({query:_a,errorPolicy:"all",fetchPolicy:"no-cache",variables:{bundleId:this.bundleId}});if(e.data.globalPropertyState&&e.data.globalPropertyState.id)return e.data.globalPropertyState;const t=await P.getShadowGraphqlClient().mutate({mutation:Za,variables:{bundleId:this.bundleId}});if((A=t.data)!=null&&A.globalPropertyStateCreate&&t.data.globalPropertyStateCreate.id)return t.data.globalPropertyStateCreate;throw new Error(`Unable to create global property state for bundle: ${this.bundleId}`)}}class tn{constructor(e){this.setState(e)}setState(e){e&&(this.state=JSON.parse(e))}applyStateToWorkflowExperience(e){var n,i;if(!this.state||!((n=e==null?void 0:e.getWorkflowManager())!=null&&n.getModelContainer()))return;const t=(i=this.state.transactions.find(o=>o.transactionId===e.getWorkflowManager().getTransaction().id&&o.transform))==null?void 0:i.transform;if(!t)return;const A=this.flipTransform(t,e.getWorkflowManager().getPreviewService()),a=e.getWorkflowManager().getModelContainer();a.position=A.position,a.rotation=A.rotation,a.scale=A.scale}setWorkflowExperienceTransform(e,t){var o;if(!((o=e==null?void 0:e.getWorkflowManager())!=null&&o.getModelContainer()))return;const A=this.flipTransform(t,e.getWorkflowManager().getPreviewService()),a=e.getWorkflowManager().getModelContainer();a.position=A.position,a.rotation=A.rotation,a.scale=A.scale;const n=e.getWorkflowManager().getTransaction().id;this.state?this.state.transactions||(this.state.transactions=[]):this.state={transactions:[]};const i=this.state.transactions.find(s=>s.transactionId===n);i?i.transform=t:this.state.transactions.push({transactionId:n,transform:t})}removeStateForTransaction(e){if(!this.state||!this.state.transactions)return;const t=this.state.transactions.findIndex(A=>A.transactionId===e);t!==-1&&this.state.transactions.splice(t,1)}getSerializedState(){if(this.state)return JSON.stringify(this.state)}flipTransform(e,t){return t&&t.flipTransform?t.flipTransform(e.position,{x:e.rotation.x*Math.PI/180,y:e.rotation.y*Math.PI/180,z:e.rotation.z*Math.PI/180},e.scale):{position:e.position,rotation:{x:e.rotation.x*Math.PI/180,y:e.rotation.y*Math.PI/180,z:e.rotation.z*Math.PI/180},scale:e.scale}}}class _t{constructor(e){this.collection=e}getId(){return this.collection.id}getName(){return this.collection.name}getProducts(){return this.collection.productCollectionProducts.map(e=>new eA(e))}getTransformCollection(){if(this.collection.transformCollection)return new tA(this.collection.transformCollection)}getResource(){return this.collection}}class eA{constructor(e){this.product=e.product,this.productResource=e}getId(){return this.product.id}getName(){return this.product.name}getIntegrationByType(e){const A=(this.product.integrationProducts||[]).find(a=>{var n,i;return!!((n=a.integration)!=null&&n.type)&&((i=a.integration)==null?void 0:i.type)===e});if(!A)throw new Error("Failed to find requested integration type on product. This is generally due to a configuration error");return A}getDefaultWorkflow(){const e=this.product.workflows||[];if(e.length===0)throw new U("No workflows found on product. This is generally due to a configuration error. Please confirm at least one workflow is configured for this product.");const t=this.productResource.workflowId;if(t){const a=e.find(n=>n.workflowName===t);if(a)return new Ke(a)}const A=e[0];return new Ke(A)}getIntegrations(){return this.product.integrationProducts||[]}getResource(){return this.product}}class Ke{constructor(e){this.workflow=e}getId(){return this.workflow.workflowName}getName(){return this.workflow.friendlyName}getThumbnail(){return this.workflow.imageUrl}}class tA{constructor(e){this.collection=e}getId(){return this.collection.id}getName(){return this.collection.name}getTransforms(){return this.collection.transforms.map(e=>new AA(e))}}class AA{constructor(e){this.transform=e}getId(){return this.transform.id}getName(){return this.transform.name}get(){return{position:this.transform.position,rotation:this.transform.rotation,scale:this.transform.scale}}}class aA{constructor(e,t,A,a){var n,i,o;this.eventListeners=new Map,this.workflowExperiences=[],this.client=e,this.id=t.id,this.name=t.name||"",this.metadata=new Map(((n=t.metadata)==null?void 0:n.map(s=>[s.key,s.value]))||[]),this.productCollection=t.productCollection,this.bundleStateManager=new tn(t.bundleStateData),this.globalConfiguration=(i=this.productCollection)==null?void 0:i.globalPropertyConfiguration,this.globalPropertyStateManager=new en(this.id,this.checkConditionalHandlesChanged.bind(this)),this.globalPropertyHandleService=new Va(this),this.setPreviewService(A),this.initializationPromise=this.loadExistingBundles(((o=t.transactions)==null?void 0:o.map(s=>s.id))||[],a)}getInitializationPromise(){return this.initializationPromise}addEventListener(e,t){this.eventListeners.set(e,[...this.eventListeners.get(e)||[],t])}removeEventListener(e,t){const A=this.eventListeners.get(e)||[],a=A.indexOf(t);a>-1&&A.splice(a,1)}getClient(){return this.client}getId(){return this.id}getName(){return this.name}setName(e){return this.name=e,this.updateBundle()}getMetadata(){return new Map(this.metadata)}setMetadata(e){return this.metadata=new Map(e),this.updateBundle()}setNameAndMetadata(e,t){return this.name=e,this.metadata=new Map(t),this.updateBundle()}hasGlobalProperties(){return!!this.globalConfiguration&&this.globalConfiguration.aspects.length>0}async getGlobalProperties(){return this.globalPropertyHandleService.applyConditionsFromState(await this.globalPropertyHandleService.getHandles(),this.getGlobalPropertyStateManager().getGlobalPropertyState())}getGlobalPropertyStateManager(){return this.globalPropertyStateManager}async getGlobalPropertyTotalSubunits(){return(await this.globalPropertyHandleService.getHandles()).filter(A=>A.getType()===r.AspectType.Option).map(A=>{var a;return((a=A.getCurrentVariant())==null?void 0:a.getPrice())||0}).reduce((A,a)=>A+a,0)}getTotalSubunits(){return this.workflowExperiences.map(e=>e.getTotalPriceSubunits()).reduce((e,t)=>e+t,0)}getProductCollection(){return this.productCollection?new _t(this.productCollection):void 0}async addWorkflowExperience(e){await this.appendWorkflowExperience(e),await this.injectExperienceIntoPreviewService(e);const t=await this.getGlobalProperties();await Promise.all(t.map(A=>A.applyGlobalState([e])))}async addWorkflowExperiences(e){const t=e.map(i=>i.getWorkflowManager().getTransaction().id),A=t.map(i=>this.workflowExperiences.find(o=>o.getWorkflowManager().getTransaction().id===i));if(A.some(i=>!!i))throw new Error("Unable to add transaction to bundle - Already Exists: "+A.filter(i=>!!i).map(i=>i==null?void 0:i.getWorkflowManager().getTransaction().id).join(", "));const a=N.getMap("transactionOwnerIds")||new Map,n=t.map(i=>a.get(i));await P.getShadowGraphqlClient().mutate({mutation:Ja,variables:{id:this.id,transactionIds:t,transactionOwnerIds:n}}).catch(i=>{console.error(i)}),this.workflowExperiences.push(...e),await Promise.all([this.updateTransactionOrder(),(async()=>{for(const o of e)await this.injectExperienceIntoPreviewService(o);const i=await this.getGlobalProperties();await Promise.all(i.map(o=>o.applyGlobalState(e)))})()])}async appendWorkflowExperience(e,t=!0){const A=e.getWorkflowManager().getTransaction().id;if(this.workflowExperiences.find(o=>o.getWorkflowManager().getTransaction().id===A))throw new Error("Unable to add transaction to bundle - Already Exists!");const i=(N.getMap("transactionOwnerIds")||new Map).get(A);await P.getShadowGraphqlClient().mutate({mutation:Ta,variables:{id:this.id,transactionId:A},context:{transactionOwnerId:i}}),t&&this.workflowExperiences.push(e)}async removeWorkflowExperience(e){const t=this.workflowExperiences.indexOf(e);await this.removeTransaction(t,this.workflowExperiences[t].getWorkflowManager().getTransaction())}async removeWorkflowExperiences(e){await this.removeTransactions(e.map(t=>t.getWorkflowManager().getTransaction()))}async removeWorkflowExperienceByTransaction(e){const t=this.workflowExperiences.findIndex(A=>A.getWorkflowManager().getTransaction().id===e.id);await this.removeTransaction(t,e)}async removeWorkflowExperiencesByTransactions(e){await this.removeTransactions(e)}async removeTransaction(e,t,A=!0){if(e>-1){const a=this.workflowExperiences.find(n=>n.getWorkflowManager().getTransaction().id===t.id);this.previewService&&a.getWorkflowManager().ejectFromPreviewService(),this.bundleStateManager.removeStateForTransaction(t.id),await P.getShadowGraphqlClient().mutate({mutation:La,variables:{id:this.id,transactionId:t.id,name:this.name,metadata:Array.from(this.metadata.entries()).map(([n,i])=>({key:n,value:i})),bundleStateData:this.bundleStateManager.getSerializedState()}}),A&&(this.workflowExperiences.splice(e,1),await this.updateTransactionOrder())}else throw new Error("Unable to remove workflow experience from bundle - Not Found! - "+t.id)}async removeTransactions(e){const t=e.map(a=>this.workflowExperiences.findIndex(n=>n.getWorkflowManager().getTransaction().id===a.id));if(t.some(a=>a===-1))throw new Error("Unable to remove workflow experience from bundle - The following aren't included in the bundle: "+e.filter((a,n)=>t[n]===-1).map(a=>a.id).join(", "));const A=e.map(a=>this.workflowExperiences.splice(this.workflowExperiences.findIndex(n=>n.getWorkflowManager().getTransaction().id===a.id),1)[0]);this.previewService&&A.forEach(a=>a.getWorkflowManager().ejectFromPreviewService()),e.forEach(a=>this.bundleStateManager.removeStateForTransaction(a.id)),await P.getShadowGraphqlClient().mutate({mutation:Oa,variables:{id:this.id,transactionIds:e.map(a=>a.id),name:this.name,metadata:Array.from(this.metadata.entries()).map(([a,n])=>({key:a,value:n})),bundleStateData:this.bundleStateManager.getSerializedState()}}),await this.updateTransactionOrder()}async insertWorkflowExperience(e,t){await this.appendWorkflowExperience(e,!1),this.workflowExperiences.splice(t,0,e),await Promise.all([this.updateTransactionOrder(),(async()=>{await this.injectExperienceIntoPreviewService(e);const A=await this.getGlobalProperties();await Promise.all(A.map(a=>a.applyGlobalState([e])))})()])}async replaceWorkflowExperience(e,t){if(e<0||e>=this.workflowExperiences.length)throw new Error("Unable to replace workflow experience in bundle - Index out of range!");const A=this.workflowExperiences[e];if(A.getWorkflowManager().getTransaction().id===t.getWorkflowManager().getTransaction().id)throw new Error("Unable to replace workflow experience in bundle - Same transaction!");await Promise.all([this.removeTransaction(e,A.getWorkflowManager().getTransaction(),!1),this.appendWorkflowExperience(t,!1)]),this.workflowExperiences[e]=t,await Promise.all([this.updateTransactionOrder(),(async()=>{await this.injectExperienceIntoPreviewService(t);const a=await this.getGlobalProperties();await Promise.all(a.map(n=>n.applyGlobalState([t])))})()])}async swapWorkflowExperiences(e,t){if(e<0||e>=this.workflowExperiences.length||t<0||t>=this.workflowExperiences.length)throw new Error("Unable to swap workflow experiences in bundle - Index out of range!");if(e===t)return;const A=this.workflowExperiences[e];this.workflowExperiences[e]=this.workflowExperiences[t],this.workflowExperiences[t]=A,await this.updateTransactionOrder()}getWorkflowExperiences(){return[...this.workflowExperiences]}async sortWorkflowExperiences(e){this.workflowExperiences=this.workflowExperiences.sort(e),await this.updateTransactionOrder()}getWorkflowExperienceCount(){return this.workflowExperiences.length}async addStakeholder(e,t){var n;const A=await P.getShadowGraphqlClient().mutate({mutation:za,variables:{id:this.id,details:e,type:t||re.Owner}});if(!((n=A.data)!=null&&n.bundleAddStakeholder))throw new Error("Bundle not found!");const a=A.data.bundleAddStakeholder.bundleStakeholders||[];this.storeStakeholderCustomers(a)}async updateStakeholders(e){var a;const t=await P.getShadowGraphqlClient().mutate({mutation:ja,variables:{id:this.id,input:e}});if(!((a=t.data)!=null&&a.bundleUpdateStakeholders))throw new Error("Bundle not found!");const A=t.data.bundleUpdateStakeholders.bundleStakeholders||[];this.storeStakeholderCustomers(A)}async getAllStakeholders(){const e=await P.getShadowGraphqlClient().query({query:Ga,errorPolicy:"all",variables:{id:this.id}});if(!e.data.bundles||e.data.bundles.length===0)throw new Error("Bundle not found!");const t=e.data.bundles[0].bundleStakeholders||[];return this.storeStakeholderCustomers(t),t}storeStakeholderCustomers(e){e.forEach(t=>{t.customer&&this.client.storeCustomer(t.customer)})}async finish(e){return await Qa(this.workflowExperiences.map(t=>({workflowManager:t.getWorkflowManager(),workflow:t.getWorkflowManager().getWorkflow(),layouts:t.getWorkflowManager().getLayouts(),getReducerState:()=>t.getCommandContext().getState(),product:t.getWorkflowManager().getProduct(),transaction:t.getWorkflowManager().getTransaction(),workflowSelections:t.getWorkflowManager().getWorkflowSelections(),designName:t.getWorkflowManager().getWorkflow().name,variationRecords:t.getWorkflowManager().getVariationRecords(),updateVariationRecords:A=>t.updateVariationRecords(A),workflowMetadata:t.getWorkflowManager().getWorkflowMetadata()})),e??(()=>{}))}getPreviewService(){return this.previewService}async setPreviewService(e){if(this.workflowExperiences&&this.workflowExperiences.length>0&&(this.workflowExperiences.forEach(t=>t.getWorkflowManager().ejectFromPreviewService()),e))for(const t of this.workflowExperiences)await this.injectExperienceIntoPreviewService(t);e&&e.registerModelLoadEventListener&&e.registerModelLoadEventListener(this.onModelLoadEvent.bind(this)),this.previewService&&this.previewService!==e&&this.previewService.unregisterModelLoadEventListener&&this.previewService.unregisterModelLoadEventListener(this.onModelLoadEvent.bind(this)),this.previewService=e}setWorkflowExperienceTransform(e,t){return this.bundleStateManager.setWorkflowExperienceTransform(e,t),this.updateBundle()}onModelLoadEvent(e){e.eventType==="unload"||!e.modelContainer||(e.modelContainer.registerMaterialSelectedCallback(()=>this.onModelSelectedEvent(e.modelContainer,!0)),e.modelContainer.registerMaterialDeselectedCallback(()=>this.onModelSelectedEvent(e.modelContainer,!1)))}onModelSelectedEvent(e,t){if(e&&e.metadata&&e.metadata instanceof Map&&e.metadata.has("workflowManager")){const a=e.metadata.get("workflowManager").getTransaction(),n=this.workflowExperiences.find(i=>i.getWorkflowManager().getTransaction().id===a.id);n&&this.fireEvent(t?"workflow-experience-hover-enter":"workflow-experience-hover-exit",{workflowExperience:n})}}async injectExperienceIntoPreviewService(e){if(this.previewService){try{this.bundleStateManager.applyStateToWorkflowExperience(e)}catch(t){console.error(`Unable to apply state to workflow experience: ${t}`)}await e.getWorkflowManager().injectIntoPreviewService(this.previewService)}}async loadExistingBundles(e,t){if(e.length===0)return;const A=await this.client.getWorkflowExperiences(e.map(a=>({type:"transaction",transactionId:a})),t);if(this.workflowExperiences=A,this.previewService)for(const a of A)await this.injectExperienceIntoPreviewService(a)}async updateBundle(){var t;if(!((t=(await P.getShadowGraphqlClient().mutate({mutation:ba,variables:{id:this.id,name:this.name,metadata:Array.from(this.metadata.entries()).map(([A,a])=>({key:A,value:a})),bundleStateData:this.bundleStateManager.getSerializedState()}})).data)!=null&&t.bundleUpdate))throw new Error("Bundle not found!")}async updateTransactionOrder(){await P.getShadowGraphqlClient().mutate({mutation:Ka,variables:{id:this.id,transactionIds:this.workflowExperiences.map(e=>e.getWorkflowManager().getTransaction().id)}})}async checkConditionalHandlesChanged(e,t){const A=await this.globalPropertyHandleService.getHandles(),a=this.globalPropertyHandleService.applyConditionsFromState(A,e),n=this.globalPropertyHandleService.applyConditionsFromState(A,t);(()=>{if(a.length!==n.length)return!0;for(let o=0;o<a.length;o++)if(a[o].getName()!==n[o].getName())return!0;return!1})()&&this.fireEvent("conditional-global-properties-changed",{globalProperties:n})}fireEvent(e,t){(this.eventListeners.get(e)||[]).forEach(a=>a({bundle:this,event:e,data:t}))}}const An=M.gql`
2248
+ `;class en{constructor(e,t){this.bundleId=e,this.initPromise=this.getOrCreateGlobalPropertyState().then(A=>{this.globalPropertyState=A}),this.onGlobalPropertyStateChange=t}getInitializationPromise(){return this.initPromise}getGlobalPropertyState(){return this.globalPropertyState}getAspect(e){if(!this.globalPropertyState)throw new Error("Global property state not initialized");const t=this.globalPropertyState.aspects.find(A=>A.name===e);if(t)return t.value}async setAspect(e,t){if(!this.globalPropertyState)throw new Error("Global property state not initialized");const A=te(this.globalPropertyState),a=this.globalPropertyState.aspects.find(n=>n.name===e);a?a.value=t:this.globalPropertyState.aspects.push({name:e,value:t}),await this.updateGlobalPropertyState(),await this.onGlobalPropertyStateChange(A,this.globalPropertyState)}async updateGlobalPropertyState(){var t;if(!this.globalPropertyState)throw new Error("Global property state not initialized");const e=await P.getShadowGraphqlClient().mutate({mutation:$a,variables:{id:this.globalPropertyState.id,aspects:this.globalPropertyState.aspects}});if((t=e.data)!=null&&t.globalPropertyStateUpdate)this.globalPropertyState=e.data.globalPropertyStateUpdate;else throw new Error("Unable to update global property state")}async getOrCreateGlobalPropertyState(){var A;const e=await P.getShadowGraphqlClient().query({query:_a,errorPolicy:"all",fetchPolicy:"no-cache",variables:{bundleId:this.bundleId}});if(e.data.globalPropertyState&&e.data.globalPropertyState.id)return e.data.globalPropertyState;const t=await P.getShadowGraphqlClient().mutate({mutation:Za,variables:{bundleId:this.bundleId}});if((A=t.data)!=null&&A.globalPropertyStateCreate&&t.data.globalPropertyStateCreate.id)return t.data.globalPropertyStateCreate;throw new Error(`Unable to create global property state for bundle: ${this.bundleId}`)}}class tn{constructor(e){this.setState(e)}setState(e){e&&(this.state=JSON.parse(e))}applyStateToWorkflowExperience(e){var n,i;if(!this.state||!((n=e==null?void 0:e.getWorkflowManager())!=null&&n.getModelContainer()))return;const t=(i=this.state.transactions.find(o=>o.transactionId===e.getWorkflowManager().getTransaction().id&&o.transform))==null?void 0:i.transform;if(!t)return;const A=this.flipTransform(t,e.getWorkflowManager().getPreviewService()),a=e.getWorkflowManager().getModelContainer();a.position=A.position,a.rotation=A.rotation,a.scale=A.scale}setWorkflowExperienceTransform(e,t){var o;if(!((o=e==null?void 0:e.getWorkflowManager())!=null&&o.getModelContainer()))return;const A=this.flipTransform(t,e.getWorkflowManager().getPreviewService()),a=e.getWorkflowManager().getModelContainer();a.position=A.position,a.rotation=A.rotation,a.scale=A.scale;const n=e.getWorkflowManager().getTransaction().id;this.state?this.state.transactions||(this.state.transactions=[]):this.state={transactions:[]};const i=this.state.transactions.find(s=>s.transactionId===n);i?i.transform=t:this.state.transactions.push({transactionId:n,transform:t})}removeStateForTransaction(e){if(!this.state||!this.state.transactions)return;const t=this.state.transactions.findIndex(A=>A.transactionId===e);t!==-1&&this.state.transactions.splice(t,1)}getSerializedState(){if(this.state)return JSON.stringify(this.state)}flipTransform(e,t){return t&&t.flipTransform?t.flipTransform(e.position,{x:e.rotation.x*Math.PI/180,y:e.rotation.y*Math.PI/180,z:e.rotation.z*Math.PI/180},e.scale):{position:e.position,rotation:{x:e.rotation.x*Math.PI/180,y:e.rotation.y*Math.PI/180,z:e.rotation.z*Math.PI/180},scale:e.scale}}}class _t{constructor(e){this.collection=e}getId(){return this.collection.id}getName(){return this.collection.name}getProducts(){return this.collection.productCollectionProducts.map(e=>new eA(e))}getTransformCollection(){if(this.collection.transformCollection)return new tA(this.collection.transformCollection)}getResource(){return this.collection}}class eA{constructor(e){this.product=e.product,this.productResource=e}getId(){return this.product.id}getName(){return this.product.name}getIntegrationByType(e){const A=(this.product.integrationProducts||[]).find(a=>{var n,i;return!!((n=a.integration)!=null&&n.type)&&((i=a.integration)==null?void 0:i.type)===e});if(!A)throw new Error("Failed to find requested integration type on product. This is generally due to a configuration error");return A}getDefaultWorkflow(){const e=this.product.workflows||[];if(e.length===0)throw new U("No workflows found on product. This is generally due to a configuration error. Please confirm at least one workflow is configured for this product.");const t=this.productResource.workflowId;if(t){const a=e.find(n=>n.workflowName===t);if(a)return new Ke(a)}const A=e[0];return new Ke(A)}getIntegrations(){return this.product.integrationProducts||[]}getResource(){return this.product}}class Ke{constructor(e){this.workflow=e}getId(){return this.workflow.workflowName}getName(){return this.workflow.friendlyName}getThumbnail(){return this.workflow.imageUrl}}class tA{constructor(e){this.collection=e}getId(){return this.collection.id}getName(){return this.collection.name}getTransforms(){return this.collection.transforms.map(e=>new AA(e))}}class AA{constructor(e){this.transform=e}getId(){return this.transform.id}getName(){return this.transform.name}get(){return{position:this.transform.position,rotation:this.transform.rotation,scale:this.transform.scale}}}class aA{constructor(e,t,A,a){var n,i,o;this.eventListeners=new Map,this.workflowExperiences=[],this.client=e,this.id=t.id,this.name=t.name||"",this.metadata=new Map(((n=t.metadata)==null?void 0:n.map(s=>[s.key,s.value]))||[]),this.productCollection=t.productCollection,this.bundleStateManager=new tn(t.bundleStateData),this.globalConfiguration=(i=this.productCollection)==null?void 0:i.globalPropertyConfiguration,this.globalPropertyStateManager=new en(this.id,this.checkConditionalHandlesChanged.bind(this)),this.globalPropertyHandleService=new Va(this),this.setPreviewService(A),this.initializationPromise=this.loadExistingBundles(((o=t.transactions)==null?void 0:o.map(s=>s.id))||[],a)}getInitializationPromise(){return this.initializationPromise}addEventListener(e,t){this.eventListeners.set(e,[...this.eventListeners.get(e)||[],t])}removeEventListener(e,t){const A=this.eventListeners.get(e)||[],a=A.indexOf(t);a>-1&&A.splice(a,1)}getClient(){return this.client}getId(){return this.id}getName(){return this.name}setName(e){return this.name=e,this.updateBundle()}getMetadata(){return new Map(this.metadata)}setMetadata(e){return this.metadata=new Map(e),this.updateBundle()}setNameAndMetadata(e,t){return this.name=e,this.metadata=new Map(t),this.updateBundle()}hasGlobalProperties(){return!!this.globalConfiguration&&this.globalConfiguration.aspects.length>0}async getGlobalProperties(){return this.globalPropertyHandleService.applyConditionsFromState(await this.globalPropertyHandleService.getHandles(),this.getGlobalPropertyStateManager().getGlobalPropertyState())}getGlobalPropertyStateManager(){return this.globalPropertyStateManager}async getGlobalPropertyTotalSubunits(){return(await this.globalPropertyHandleService.getHandles()).filter(A=>A.getType()===r.AspectType.Option).map(A=>{var a;return((a=A.getCurrentVariant())==null?void 0:a.getPrice())||0}).reduce((A,a)=>A+a,0)}getTotalSubunits(){return this.workflowExperiences.map(e=>e.getTotalPriceSubunits()).reduce((e,t)=>e+t,0)}getProductCollection(){return this.productCollection?new _t(this.productCollection):void 0}async addWorkflowExperience(e){await this.appendWorkflowExperience(e),await this.injectExperienceIntoPreviewService(e);const t=await this.getGlobalProperties();await Promise.all(t.map(A=>A.applyGlobalState([e])))}async addWorkflowExperiences(e){const t=e.map(i=>i.getWorkflowManager().getTransaction().id),A=t.map(i=>this.workflowExperiences.find(o=>o.getWorkflowManager().getTransaction().id===i));if(A.some(i=>!!i))throw new Error("Unable to add transaction to bundle - Already Exists: "+A.filter(i=>!!i).map(i=>i==null?void 0:i.getWorkflowManager().getTransaction().id).join(", "));const a=N.getMap("transactionOwnerIds")||new Map,n=t.map(i=>a.get(i));await P.getShadowGraphqlClient().mutate({mutation:Ja,variables:{id:this.id,transactionIds:t,transactionOwnerIds:n}}).catch(i=>{console.error(i)}),this.workflowExperiences.push(...e),await Promise.all([this.updateTransactionOrder(),(async()=>{for(const o of e)await this.injectExperienceIntoPreviewService(o);const i=await this.getGlobalProperties();await Promise.all(i.map(o=>o.applyGlobalState(e)))})()])}async appendWorkflowExperience(e,t=!0){const A=e.getWorkflowManager().getTransaction().id;if(this.workflowExperiences.find(o=>o.getWorkflowManager().getTransaction().id===A))throw new Error("Unable to add transaction to bundle - Already Exists!");const i=(N.getMap("transactionOwnerIds")||new Map).get(A);await P.getShadowGraphqlClient().mutate({mutation:Ta,variables:{id:this.id,transactionId:A},context:{transactionOwnerId:i}}),t&&this.workflowExperiences.push(e)}async removeWorkflowExperience(e){const t=this.workflowExperiences.indexOf(e);await this.removeTransaction(t,this.workflowExperiences[t].getWorkflowManager().getTransaction())}async removeWorkflowExperiences(e){await this.removeTransactions(e.map(t=>t.getWorkflowManager().getTransaction()))}async removeWorkflowExperienceByTransaction(e){const t=this.workflowExperiences.findIndex(A=>A.getWorkflowManager().getTransaction().id===e.id);await this.removeTransaction(t,e)}async removeWorkflowExperiencesByTransactions(e){await this.removeTransactions(e)}async removeTransaction(e,t,A=!0){if(e>-1){const a=this.workflowExperiences.find(n=>n.getWorkflowManager().getTransaction().id===t.id);this.previewService&&a.getWorkflowManager().ejectFromPreviewService(),this.bundleStateManager.removeStateForTransaction(t.id),await P.getShadowGraphqlClient().mutate({mutation:La,variables:{id:this.id,transactionId:t.id,name:this.name,metadata:Array.from(this.metadata.entries()).map(([n,i])=>({key:n,value:i})),bundleStateData:this.bundleStateManager.getSerializedState()}}),A&&(this.workflowExperiences.splice(e,1),await this.updateTransactionOrder())}else throw new Error("Unable to remove workflow experience from bundle - Not Found! - "+t.id)}async removeTransactions(e){const t=e.map(a=>this.workflowExperiences.findIndex(n=>n.getWorkflowManager().getTransaction().id===a.id));if(t.some(a=>a===-1))throw new Error("Unable to remove workflow experience from bundle - The following aren't included in the bundle: "+e.filter((a,n)=>t[n]===-1).map(a=>a.id).join(", "));const A=e.map(a=>this.workflowExperiences.splice(this.workflowExperiences.findIndex(n=>n.getWorkflowManager().getTransaction().id===a.id),1)[0]);this.previewService&&A.forEach(a=>a.getWorkflowManager().ejectFromPreviewService()),e.forEach(a=>this.bundleStateManager.removeStateForTransaction(a.id)),await P.getShadowGraphqlClient().mutate({mutation:Oa,variables:{id:this.id,transactionIds:e.map(a=>a.id),name:this.name,metadata:Array.from(this.metadata.entries()).map(([a,n])=>({key:a,value:n})),bundleStateData:this.bundleStateManager.getSerializedState()}}),await this.updateTransactionOrder()}async insertWorkflowExperience(e,t){await this.appendWorkflowExperience(e,!1),this.workflowExperiences.splice(t,0,e),await Promise.all([this.updateTransactionOrder(),(async()=>{await this.injectExperienceIntoPreviewService(e);const A=await this.getGlobalProperties();await Promise.all(A.map(a=>a.applyGlobalState([e])))})()])}async replaceWorkflowExperience(e,t){if(e<0||e>=this.workflowExperiences.length)throw new Error("Unable to replace workflow experience in bundle - Index out of range!");const A=this.workflowExperiences[e];if(A.getWorkflowManager().getTransaction().id===t.getWorkflowManager().getTransaction().id)throw new Error("Unable to replace workflow experience in bundle - Same transaction!");await Promise.all([this.removeTransaction(e,A.getWorkflowManager().getTransaction(),!1),this.appendWorkflowExperience(t,!1)]),this.workflowExperiences[e]=t,await Promise.all([this.updateTransactionOrder(),(async()=>{await this.injectExperienceIntoPreviewService(t);const a=await this.getGlobalProperties();await Promise.all(a.map(n=>n.applyGlobalState([t])))})()])}async swapWorkflowExperiences(e,t){if(e<0||e>=this.workflowExperiences.length||t<0||t>=this.workflowExperiences.length)throw new Error("Unable to swap workflow experiences in bundle - Index out of range!");if(e===t)return;const A=this.workflowExperiences[e];this.workflowExperiences[e]=this.workflowExperiences[t],this.workflowExperiences[t]=A,await this.updateTransactionOrder()}getWorkflowExperiences(){return[...this.workflowExperiences]}async sortWorkflowExperiences(e){this.workflowExperiences=this.workflowExperiences.sort(e),await this.updateTransactionOrder()}getWorkflowExperienceCount(){return this.workflowExperiences.length}async addStakeholder(e,t){var n;const A=await P.getShadowGraphqlClient().mutate({mutation:za,variables:{id:this.id,details:e,type:t||re.Owner}});if(!((n=A.data)!=null&&n.bundleAddStakeholder))throw new Error("Bundle not found!");const a=A.data.bundleAddStakeholder.bundleStakeholders||[];this.storeStakeholderCustomers(a)}async updateStakeholders(e){var a;const t=await P.getShadowGraphqlClient().mutate({mutation:ja,variables:{id:this.id,input:e}});if(!((a=t.data)!=null&&a.bundleUpdateStakeholders))throw new Error("Bundle not found!");const A=t.data.bundleUpdateStakeholders.bundleStakeholders||[];this.storeStakeholderCustomers(A)}async getAllStakeholders(){const e=await P.getShadowGraphqlClient().query({query:Ga,errorPolicy:"all",variables:{id:this.id}});if(!e.data.bundles||e.data.bundles.length===0)throw new Error("Bundle not found!");const t=e.data.bundles[0].bundleStakeholders||[];return this.storeStakeholderCustomers(t),t}storeStakeholderCustomers(e){e.forEach(t=>{t.customer&&this.client.storeCustomer(t.customer)})}async finish(e){return await Qa(this.workflowExperiences.map(t=>({workflowManager:t.getWorkflowManager(),workflow:t.getWorkflowManager().getWorkflow(),layouts:t.getWorkflowManager().getLayouts(),getReducerState:()=>t.getCommandContext().getState(),product:t.getWorkflowManager().getProduct(),transaction:t.getWorkflowManager().getTransaction(),workflowSelections:t.getWorkflowManager().getWorkflowSelections(),designName:t.getWorkflowManager().getWorkflow().name,variationRecords:t.getWorkflowManager().getVariationRecords(),updateVariationRecords:A=>t.updateVariationRecords(A),workflowMetadata:t.getWorkflowManager().getWorkflowMetadata()})),e??(()=>{}))}getPreviewService(){return this.previewService}async setPreviewService(e){if(this.workflowExperiences&&this.workflowExperiences.length>0&&(this.workflowExperiences.forEach(t=>t.getWorkflowManager().ejectFromPreviewService()),e))for(const t of this.workflowExperiences)await this.injectExperienceIntoPreviewService(t);e&&e.registerModelLoadEventListener&&e.registerModelLoadEventListener(this.onModelLoadEvent.bind(this)),this.previewService&&this.previewService!==e&&this.previewService.unregisterModelLoadEventListener&&this.previewService.unregisterModelLoadEventListener(this.onModelLoadEvent.bind(this)),this.previewService=e}setWorkflowExperienceTransform(e,t){return this.bundleStateManager.setWorkflowExperienceTransform(e,t),this.updateBundle()}onModelLoadEvent(e){e.eventType==="unload"||!e.modelContainer||(e.modelContainer.registerMaterialSelectedCallback(()=>this.onModelSelectedEvent(e.modelContainer,!0)),e.modelContainer.registerMaterialDeselectedCallback(()=>this.onModelSelectedEvent(e.modelContainer,!1)))}onModelSelectedEvent(e,t){if(e&&e.metadata&&e.metadata instanceof Map&&e.metadata.has("workflowManager")){const a=e.metadata.get("workflowManager").getTransaction(),n=this.workflowExperiences.find(i=>i.getWorkflowManager().getTransaction().id===a.id);n&&this.fireEvent(t?"workflow-experience-hover-enter":"workflow-experience-hover-exit",{workflowExperience:n})}}async injectExperienceIntoPreviewService(e){if(this.previewService){const t=e.getWorkflowManager().injectIntoPreviewService(this.previewService);try{this.bundleStateManager.applyStateToWorkflowExperience(e)}catch(A){console.error(`Unable to apply state to workflow experience: ${A}`)}await t}}async loadExistingBundles(e,t){if(e.length===0)return;const A=await this.client.getWorkflowExperiences(e.map(a=>({type:"transaction",transactionId:a})),t);if(this.workflowExperiences=A,this.previewService)for(const a of A)await this.injectExperienceIntoPreviewService(a)}async updateBundle(){var t;if(!((t=(await P.getShadowGraphqlClient().mutate({mutation:ba,variables:{id:this.id,name:this.name,metadata:Array.from(this.metadata.entries()).map(([A,a])=>({key:A,value:a})),bundleStateData:this.bundleStateManager.getSerializedState()}})).data)!=null&&t.bundleUpdate))throw new Error("Bundle not found!")}async updateTransactionOrder(){await P.getShadowGraphqlClient().mutate({mutation:Ka,variables:{id:this.id,transactionIds:this.workflowExperiences.map(e=>e.getWorkflowManager().getTransaction().id)}})}async checkConditionalHandlesChanged(e,t){const A=await this.globalPropertyHandleService.getHandles(),a=this.globalPropertyHandleService.applyConditionsFromState(A,e),n=this.globalPropertyHandleService.applyConditionsFromState(A,t);(()=>{if(a.length!==n.length)return!0;for(let o=0;o<a.length;o++)if(a[o].getName()!==n[o].getName())return!0;return!1})()&&this.fireEvent("conditional-global-properties-changed",{globalProperties:n})}fireEvent(e,t){(this.eventListeners.get(e)||[]).forEach(a=>a({bundle:this,event:e,data:t}))}}const An=M.gql`
2249
2249
  mutation processFlowCreate($processFlowId: String!, $inputs: [String]!) {
2250
2250
  processExecutionCreate(processFlowId: $processFlowId, inputs: $inputs) {
2251
2251
  id
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spiffcommerce/core",
3
- "version": "16.4.0-rc.1",
3
+ "version": "16.4.0",
4
4
  "description": "Core client API for interacting with the Spiff Commerce backend.",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.umd.cjs",
@@ -28,7 +28,7 @@
28
28
  "devDependencies": {
29
29
  "@ladle/react": "2.17.2",
30
30
  "@preact/preset-vite": "^2.5.0",
31
- "@spiffcommerce/preview": "^5.0.0-rc.6",
31
+ "@spiffcommerce/preview": "^5.0.0",
32
32
  "@types/lodash.clonedeep": "^4.5.7",
33
33
  "@types/lodash.debounce": "^4.0.7",
34
34
  "@types/lodash.isequal": "^4.5.6",