1import{bA as A,ad as y,g as M,cH as $,cI as Q,k as h,bS as B,cJ as C,bT as N,cK as U}from"./addresses-is-address-empty.ZJ1ThpFy.js";import{T as K,A as j}from"./utilities-extension-execution-errors.DiES5O5t.js";import{n as k}from"./esnext-vendor.BBoIVOQW.js";import{br as S}from"./hydrate.Tsur0pib.js";import{v as H}from"./dist-v4.EwEgHOG0.js";import{M as z}from"./extension-targets-rendering-extension-targets.r3iA_rtw.js";import{a as G}from"./deferred-mutations-assert-cart-line-change.C8mxht6x.js";import"./app.CUBUOQtX.js";import"./shared-permissions.DW7FOCqf.js";import"./hooks-useShopPayExternalAppContext.YLtTu3k6.js";import"./AnnouncementRuntimeExtensions.C0O5WcH_.js";import"./QRCode.OrXXMCLJ.js";import"./Checkbox.dqdh-9I_.js";import"./Choice.18syyoW7.js";import"./shop-pay-installments-types._vopm3yK.js";import"./utilities-dates.ChO2GdxN.js";import"./components-RedirectionNotice.module.qsrQCJ0v.js";import"./PaymentIcon.CEAvn1dM.js";import"./NumberField.D9yAw9rS.js";import"./Switch.DcsaOS_H.js";import"./extensions-remote-dom.DLP6cFL6.js";import"./EmailField.C8ocsqns.js";import"./TextArea.An2gFfWj.js";import"./Sheet.Dx28vgYD.js";import"./styles-floating-layer.module.BESFBxYZ.js";import"./hooks-useForceShopPayUrl.z4AJBm7B.js";import"./AddressPresenter.Ce4uWLSc.js";import"./PhoneField.D8EvZ0Pm.js";import"./shipping-methods-grouping.S_i1LuzE.js";import"./amazon-pay-useAmazonPayPaymentLine.CMgi35Fb.js";import"./hooks-useSuppressShopPayModalOnLoad.CwH45iIp.js";import"./hooks-useShowShopPayOptin.BPTq9EIX.js";import"./consent-manager-shared.D79dm3f4.js";import"./performance-index.mu6MOGR9.js";import"./shared-negotiation-input-redeemable.DoYPkwkm.js";function J(t){const{change:e,merchandiseLines:n}=t,i={merchandiseLines:n.peek(),discountLines:t.discountLines.peek()},o=k(()=>{switch(e.type){case"addCartLine":return W({...t,change:e});case"removeCartLine":return X({...t,change:e});case"updateCartLine":return Y({...t,change:e})}}),s=n.peek().find(r=>r.stableId===o);return{stableId:o,written:s,before:i}}function W({change:t,merchandiseLines:e,negotiatedMerchandise:n,discountLines:i,isCheckoutEditor:o,remoteMerchandiseDetails:s}){const r=e.peek(),d=V(t.merchandiseId),u=O(t.attributes),a=tt(t.parent,r,n),l=r.find(c=>!A(c.stableId,s)&&nt(c,{merchandiseId:d,properties:u,sellingPlanId:t.sellingPlanId,parentStableId:a?.stableId}));if(l==null&&t.quantity<=0)throw new y("Invalid line item quantity, must be greater than zero");if(l){const c=l.quantity+t.quantity;if(c>0){const[p]=l.lineAllocations;return E(e,l.stableId,c,{allocationId:p?.stableId,allocationQuantity:p?p.quantity+t.quantity:void 0}),l.stableId}return w(r,o),L({merchandiseLines:e,discountLines:i,line:l}),l.stableId}const f=at({merchandiseId:d,quantity:t.quantity,properties:u,sellingPlanId:t.sellingPlanId,parent:a});return e.value=[...r,f],f.stableId}function X({change:t,merchandiseLines:e,discountLines:n,negotiatedMerchandise:i,isCheckoutEditor:o,remoteMerchandiseDetails:s}){const r=e.peek(),{target:d,allocationId:u}=R(t.id,r,i,s),a=d.quantity-t.quantity;if(a>0){const l=d.lineAllocations.find(({stableId:f})=>f===u);return E(e,d.stableId,a,{allocationId:u,allocationQuantity:l?Math.max(l.quantity-t.quantity,0):void 0}),d.stableId}return w(r,o),L({merchandiseLines:e,discountLines:n,line:d}),d.stableId}function Y({change:t,merchandiseLines:e,discountLines:n,negotiatedMerchandise:i,isCheckoutEditor:o,remoteMerchandiseDetails:s}){const r=e.peek(),{target:d,allocationId:u}=R(t.id,r,i,s),a=t.quantity,l=Z(u,a,d);return l!=null&&l<=0?(w(r,o),L({merchandiseLines:e,discountLines:n,line:d}),d.stableId):(e.value=r.map(f=>f.stableId!==d.stableId?f:{...f,...l==null?{}:{quantity:l,lineComponents:_(f,l)},...a==null?{}:{lineAllocations:f.lineAllocations.map(c=>c.stableId===u?{...c,quantity:a}:c)},...t.merchandiseId?{id:V(t.merchandiseId)}:{},...t.attributes?{properties:O(t.attributes)}:{},...t.sellingPlanId===void 0?{}:{sellingPlan:t.sellingPlanId?{id:t.sellingPlanId,name:""}:void 0},totalPrice:void 0}),d.stableId)}function L({merchandiseLines:t,discountLines:e,line:n}){t.value=t.peek().filter(({stableId:i})=>i!==n.stableId),it(e,x(n)),Q(e)}function Z(t,e,n){if(e===void 0)return;const i=n.lineAllocations.filter(o=>o.stableId!==t);return i.length===0?e:e+i.reduce((o,s)=>o+s.quantity,0)}function E(t,e,n,i){const o=i?.allocationId,s=i?.allocationQuantity;t.value=t.peek().map(r=>{if(r.stableId!==e)return r;const d=o!=null&&s!=null?r.lineAllocations.map(u=>u.stableId===o?{...u,quantity:s}:u):r.lineAllocations;return{...r,quantity:n,lineAllocations:d,lineComponents:_(r,n),totalPrice:void 0}})}function R(t,e,n,i){const o=M(t);if(!o)throw new y(`Invalid line item id: "${t}". Id must be in format gid://shopify/LineItem/123`);const s=S(o,n)?.parent?.stableId,r=e.find(d=>d.stableId===s);if(!r)throw new y(`Unknown line item id: "${t}"`);if(A(r.stableId,i))throw new $("Cart line changes are not supported for merchandise from another shop.");return{target:r,allocationId:o}}function w(t,e){if(e&&t.length<=1)throw new y("Cannot remove the last cart line. A checkout must contain at least one cart line.")}function _(t,e){return t.lineComponents.length===0?t.lineComponents:t.lineComponents.map(n=>{if(!n.quantity||!t.quantity||n.quantity<t.quantity)throw new y("Invalid line component quantity");return{...n,quantity:e*(n.quantity/t.quantity),totalPrice:void 0}})}function x(t){return[t.stableId,...t.lineComponents.map(({stableId:e})=>e)]}function tt(t,e,n){if(!t)return;const i="merchandiseId"in t?n.find(s=>s.variantId===t.merchandiseId):et(t.lineId,n),o="merchandiseId"in t?t.merchandiseId:t.lineId;if(!i)throw new y(`Unknown parent line id: "${o}"`);if(!e.some(s=>s.stableId===i.stableId))throw new y(`Unknown parent line input id: "${o}"`);return{stableId:i.stableId,lineAllocations:i.lineAllocations}}function et(t,e){const n=M(t);if(!n)throw new y(`Invalid parent line id: "${t}". Id must be in format gid://shopify/CartLine/123`);return S(n,e)?.parent}function nt(t,{merchandiseId:e,properties:n,sellingPlanId:i,parentStableId:o}){const s=(t.sellingPlan?.id??null)===(i??null),r=(t.parentRelationship?.parent?.stableId??null)===(o??null);return t.id===e&&r&&s&&T(t.properties,n)}function it(t,e){if(e.length===0)return;const n=new Set(e);t.value=t.peek().map(i=>{if(!h(i))return i;const o=i.allocations.filter(({target:s})=>s.stableId==null||!n.has(s.stableId));return o.length===i.allocations.length?i:{...i,allocations:o}}).filter(i=>!h(i)||i.discount.type!=="custom"||i.allocations.length>0||i.anyAllocation!=null)}function ot(t,e,n){if(n.length===0)return;const i=new Set(n),o=a=>h(a)?a.allocations.filter(({target:l})=>l.stableId!=null&&i.has(l.stableId)):[],s=e.filter(a=>o(a).length>0);if(s.length===0)return;const r=[...t.peek()],d=new Map(r.flatMap((a,l)=>h(a)?[[g(a),l]]:[])),u=[];for(const a of s){if(!h(a))continue;const l=o(a),f=d.get(g(a));if(f==null){u.push({...a,allocations:l});continue}const c=r[f];if(!h(c))continue;const p=new Set(c.allocations.map(({target:m})=>m.stableId)),b=l.filter(({target:m})=>!p.has(m.stableId));b.length!==0&&(r[f]={...c,allocations:[...c.allocations,...b]})}t.value=[...r,...u]}function g(t){return[t.sourceTerm??"",t.discount.type,t.title,t.discount.allocationMethod??"",t.discount.targetSelection??""].join("\0")}function T(t,e){const n=i=>i.map(({name:o,value:s})=>`${o}\0${String(s)}`).sort().join("�");return n(t)===n(e)}function O(t){return(t??[]).map(({key:e,value:n})=>({sourceType:"MerchandisePropertyValueString",name:e,value:n,visible:!0}))}function V(t){return t.replace("ProductVariant/","ProductVariantMerchandise/")}function D(t,e){return t==null||t.quantity!==e.quantity||(t.sellingPlan?.id??null)!==(e.sellingPlan?.id??null)||(t.id??null)!==(e.id??null)?!1:T(t.properties,e.properties)}function rt({mutation:t,negotiatedMerchandise:e}){const n=e.find(o=>o.stableId===t.stableId);return t.written==null?n==null:t.before.merchandiseLines.find(o=>o.stableId===t.stableId)==null?n!=null:D(n,t.written)}function st({mutation:t,merchandiseLines:e,discountLines:n,negotiatedMerchandise:i}){const{stableId:o}=t,s=i.find(p=>p.stableId===o),r=t.before.merchandiseLines,d=r.find(p=>p.stableId===o),u=e.peek(),a=u.find(p=>p.stableId===o),l=t.written!=null&&D(a,t.written),f=l&&d==null&&s==null?a:void 0,c=t.written==null&&a==null?s:void 0;k(()=>{const p=l?u.map(b=>b.stableId===o?d??s??b:b):[...u];
1if(c){const b=r.findIndex(I=>I.stableId===o),m=()=>{if(b===-1)return p.length;for(let I=b-1;I>=0;I--){const v=r[I].stableId,P=p.findIndex(F=>F.stableId===v);if(P!==-1)return P+1}return 0};p.splice(m(),0,c)}e.value=p,f&&L({merchandiseLines:e,discountLines:n,line:f}),ot(n,t.before.discountLines,c?x(c):[])})}function at({merchandiseId:t,quantity:e,properties:n,sellingPlanId:i,parent:o}){return{stableId:H(),id:t,quantity:e,properties:n,totalPrice:void 0,lineComponents:[],lineAllocations:[],sellingPlan:i?{id:i,name:""}:void 0,typename:"ProductVariantMerchandise",legacyFee:!1,parentRelationship:o?{parent:o}:void 0}}function q(t){const e="targetEntity"in t?t.targetEntity:null;return U(e)?e.merchandiseLine.stableId:void 0}const Ut=async({change:t,negotiated:e,proposed:n,stableDetourSignal:i,cartInstructionsSignal:o,negotiate:s,isCheckoutEditor:r,observability:d})=>{G(t,o.value.lines);const u=["non-merchandise"];t.type==="addCartLine"&&u.push("delivery-line-detail");const a=J({change:t,merchandiseLines:n.merchandiseLines,discountLines:n.discountLines,negotiatedMerchandise:e.fields.merchandiseLines.value?.lines??[],remoteMerchandiseDetails:e.fields.remoteMerchandiseDetails?.value,isCheckoutEditor:r}),l=()=>{st({mutation:a,merchandiseLines:n.merchandiseLines,discountLines:n.discountLines,negotiatedMerchandise:e.fields.merchandiseLines.value?.lines??[]}),d.counter({name:"extension_cart_line_change_rolled_back",value:1,attributes:{changeType:t.type}})},f=(m,I)=>{d.counter({name:"extension_cart_line_change_outcome",value:1,attributes:{outcome:m,changeType:t.type,...I}})};let c;try{c=await s({silenceViolations:u})}catch(m){const v=!(m instanceof K||m instanceof j)&&rt({mutation:a,negotiatedMerchandise:e.fields.merchandiseLines.value?.lines??[]});throw v||l(),f("failed",{reflected:v}),m}const p=c.errors.find(m=>{const I=q(m);return I!=null?I===a.stableId:B.has(m.code)});if(!p)return f("applied"),{type:"success"};if(p.type==="ConfirmChangeViolation"&&!C.has(p.code))return f("applied"),{type:"success"};throw N(c.violations)&&(i.value=["stockProblems"]),c.errors.some(m=>{if(!C.has(m.code))return!1;const I=q(m);return I==null||I===a.stableId})||l(),f("refused"),new y(`${z} ${p.nonLocalizedMessage}`,{cause:{code:p.code}})};export{Ut as applyCartLineChange};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.