1import{bE as V,bd as b,g as _,cT as X,cU as z,k as L,cV as w,cW as C,cX as H,cY as A,cZ as W,c_ as Y}from"./utilities-previous.Cnwy8G2v.js";import{T as Z,A as J}from"./utilities-extension-execution-errors.BbHw1F9O.js";import{n as D}from"./esnext-vendor.DDpcaSqU.js";import{d8 as x}from"./hydrate.BcfGVx0L.js";import{v as ee}from"./dist-v4.EwEgHOG0.js";import{i as v,M as te}from"./extension-targets-rendering-extension-targets.up2ahoGo.js";import"./app.DdDscdWN.js";import"./graphql-PaymentSessionMutation.C-nDEOnn.js";import"./shared-permissions.B59tuyWB.js";import"./hooks-useShopPayExternalAppContext.R6pnDxYu.js";import"./AnnouncementRuntimeExtensions.C2AR34tD.js";import"./QRCode.Dq91AK4b.js";import"./Pressable.ChoeO1LE.js";import"./Checkbox.CdJzVizb.js";import"./Choice.D9vYH2mX.js";import"./utilities-dates.ChO2GdxN.js";import"./Popover.ZkMcWGfE.js";import"./PaymentIcon.-ypvsmSq.js";import"./NumberField.9L3K4XMZ.js";import"./Switch.Bbq5KJxf.js";import"./extensions-remote-dom.Ca77wI-c.js";import"./EmailField.DprXO6cp.js";import"./TextArea.CCo8tV-a.js";import"./Sheet.CBSPD0rO.js";import"./styles-floating-layer.module.CZ3-lUNr.js";import"./hooks-useForceShopPayUrl.JfumpHY0.js";import"./utilities-stable-ref.dCtJn23l.js";import"./PhoneField.rAEISard.js";import"./FormLayout.UkTBTXMZ.js";import"./amazon-pay-useAmazonPayPaymentLine.X7jqQiq6.js";import"./hooks-useSuppressShopPayModalOnLoad.Cvo-K0z2.js";import"./helpers-isSwitchAccountBootRequest.C3tmXnb7.js";import"./performance-index.mu6MOGR9.js";import"./cross-border-hooks.BVR78hJy.js";function ne(e){const{change:t,merchandiseLines:n}=e,r={merchandiseLines:n.peek(),discountLines:e.discountLines.peek()},i=D(()=>{switch(t.type){case"addCartLine":return ie({...e,change:t});case"removeCartLine":return re({...e,change:t});case"updateCartLine":return ae({...e,change:t})}}),o=n.peek(),s=o.find(a=>a.stableId===i),d=new Set(o.map(a=>a.stableId)),u=r.merchandiseLines.filter(a=>a.stableId!==i&&!d.has(a.stableId)).map(a=>a.stableId);return{stableId:i,cascadedStableIds:u,written:s,before:r}}function ie({change:e,merchandiseLines:t,negotiatedMerchandise:n,discountLines:r,isCheckoutEditor:i,remoteMerchandiseDetails:o}){const s=t.peek(),d=U(e.merchandiseId),u=Q(e.attributes),a=de(e.parent,s,n),l=s.find(m=>!V(m.stableId,o)&&ue(m,{merchandiseId:d,properties:u,sellingPlanId:e.sellingPlanId,parentStableId:a?.stableId}));if(l==null&&e.quantity<=0)throw new b("Invalid line item quantity, must be greater than zero");if(l){const m=l.quantity+e.quantity;if(m>0){const[y]=l.lineAllocations;return O(t,l.stableId,m,{allocationId:y?.stableId,allocationQuantity:y?y.quantity+e.quantity:void 0}),l.stableId}return M({merchandiseLines:t,discountLines:r,lines:s,line:l,isCheckoutEditor:i}),l.stableId}const f=be({merchandiseId:d,quantity:e.quantity,properties:u,sellingPlanId:e.sellingPlanId,parent:a});return t.value=[...s,f],f.stableId}function re({change:e,merchandiseLines:t,discountLines:n,negotiatedMerchandise:r,isCheckoutEditor:i,remoteMerchandiseDetails:o}){const s=t.peek(),{target:d,allocationId:u}=T(e.id,s,r,o),a=d.quantity-e.quantity;if(a>0){const l=d.lineAllocations.find(({stableId:f})=>f===u);return O(t,d.stableId,a,{allocationId:u,allocationQuantity:l?Math.max(l.quantity-e.quantity,0):void 0}),d.stableId}return M({merchandiseLines:t,discountLines:n,lines:s,line:d,isCheckoutEditor:i}),d.stableId}function ae({change:e,merchandiseLines:t,discountLines:n,negotiatedMerchandise:r,isCheckoutEditor:i,remoteMerchandiseDetails:o}){const s=t.peek(),{target:d,allocationId:u}=T(e.id,s,r,o),a=e.quantity,l=se(u,a,d);return l!=null&&l<=0?(M({merchandiseLines:t,discountLines:n,lines:s,line:d,isCheckoutEditor:i}),d.stableId):(t.value=s.map(f=>f.stableId!==d.stableId?f:{...f,...l==null?{}:{quantity:l,lineComponents:F(f,l)},...a==null?{}:{lineAllocations:f.lineAllocations.map(m=>m.stableId===u?{...m,quantity:a}:m)},...e.merchandiseId?{id:U(e.merchandiseId)}:{},...e.attributes?{properties:Q(e.attributes)}:{},...e.sellingPlanId===void 0?{}:{sellingPlan:e.sellingPlanId?{id:e.sellingPlanId,name:""}:void 0},totalPrice:void 0}),d.stableId)}function M({merchandiseLines:e,discountLines:t,lines:n,line:r,isCheckoutEditor:i}){const o=oe(r,n);le(n,i,o.length),$({merchandiseLines:e,discountLines:t,removed:o})}function oe(e,t){return[e,...t.filter(n=>n.parentRelationship?.parent?.stableId===e.stableId)]}function $({merchandiseLines:e,discountLines:t,removed:n}){const r=new Set(n.map(({stableId:i})=>i));e.value=e.peek().filter(({stableId:i})=>!r.has(i)),fe(t,n.flatMap(i=>G(i))),z(t)}function se(e,t,n){if(t===void 0)return;const r=n.lineAllocations.filter(i=>i.stableId!==e);return r.length===0?t:t+r.reduce((i,o)=>i+o.quantity,0)}function O(e,t,n,r){const i=r?.allocationId,o=r?.allocationQuantity;e.value=e.peek().map(s=>{if(s.stableId!==t)return s;const d=i!=null&&o!=null?s.lineAllocations.map(u=>u.stableId===i?{...u,quantity:o}:u):s.lineAllocations;return{...s,quantity:n,lineAllocations:d,lineComponents:F(s,n),totalPrice:void 0}})}function T(e,t,n,r){const i=_(e);if(!i)throw new b(`Invalid line item id: "${e}". Id must be in format gid://shopify/LineItem/123`);const o=x(i,n)?.parent?.stableId,s=t.find(d=>d.stableId===o);if(!s)throw new b(`Unknown line item id: "${e}"`);
1if(V(s.stableId,r))throw new X("Cart line changes are not supported for merchandise from another shop.");return{target:s,allocationId:i}}function le(e,t,n){if(t&&e.length<=n)throw new b("Cannot remove the last cart line. A checkout must contain at least one cart line.")}function F(e,t){return e.lineComponents.length===0?e.lineComponents:e.lineComponents.map(n=>{if(!n.quantity||!e.quantity||n.quantity<e.quantity)throw new b("Invalid line component quantity");return{...n,quantity:t*(n.quantity/e.quantity),totalPrice:void 0}})}function G(e){return[e.stableId,...e.lineComponents.map(({stableId:t})=>t)]}function de(e,t,n){if(!e)return;const r="merchandiseId"in e?n.find(o=>o.variantId===e.merchandiseId):ce(e.lineId,n),i="merchandiseId"in e?e.merchandiseId:e.lineId;if(!r)throw new b(`Unknown parent line id: "${i}"`);if(!t.some(o=>o.stableId===r.stableId))throw new b(`Unknown parent line input id: "${i}"`);return{stableId:r.stableId,lineAllocations:r.lineAllocations}}function ce(e,t){const n=_(e);if(!n)throw new b(`Invalid parent line id: "${e}". Id must be in format gid://shopify/CartLine/123`);return x(n,t)?.parent}function ue(e,{merchandiseId:t,properties:n,sellingPlanId:r,parentStableId:i}){const o=(e.sellingPlan?.id??null)===(r??null),s=(e.parentRelationship?.parent?.stableId??null)===(i??null);return e.id===t&&s&&o&&N(e.properties,n)}function fe(e,t){if(t.length===0)return;const n=new Set(t);e.value=e.peek().map(r=>{if(!L(r))return r;const i=r.allocations.filter(({target:o})=>o.stableId==null||!n.has(o.stableId));return i.length===r.allocations.length?r:{...r,allocations:i}}).filter(r=>!L(r)||r.discount.type!=="custom"||r.allocations.length>0||r.anyAllocation!=null)}function pe(e,t,n){if(n.length===0)return;const r=new Set(n),i=a=>L(a)?a.allocations.filter(({target:l})=>l.stableId!=null&&r.has(l.stableId)):[],o=t.filter(a=>i(a).length>0);if(o.length===0)return;const s=[...e.peek()],d=new Map(s.flatMap((a,l)=>L(a)?[[k(a),l]]:[])),u=[];for(const a of o){if(!L(a))continue;const l=i(a),f=d.get(k(a));if(f==null){u.push({...a,allocations:l});continue}const m=s[f];if(!L(m))continue;const y=new Set(m.allocations.map(({target:c})=>c.stableId)),P=l.filter(({target:c})=>!y.has(c.stableId));P.length!==0&&(s[f]={...m,allocations:[...m.allocations,...P]})}e.value=[...s,...u]}function k(e){return[e.sourceTerm??"",e.discount.type,e.title,e.discount.allocationMethod??"",e.discount.targetSelection??""].join("\0")}function N(e,t){const n=r=>r.map(({name:i,value:o})=>`${i}\0${String(o)}`).sort().join("�");return n(e)===n(t)}function Q(e){return(e??[]).map(({key:t,value:n})=>({sourceType:"MerchandisePropertyValueString",name:t,value:n,visible:!0}))}function U(e){return e.replace("ProductVariant/","ProductVariantMerchandise/")}function B(e,t){return e==null||e.quantity!==t.quantity||(e.sellingPlan?.id??null)!==(t.sellingPlan?.id??null)||(e.id??null)!==(t.id??null)?!1:N(e.properties,t.properties)}function S({mutation:e,negotiatedMerchandise:t}){const n=t.find(i=>i.stableId===e.stableId);return e.written==null?n==null:e.before.merchandiseLines.find(i=>i.stableId===e.stableId)==null?n!=null:B(n,e.written)}function me({mutation:e,proposedMerchandise:t}){const n=t.some(r=>r.stableId===e.stableId);return e.written==null?!n:n}function Ie({mutation:e,merchandiseLines:t,discountLines:n,negotiatedMerchandise:r}){const{stableId:i}=e,o=r.find(c=>c.stableId===i),s=e.before.merchandiseLines,d=s.find(c=>c.stableId===i),u=t.peek(),a=u.find(c=>c.stableId===i),l=c=>s.findIndex(p=>p.stableId===c.stableId),f=e.written!=null&&B(a,e.written),m=f&&d==null&&o==null?a:void 0,y=c=>c===i?o:s.find(p=>p.stableId===c),P=e.written==null?[i,...e.cascadedStableIds].filter(c=>!u.some(p=>p.stableId===c)).flatMap(c=>y(c)??[]).sort((c,p)=>l(c)-l(p)):[];D(()=>{const c=f?u.map(I=>I.stableId===i?d??o??I:I):[...u],p=I=>{if(I===-1)return c.length;for(let g=I-1;g>=0;g--){const j=s[g].stableId,q=c.findIndex(K=>K.stableId===j);if(q!==-1)return q+1}return 0};for(const I of P)c.splice(p(l(I)),0,I);t.value=c,m&&$({merchandiseLines:t,discountLines:n,removed:[m]}),pe(n,e.before.discountLines,P.flatMap(I=>G(I)))})}function be({merchandiseId:e,quantity:t,properties:n,sellingPlanId:r,parent:i}){return{stableId:ee(),id:e,quantity:t,properties:n,totalPrice:void 0,lineComponents:[],lineAllocations:[],sellingPlan:r?{id:r,name:""}:void 0,typename:"ProductVariantMerchandise",legacyFee:!1,parentRelationship:i?{parent:i}:void 0}}
1function he(e,t){ye(e),Pe(t,e.type)}function h(e,t){if(!e)throw new b(t)}function E(e){h(e==null||Array.isArray(e),'Property "attributes" must be an array or undefined.'),e&&e.forEach(t=>{h(t!=null,'Attribute must be an object with "key" and "value" properties.'),h(typeof t.key=="string",'Property "key" must be a string.'),h(typeof t.value=="string",'Property "value" must be a string.')})}function ye(e){switch(e.type){case"addCartLine":{h(w(e.merchandiseId,"ProductVariant",C)||w(e.merchandiseId,"ProductVariantMerchandise",C),`Invalid property "merchandiseId": "${e.merchandiseId}". Merchandise id must be a valid GID in format gid://shopify/[ProductVariant|ProductVariantMerchandise]/1234567890`),h(Number.isSafeInteger(e.quantity),'Property "quantity" must be an integer.'),h(e.sellingPlanId===null||e.sellingPlanId===void 0||w(e.sellingPlanId,"SellingPlan"),'Property "sellingPlanId" must be a valid GID or undefined.'),E(e.attributes);break}case"removeCartLine":{h(typeof e.id=="string",'Property "id" must be a string.'),h(Number.isSafeInteger(e.quantity),'Property "quantity" must be an integer.');break}case"updateCartLine":{h(typeof e.id=="string",'Property "id" must be a string.'),h(e.merchandiseId===null||e.merchandiseId===void 0||w(e.merchandiseId,"ProductVariant",C)||w(e.merchandiseId,"ProductVariantMerchandise",C),`Invalid property "merchandiseId": "${e.merchandiseId}". Merchandise id must be undefined or a valid GID in format gid://shopify/[ProductVariant|ProductVariantMerchandise]/1234567890`),h(e.quantity==null||Number.isSafeInteger(e.quantity),'Property "quantity" must be an integer or undefined.'),h(e.sellingPlanId===void 0||e.sellingPlanId===null||w(e.sellingPlanId,"SellingPlan"),'Property "sellingPlanId" must be a valid GID or undefined.'),E(e.attributes);break}default:throw new b(`Unknown line item change type: "${e?.type}"`)}}function Pe(e,t){const n=`applyCartLinesChange with type ${t}`;if(t==="addCartLine"&&!e.canAddCartLine)throw new b(v(n,"lines"));if(t==="removeCartLine"&&!e.canRemoveCartLine)throw new b(v(n,"lines"));if(t==="updateCartLine"&&!e.canUpdateCartLine)throw new b(v(n,"lines"))}function R(e){const t="targetEntity"in e?e.targetEntity:null;return Y(t)?t.merchandiseLine.stableId:void 0}const et=async({change:e,negotiated:t,proposed:n,stableDetourSignal:r,cartInstructionsSignal:i,negotiate:o,isCheckoutEditor:s,observability:d})=>{he(e,i.value.lines);const u=["non-merchandise"];e.type==="addCartLine"&&u.push("delivery-line-detail");const a=ne({change:e,merchandiseLines:n.merchandiseLines,discountLines:n.discountLines,negotiatedMerchandise:t.fields.merchandiseLines.value?.lines??[],remoteMerchandiseDetails:t.fields.remoteMerchandiseDetails?.value,isCheckoutEditor:s}),l=()=>{Ie({mutation:a,merchandiseLines:n.merchandiseLines,discountLines:n.discountLines,negotiatedMerchandise:t.fields.merchandiseLines.value?.lines??[]}),d.counter({name:"extension_cart_line_change_rolled_back",value:1,attributes:{changeType:e.type}})},f=(p,I)=>{d.counter({name:"extension_cart_line_change_outcome",value:1,attributes:{outcome:p,changeType:e.type,...I}})},m=()=>({reflected:S({mutation:a,negotiatedMerchandise:t.fields.merchandiseLines.value?.lines??[]}),retained:me({mutation:a,proposedMerchandise:n.merchandiseLines.value})});let y;try{y=await o({silenceViolations:u})}catch(p){const g=!(p instanceof Z||p instanceof J)&&S({mutation:a,negotiatedMerchandise:t.fields.merchandiseLines.value?.lines??[]});throw g||l(),f("failed",{reflected:g}),p}const P=y.errors.find(p=>{const I=R(p);return I!=null?I===a.stableId:H.has(p.code)});if(!P)return f("applied",m()),{type:"success"};if(P.type==="ConfirmChangeViolation"&&!A.has(P.code))return f("applied",m()),{type:"success"};throw W(y.violations)&&(r.value=["stockProblems"]),y.errors.some(p=>{if(!A.has(p.code))return!1;const I=R(p);return I==null||I===a.stableId})||l(),f("refused"),new b(`${te} ${P.nonLocalizedMessage}`,{cause:{code:P.code}})};export{et 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.