1import{bx as V,cJ as b,g as _,cK as j,cL as K,cM as g,cN as C,cO as z,cP as A,cQ as H}from"./addresses-is-address-empty.Bn2lNY_L.js";import{i as L,b as X}from"./graphql-PaymentSessionMutation.CAXCwUvV.js";import{T as J,A as W}from"./utilities-extension-execution-errors.Bov3aYbn.js";import{n as D}from"./esnext-vendor.B9Da21W6.js";import{cS as x}from"./hydrate.DZ4YTm7p.js";import{v as Y}from"./dist-v4.EwEgHOG0.js";import{i as q,M as Z}from"./extension-targets-rendering-extension-targets.CtUkCn-H.js";import"./app.DbSrjjlA.js";import"./hooks-useShopPayExternalAppContext.DWwMuRnz.js";import"./hooks-useReplaceShopPayInHistory.DPpLejwu.js";import"./AnnouncementRuntimeExtensions.CyqR_T7y.js";import"./QRCode.CJw16n0F.js";import"./Pressable.ZFaj0-Rx.js";import"./Checkbox.DQyvbr9E.js";import"./Choice.DSKmQsTS.js";import"./utilities-dates.ChO2GdxN.js";import"./Popover.BScgzCsS.js";import"./PaymentIcon.CIva5Q4J.js";import"./NumberField.C_TDDWmw.js";import"./Switch.jEKUIqPZ.js";import"./extensions-remote-dom.MLww-Mka.js";import"./EmailField.M5oHXAvJ.js";import"./TextArea.C1mMoZ65.js";import"./Sheet.CvqBdJB1.js";import"./styles-floating-layer.module.BCoSmdvP.js";import"./hooks-useForceShopPayUrl.BFRInJvm.js";import"./AddressPresenter.BIgrto4P.js";import"./PhoneField.C0TeiZrn.js";import"./components-RedirectionNotice.module.CsVdFi0g.js";import"./amazon-pay-useAmazonPayPaymentLine.Ju006AId.js";import"./hooks-useSuppressShopPayModalOnLoad.DFOLDSdS.js";import"./hooks-useShowShopPayOptin.Br0M25ky.js";import"./consent-manager-shared.DROIEFP2.js";import"./performance-index.mu6MOGR9.js";import"./Page.jjGbeNkl.js";function tt(t){const{change:e,merchandiseLines:n}=t,i={merchandiseLines:n.peek(),discountLines:t.discountLines.peek()},r=D(()=>{switch(e.type){case"addCartLine":return et({...t,change:e});case"removeCartLine":return nt({...t,change:e});case"updateCartLine":return it({...t,change:e})}}),a=n.peek().find(o=>o.stableId===r);return{stableId:r,written:a,before:i}}function et({change:t,merchandiseLines:e,negotiatedMerchandise:n,discountLines:i,isCheckoutEditor:r,remoteMerchandiseDetails:a}){const o=e.peek(),d=Q(t.merchandiseId),c=G(t.attributes),s=ot(t.parent,o,n),l=o.find(f=>!V(f.stableId,a)&&st(f,{merchandiseId:d,properties:c,sellingPlanId:t.sellingPlanId,parentStableId:s?.stableId}));if(l==null&&t.quantity<=0)throw new b("Invalid line item quantity, must be greater than zero");if(l){const f=l.quantity+t.quantity;if(f>0){const[p]=l.lineAllocations;return O(e,l.stableId,f,{allocationId:p?.stableId,allocationQuantity:p?p.quantity+t.quantity:void 0}),l.stableId}return M(o,r),v({merchandiseLines:e,discountLines:i,line:l}),l.stableId}const u=ft({merchandiseId:d,quantity:t.quantity,properties:c,sellingPlanId:t.sellingPlanId,parent:s});return e.value=[...o,u],u.stableId}function nt({change:t,merchandiseLines:e,discountLines:n,negotiatedMerchandise:i,isCheckoutEditor:r,remoteMerchandiseDetails:a}){const o=e.peek(),{target:d,allocationId:c}=$(t.id,o,i,a),s=d.quantity-t.quantity;if(s>0){const l=d.lineAllocations.find(({stableId:u})=>u===c);return O(e,d.stableId,s,{allocationId:c,allocationQuantity:l?Math.max(l.quantity-t.quantity,0):void 0}),d.stableId}return M(o,r),v({merchandiseLines:e,discountLines:n,line:d}),d.stableId}function it({change:t,merchandiseLines:e,discountLines:n,negotiatedMerchandise:i,isCheckoutEditor:r,remoteMerchandiseDetails:a}){const o=e.peek(),{target:d,allocationId:c}=$(t.id,o,i,a),s=t.quantity,l=rt(c,s,d);return l!=null&&l<=0?(M(o,r),v({merchandiseLines:e,discountLines:n,line:d}),d.stableId):(e.value=o.map(u=>u.stableId!==d.stableId?u:{...u,...l==null?{}:{quantity:l,lineComponents:T(u,l)},...s==null?{}:{lineAllocations:u.lineAllocations.map(f=>f.stableId===c?{...f,quantity:s}:f)},...t.merchandiseId?{id:Q(t.merchandiseId)}:{},...t.attributes?{properties:G(t.attributes)}:{},...t.sellingPlanId===void 0?{}:{sellingPlan:t.sellingPlanId?{id:t.sellingPlanId,name:""}:void 0},totalPrice:void 0}),d.stableId)}function v({merchandiseLines:t,discountLines:e,line:n}){t.value=t.peek().filter(({stableId:i})=>i!==n.stableId),lt(e,N(n)),K(e)}function rt(t,e,n){if(e===void 0)return;const i=n.lineAllocations.filter(r=>r.stableId!==t);return i.length===0?e:e+i.reduce((r,a)=>r+a.quantity,0)}function O(t,e,n,i){const r=i?.allocationId,a=i?.allocationQuantity;t.value=t.peek().map(o=>{if(o.stableId!==e)return o;const d=r!=null&&a!=null?o.lineAllocations.map(c=>c.stableId===r?{...c,quantity:a}:c):o.lineAllocations;return{...o,quantity:n,lineAllocations:d,lineComponents:T(o,n),totalPrice:void 0}})}function $(t,e,n,i){const r=_(t);if(!r)throw new b(`Invalid line item id: "${t}". Id must be in format gid://shopify/LineItem/123`);const a=x(r,n)?.parent?.stableId,o=e.find(d=>d.stableId===a);if(!o)throw new b(`Unknown line item id: "${t}"`);if(V(o.stableId,i))throw new j("Cart line changes are not supported for merchandise from another shop.");return{target:o,allocationId:r}}function M(t,e){if(e&&t.length<=1)throw new b("Cannot remove the last cart line. A checkout must contain at least one cart line.")}function T(t,e){return t.lineComponents.length===0?t.lineComponents:t.lineComponents.map(n=>{if(!n.quantity||!t.quantity||n.quantity<t.quantity)throw new b("Invalid line component quantity");return{...n,quantity:e*(n.quantity/t.quantity),totalPrice:void 0}})}function N(t){return[t.stableId,...t.lineComponents.map(({stableId:e})=>e)]}function ot(t,e,n){if(!t)return;const i="merchandiseId"in t?n.find(a=>a.variantId===t.merchandiseId):at(t.lineId,n),r="merchandiseId"in t?t.merchandiseId:t.lineId;if(!i)throw new b(`Unknown parent line id: "${r}"`);if(!e.some(a=>a.stableId===i.stableId))throw new b(`Unknown parent line input id: "${r}"`);return{stableId:i.stableId,lineAllocations:i.lineAllocations}}function at(t,e){const n=_(t);if(!n)throw new b(`Invalid parent line id: "${t}". Id must be in format gid://shopify/CartLine/123`);return x(n,e)?.parent}function st(t,{merchandiseId:e,properties:n,sellingPlanId:i,parentStableId:r}){const a=(t.sellingPlan?.id??null)===(i??null),o=(t.parentRelationship?.parent?.stableId??null)===(r??null);return t.id===e&&o&&a&&F(t.properties,n)}function lt(t,e){if(e.length===0)return;const n=new Set(e);t.value=t.peek().map(i=>{if(!L(i))return i;const r=i.allocations.filter(({target:a})=>a.stableId==null||!n.has(a.stableId));return r.length===i.allocations.length?i:{...i,allocations:r}}).filter(i=>!L(i)||i.discount.type!=="custom"||i.allocations.length>0||i.anyAllocation!=null)}function dt(t,e,n){if(n.length===0)return;const i=new Set(n),r=s=>L(s)?s.allocations.filter(({target:l})=>l.stableId!=null&&i.has(l.stableId)):[],a=e.filter(s=>r(s).length>0);if(a.length===0)return;const o=[...t.peek()],d=new Map(o.flatMap((s,l)=>L(s)?[[k(s),l]]:[])),c=[];for(const s of a){if(!L(s))continue;const l=r(s),u=d.get(k(s));if(u==null){c.push({...s,allocations:l});continue}const f=o[u];if(!L(f))continue;const p=new Set(f.allocations.map(({target:P})=>P.stableId)),I=l.filter(({target:P})=>!p.has(P.stableId));I.length!==0&&(o[u]={...f,allocations:[...f.allocations,...I]})}t.value=[...o,...c]}function k(t){return[t.sourceTerm??"",t.discount.type,t.title,t.discount.allocationMethod??"",t.discount.targetSelection??""].join("\0")}function F(t,e){const n=i=>i.map(({name:r,value:a})=>`${r}\0${String(a)}`).sort().join("�");return n(t)===n(e)}function G(t){return(t??[]).map(({key:e,value:n})=>({sourceType:"MerchandisePropertyValueString",name:e,value:n,visible:!0}))}function Q(t){return t.replace("ProductVariant/","ProductVariantMerchandise/")}function U(t,e){return t==null||t.quantity!==e.quantity||(t.sellingPlan?.id??null)!==(e.sellingPlan?.id??null)||(t.id??null)!==(e.id??null)?!1:F(t.properties,e.properties)}function S({mutation:t,negotiatedMerchandise:e}){const n=e.find(r=>r.stableId===t.stableId);return t.written==null?n==null:t.before.merchandiseLines.find(r=>r.stableId===t.stableId)==null?n!=null:U(n,t.written)}function ct({mutation:t,proposedMerchandise:e}){const n=e.some(i=>i.stableId===t.stableId);return t.written==null?!n:n}function ut({mutation:t,merchandiseLines:e,discountLines:n,negotiatedMerchandise:i}){const{stableId:r}=t,a=i.find(p=>p.stableId===r),o=t.before.merchandiseLines,d=o.find(p=>p.stableId===r),c=e.peek(),s=c.find(p=>p.stableId===r),l=t.written!=null&&U(s,t.written),u=l&&d==null&&a==null?s:void 0,f=t.written==null&&s==null?a:void 0;D(()=>{const p=l?c.map(I=>I.stableId===r?d??a??I:I):[...c];if(f){const I=o.findIndex(m=>m.stableId===r),P=()=>{if(I===-1)return p.length;for(let m=I-1;m>=0;m--){const h=o[m].stableId,w=p.findIndex(B=>B.stableId===h);if(w!==-1)return w+1}return 0};p.splice(P(),0,f)}e.value=p,u&&v({merchandiseLines:e,discountLines:n,line:u}),dt(n,t.before.discountLines,f?N(f):[])})}function ft({merchandiseId:t,quantity:e,properties:n,sellingPlanId:i,parent:r}){return{stableId:Y(),id:t,quantity:e,properties:n,totalPrice:void 0,lineComponents:[],lineAllocations:[],sellingPlan:i?{id:i,name:""}:void 0,typename:"ProductVariantMerchandise",legacyFee:!1,parentRelationship:r?{parent:r}:void 0}}function pt(t,e){mt(t),It(e,t.type)}function y(t,e){if(!t)throw new b(e)}function E(t){y(t==null||Array.isArray(t),'Property "attributes" must be an array or undefined.'),t&&t.forEach(e
1=>{y(e!=null,'Attribute must be an object with "key" and "value" properties.'),y(typeof e.key=="string",'Property "key" must be a string.'),y(typeof e.value=="string",'Property "value" must be a string.')})}function mt(t){switch(t.type){case"addCartLine":{y(g(t.merchandiseId,"ProductVariant",C)||g(t.merchandiseId,"ProductVariantMerchandise",C),`Invalid property "merchandiseId": "${t.merchandiseId}". Merchandise id must be a valid GID in format gid://shopify/[ProductVariant|ProductVariantMerchandise]/1234567890`),y(Number.isSafeInteger(t.quantity),'Property "quantity" must be an integer.'),y(t.sellingPlanId===null||t.sellingPlanId===void 0||g(t.sellingPlanId,"SellingPlan"),'Property "sellingPlanId" must be a valid GID or undefined.'),E(t.attributes);break}case"removeCartLine":{y(typeof t.id=="string",'Property "id" must be a string.'),y(Number.isSafeInteger(t.quantity),'Property "quantity" must be an integer.');break}case"updateCartLine":{y(typeof t.id=="string",'Property "id" must be a string.'),y(t.merchandiseId===null||t.merchandiseId===void 0||g(t.merchandiseId,"ProductVariant",C)||g(t.merchandiseId,"ProductVariantMerchandise",C),`Invalid property "merchandiseId": "${t.merchandiseId}". Merchandise id must be undefined or a valid GID in format gid://shopify/[ProductVariant|ProductVariantMerchandise]/1234567890`),y(t.quantity==null||Number.isSafeInteger(t.quantity),'Property "quantity" must be an integer or undefined.'),y(t.sellingPlanId===void 0||t.sellingPlanId===null||g(t.sellingPlanId,"SellingPlan"),'Property "sellingPlanId" must be a valid GID or undefined.'),E(t.attributes);break}default:throw new b(`Unknown line item change type: "${t?.type}"`)}}function It(t,e){const n=`applyCartLinesChange with type ${e}`;if(e==="addCartLine"&&!t.canAddCartLine)throw new b(q(n,"lines"));if(e==="removeCartLine"&&!t.canRemoveCartLine)throw new b(q(n,"lines"));if(e==="updateCartLine"&&!t.canUpdateCartLine)throw new b(q(n,"lines"))}function R(t){const e="targetEntity"in t?t.targetEntity:null;return X(e)?e.merchandiseLine.stableId:void 0}const Wt=async({change:t,negotiated:e,proposed:n,stableDetourSignal:i,cartInstructionsSignal:r,negotiate:a,isCheckoutEditor:o,observability:d})=>{pt(t,r.value.lines);const c=["non-merchandise"];t.type==="addCartLine"&&c.push("delivery-line-detail");const s=tt({change:t,merchandiseLines:n.merchandiseLines,discountLines:n.discountLines,negotiatedMerchandise:e.fields.merchandiseLines.value?.lines??[],remoteMerchandiseDetails:e.fields.remoteMerchandiseDetails?.value,isCheckoutEditor:o}),l=()=>{ut({mutation:s,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}})},u=(m,h)=>{d.counter({name:"extension_cart_line_change_outcome",value:1,attributes:{outcome:m,changeType:t.type,...h}})},f=()=>({reflected:S({mutation:s,negotiatedMerchandise:e.fields.merchandiseLines.value?.lines??[]}),retained:ct({mutation:s,proposedMerchandise:n.merchandiseLines.value})});let p;try{p=await a({silenceViolations:c})}catch(m){const w=!(m instanceof J||m instanceof W)&&S({mutation:s,negotiatedMerchandise:e.fields.merchandiseLines.value?.lines??[]});throw w||l(),u("failed",{reflected:w}),m}const I=p.errors.find(m=>{const h=R(m);return h!=null?h===s.stableId:z.has(m.code)});if(!I)return u("applied",f()),{type:"success"};if(I.type==="ConfirmChangeViolation"&&!A.has(I.code))return u("applied",f()),{type:"success"};throw H(p.violations)&&(i.value=["stockProblems"]),p.errors.some(m=>{if(!A.has(m.code))return!1;const h=R(m);return h==null||h===s.stableId})||l(),u("refused"),new b(`${Z} ${I.nonLocalizedMessage}`,{cause:{code:I.code}})};export{Wt 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.