1import{bD as e,b8 as n,g as t,dg as i,dh as a,di as s,dj as r,dk as o,dl as l,dm as d}from"./4c290a1e.DMQ9xOwP.js";import{T as c,A as u}from"./66338e00.BFkksh37.js";import{n as m}from"./2c1ab0fe.GMbxnz74.js";import{i as p,a as f}from"./0a7dffce.BcNpdEJk.js";import{dX as h}from"./164ad2e3.B4L1MRYB.js";import{v as I}from"./2210eadb.DF0gIp_2.js";import{i as b,M as g}from"./c610ba76.uN4RNIMY.js";import"./app.DGSHpWaV.js";import"./9f047e05.DwjCPgK0.js";import"./5fda1420.BLaDhoyL.js";import"./60a453ce.Cg9T1Gj2.js";import"./21cc90a2.Ccyorzx_.js";import"./bb728e1c.CHt2JlPY.js";import"./28ac86c0.52eUFLJ9.js";import"./3e6029dd.B7hwoFu9.js";import"./30074413.D6ocfG5D.js";import"./efa0389a.ZcaEqvMP.js";import"./9f57d2aa.FdiYumpp.js";import"./8574876b.DlqSeZRL.js";import"./064f6ac1.DPGuEQ9h.js";import"./598fef4e.Blg5kMTg.js";import"./33d2e4fc.99-2I_X4.js";import"./39921a74.BMGPgeDG.js";import"./27f09e31.DahwbqDd.js";import"./137622f3.BPoTw3JS.js";import"./54bf0ebb.SmHxAfrv.js";import"./1ed8f5e3.DkmKdCgD.js";import"./2fdb3dd3.DyNzUnPi.js";import"./53bbdad9.oGVvLPt2.js";import"./47b970b2.C6dKnXeF.js";import"./29034f35.DARWjbfB.js";import"./ac5a19ff.BchXgW63.js";import"./0727407f.DihGPUji.js";import"./413b21e2.DZZRDa_s.js";import"./9c384ce8.CFjjgE6K.js";function y({merchandiseLines:e,discountLines:t,lines:i,line:a,isCheckoutEditor:s}){const r=function(e,n){return[e,...n.filter(n=>n.parentRelationship?.parent?.stableId===e.stableId)]}(a,i);!function(e,t,i){if(t&&e.length<=i)throw new n("Cannot remove the last cart line. A checkout must contain at least one cart line.")}(i,s,r.length),v({merchandiseLines:e,discountLines:t,removed:r})}function v({merchandiseLines:e,discountLines:n,removed:t}){const i=new Set(t.map(({stableId:e})=>e));e.value=e.peek().filter(({stableId:e})=>!i.has(e)),function(e,n){if(0===n.length)return;const t=new Set(n);e.value=e.peek().map(e=>{if(!p(e))return e;const n=e.allocations.filter(({target:e})=>null==e.stableId||!t.has(e.stableId));return n.length===e.allocations.length?e:{...e,allocations:n}}).filter(e=>!p(e)||"custom"!==e.discount.type||e.allocations.length>0||null!=e.anyAllocation)}(n,t.flatMap(e=>M(e))),a(n)}function L(e,n,t,i){const a=i?.allocationId,s=i?.allocationQuantity;e.value=e.peek().map(e=>{if(e.stableId!==n)return e;const i=null!=a&&null!=s?e.lineAllocations.map(e=>e.stableId===a?{...e,quantity:s}:e):e.lineAllocations;return{...e,quantity:t,lineAllocations:i,lineComponents:P(e,t),totalPrice:void 0}})}function w(a,s,r,o){const l=t(a);if(!l)throw new n(`Invalid line item id: "${a}". Id must be in format gid://shopify/LineItem/123`);const d=h(l,r)?.parent?.stableId,c=s.find(e=>e.stableId===d);if(!c)throw new n(`Unknown line item id: "${a}"`);if(e(c.stableId,o))throw new i("Cart line changes are not supported for merchandise from another shop.");return{target:c,allocationId:l}}function P(e,t){return 0===e.lineComponents.length?e.lineComponents:e.lineComponents.map(i=>{if(!i.quantity||!e.quantity||i.quantity<e.quantity)throw new n("Invalid line component quantity");return{...i,quantity:t*(i.quantity/e.quantity),totalPrice:void 0}})}function M(e){return[e.stableId,...e.lineComponents.map(({stableId:e})=>e)]}function C(e){return[e.sourceTerm??"",e.discount.type,e.title,e.discount.allocationMethod??"",e.discount.targetSelection??""].join("\0")}function j(e,n){const t=e=>e.map(({name:e,value:n})=>`${e}\0${String(n)}`).sort().join("�");return t(e)===t(n)}function q(e){return(e??[]).map(({key:e,value:n})=>({sourceType:"MerchandisePropertyValueString",name:e,value:n,visible:!0}))}function k(e){return e.replace("ProductVariant/","ProductVariantMerchandise/")}function _(e,n){return null!=e&&e.quantity===n.quantity&&(e.sellingPlan?.id??null)===(n.sellingPlan?.id??null)&&(e.id??null)===(n.id??null)&&j(e.properties,n.properties)}function S({mutation:e,negotiatedMerchandise:n}){const t=n.find(n=>n.stableId===e.stableId);return null==e.written?null==t:null==e.before.merchandiseLines.find(n=>n.stableId===e.stableId)?null!=t:_(t,e.written)}function A({mutation:e,proposedMerchandise:n}){const t=n.some(n=>n.stableId===e.stableId);return null==e.written?!t:t}function E(e,t){if(!e)throw new n(t)}function D(e){E(null==e||Array.isArray(e),'Property "attributes" must be an array or undefined.'),e&&e.forEach(e
1=>{E(null!=e,'Attribute must be an object with "key" and "value" properties.'),E("string"==typeof e.key,'Property "key" must be a string.'),E("string"==typeof e.value,'Property "value" must be a string.')})}const $={throwIfRefused(){},remember(){}};function V(e){const n="targetEntity"in e?e.targetEntity:null;return f(n)?n.merchandiseLine.stableId:void 0}const x=async({change:i,negotiated:a,proposed:f,stableDetourSignal:x,cartInstructionsSignal:R,negotiate:T,isCheckoutEditor:U,observability:z,refusedMerchandise:N})=>{!function(e,t){!function(e){switch(e.type){case"addCartLine":E(s(e.merchandiseId,"ProductVariant",r)||s(e.merchandiseId,"ProductVariantMerchandise",r),`Invalid property "merchandiseId": "${e.merchandiseId}". Merchandise id must be a valid GID in format gid://shopify/[ProductVariant|ProductVariantMerchandise]/1234567890`),E(Number.isSafeInteger(e.quantity),'Property "quantity" must be an integer.'),E(null===e.sellingPlanId||void 0===e.sellingPlanId||s(e.sellingPlanId,"SellingPlan"),'Property "sellingPlanId" must be a valid GID or undefined.'),D(e.attributes);break;case"removeCartLine":E("string"==typeof e.id,'Property "id" must be a string.'),E(Number.isSafeInteger(e.quantity),'Property "quantity" must be an integer.');break;case"updateCartLine":E("string"==typeof e.id,'Property "id" must be a string.'),E(null===e.merchandiseId||void 0===e.merchandiseId||s(e.merchandiseId,"ProductVariant",r)||s(e.merchandiseId,"ProductVariantMerchandise",r),`Invalid property "merchandiseId": "${e.merchandiseId}". Merchandise id must be undefined or a valid GID in format gid://shopify/[ProductVariant|ProductVariantMerchandise]/1234567890`),E(null==e.quantity||Number.isSafeInteger(e.quantity),'Property "quantity" must be an integer or undefined.'),E(void 0===e.sellingPlanId||null===e.sellingPlanId||s(e.sellingPlanId,"SellingPlan"),'Property "sellingPlanId" must be a valid GID or undefined.'),D(e.attributes);break;default:throw new n(`Unknown line item change type: "${e?.type}"`)}}(e),function(e,t){const i=`applyCartLinesChange with type ${t}`;if("addCartLine"===t&&!e.canAddCartLine)throw new n(b(i,"lines"));if("removeCartLine"===t&&!e.canRemoveCartLine)throw new n(b(i,"lines"));if("updateCartLine"===t&&!e.canUpdateCartLine)throw new n(b(i,"lines"))}(t,e.type)}(i,R.value.lines);const G=function({change:e,refusedMerchandise:t,observability:i}){if(null==t||!function(e){return"addCartLine"===e.type&&e.quantity>0&&!e.sellingPlanId&&null==e.parent}(e))return $;const a=k(e.merchandiseId);return{throwIfRefused(){const s=t.get(a);if(null!=s)throw i.counter({name:"extension_cart_line_change_refused_from_memory",value:1,attributes:{changeType:e.type,code:s.code}}),new n(`${g} ${s.nonLocalizedMessage}`,{cause:{code:s.code}})},remember(n,s){if(!function(e){return!e.before.merchandiseLines.some(n=>n.stableId===e.stableId)}(s))return;const r=n.errors.find(e=>"MERCHANDISE_PRODUCT_NOT_PUBLISHED"===e.code&&V(e)===s.stableId);null!=r&&(t.record(a,{code:r.code,nonLocalizedMessage:r.nonLocalizedMessage}),i.counter({name:"extension_cart_line_refusal_remembered",value:1,attributes:{changeType:e.type,code:r.code}}))}}}({change:i,refusedMerchandise:N,observability:z});G.throwIfRefused();const Q=["non-merchandise"];"addCartLine"===i.type&&Q.push("delivery-line-detail");const F=function(i){const{change:a,merchandiseLines:s}=i,r={merchandiseLines:s.peek(),discountLines:i.discountLines.peek()},o=m(()=>{switch(a.type){case"addCartLine":return function({change:i,merchandiseLines:a,negotiatedMerchandise:s,discountLines:r,isCheckoutEditor:o,remoteMerchandiseDetails:l}){const d=a.peek(),c=k(i.merchandiseId),u=q(i.attributes),m=function(e,i,a){if(!e)return;const s="merchandiseId"in e?a.find(n=>n.variantId===e.merchandiseId):function(e,i){const a=t(e);if(!a)throw new n(`Invalid parent line id: "${e}". Id must be in format gid://shopify/CartLine/123`);return h(a,i)?.parent}(e.lineId,a),r="merchandiseId"in e?e.merchandiseId:e.lineId;if(!s)throw new n(`Unknown parent line id: "${r}"`);if(!i.some(e=>e.stableId===s.stableId))throw new n(`Unknown parent line input id: "${r}"`);return{stableId:s.stableId,lineAllocations:s.lineAllocations}}(i.parent,d,s),p=d.find(n=>!e(n.stableId,l)&&function(e,{merchandiseId:n,properties:t,sellingPlanId:i,parentStableId:a}){const s=(e.sellingPlan?.id??null)===(i??null),r=(e.parentRelationship?.parent?.stableId??null)===(a??null);return e.id===n&&r&&s&&j(e.properties,t)}(n,{merchandiseId:c,properties:u,sellingPlanId:i.sellingPlanId,parentStableId:m?.stableId}));if(null==p&&i.quantity<=0)throw new n("Invalid line item quantity, must be greater than zero");if(p){const e=p.quantity+i.quantity;if(e>0){const[n]=p.lineAllocations;return L(a,p.stableId,e,{allocationId:n?.stableId,allocationQuantity:n?n.quantity+i.quantity:void 0}),p.stableId}return y({merchandiseLines:a,discountLines:r,lines:d,line:p,isCheckoutEditor:o}),p.stableId}const f=function({merchandiseId:e,quantity:n,properties:t,sellingPlanId:i,parent:a}){return{stableId:I(),id:e,quantity:n,properties:t,totalPrice:void 0,lineComponents:[],lineAllocations:[],sellingPlan:i?{id:i,name:""}:void 0,typename:"ProductVariantMerchandise",legacyFee:!1,parentRelationship:a?{parent:a}:void 0}}({merchandiseId:c,quantity:i.quantity,properties:u,sellingPlanId:i.sellingPlanId,parent:m});return a.value=[...d,f],f.stableId}({...i,change:a});case"removeCartLine":return function({change:e,merchandiseLines:n,discountLines:t,negotiatedMerchandise:i,isCheckoutEditor:a,remoteMerchandiseDetails:s}){const r=n.peek(),{target:o,allocationId:l}=w(e.id,r,i,s),d=o.quantity-e.quantity;if(d>0){const t=o.lineAllocations.find(({stableId:e})=>e===l);return L(n,o.stableId,d,{allocationId:l,allocationQuantity:t?Math.max(t.quantity-e.quantity,0):void 0}),o.stableId}return y({merchandiseLines:n,discountLines:t,lines:r,line:o,isCheckoutEditor:a}),o.stableId}({...i,change:a});case"updateCartLine":return function({change:e,merchandiseLines:n,discountLines:t,negotiatedMerchandise:i,isCheckoutEditor:a,remoteMerchandiseDetails:s}){const r=n.peek(),{target:o,allocationId:l}=w(e.id,r,i,s),d=e.quantity,c=function(e,n,t){if(void 0===n)return;const i=t.lineAllocations.filter(n=>n.stableId!==e);return 0===i.length?n:n+i.reduce((e,n)=>e+n.quantity,0)}(l,d,o);return null!=c&&c<=0?(y({merchandiseLines:n,discountLines:t,lines:r,line:o,isCheckoutEditor:a}),o.stableId):(n.value=r.map(n=>n.stableId!==o.stableId?n:{...n,...null==c?{}:{quantity:c,lineComponents:P(n,c)},...null==d?{}:{lineAllocations:n.lineAllocations.map(e=>e.stableId===l?{...e,quantity:d}:e)},...e.merchandiseId?{id:k(e.merchandiseId)}:{},...e.attributes?{properties:q(e.attributes)}:{},...void 0===e.sellingPlanId?{}:{sellingPlan:e.sellingPlanId?{id:e.sellingPlanId,name:""}:void 0},totalPrice:void 0}),o.stableId)}({...i,change:a})}}),l=s.peek(),d=l.find(e=>e.stableId===o),c=new Set(l.map(e=>e.stableId)),u=r.merchandiseLines.filter(e=>e.stableId!==o&&!c.has(e.stableId)).map(e=>e.stableId);
1return{stableId:o,cascadedStableIds:u,written:d,before:r}}({change:i,merchandiseLines:f.merchandiseLines,discountLines:f.discountLines,negotiatedMerchandise:a.fields.merchandiseLines.value?.lines??[],remoteMerchandiseDetails:a.fields.remoteMerchandiseDetails?.value,isCheckoutEditor:U}),H=()=>{(function({mutation:e,merchandiseLines:n,discountLines:t,negotiatedMerchandise:i}){const{stableId:a}=e,s=i.find(e=>e.stableId===a),r=e.before.merchandiseLines,o=r.find(e=>e.stableId===a),l=n.peek(),d=l.find(e=>e.stableId===a),c=e=>r.findIndex(n=>n.stableId===e.stableId),u=null!=e.written&&_(d,e.written),f=u&&null==o&&null==s?d:void 0,h=null==e.written?[a,...e.cascadedStableIds].filter(e=>!l.some(n=>n.stableId===e)).flatMap(e=>(e=>e===a?s:r.find(n=>n.stableId===e))(e)??[]).sort((e,n)=>c(e)-c(n)):[];m(()=>{const i=u?l.map(e=>e.stableId===a?o??s??e:e):[...l],d=e=>{if(-1===e)return i.length;for(let n=e-1;n>=0;n--){const e=r[n].stableId,t=i.findIndex(n=>n.stableId===e);if(-1!==t)return t+1}return 0};for(const e of h)i.splice(d(c(e)),0,e);n.value=i,f&&v({merchandiseLines:n,discountLines:t,removed:[f]}),function(e,n,t){if(0===t.length)return;const i=new Set(t),a=e=>p(e)?e.allocations.filter(({target:e})=>null!=e.stableId&&i.has(e.stableId)):[],s=n.filter(e=>a(e).length>0);if(0===s.length)return;const r=[...e.peek()],o=new Map(r.flatMap((e,n)=>p(e)?[[C(e),n]]:[])),l=[];for(const d of s){if(!p(d))continue;const e=a(d),n=o.get(C(d));if(null==n){l.push({...d,allocations:e});continue}const t=r[n];if(!p(t))continue;const i=new Set(t.allocations.map(({target:e})=>e.stableId)),s=e.filter(({target:e})=>!i.has(e.stableId));0!==s.length&&(r[n]={...t,allocations:[...t.allocations,...s]})}e.value=[...r,...l]}(t,e.before.discountLines,h.flatMap(e=>M(e)))})})({mutation:F,merchandiseLines:f.merchandiseLines,discountLines:f.discountLines,negotiatedMerchandise:a.fields.merchandiseLines.value?.lines??[]}),z.counter({name:"extension_cart_line_change_rolled_back",value:1,attributes:{changeType:i.type}})},O=(e,n)=>{z.counter({name:"extension_cart_line_change_outcome",value:1,attributes:{outcome:e,changeType:i.type,...n}})},X=()=>({reflected:S({mutation:F,negotiatedMerchandise:a.fields.merchandiseLines.value?.lines??[]}),retained:A({mutation:F,proposedMerchandise:f.merchandiseLines.value})});let B;try{B=await T({silenceViolations:Q})}catch(J){const e=!(J instanceof c||J instanceof u)&&S({mutation:F,negotiatedMerchandise:a.fields.merchandiseLines.value?.lines??[]});throw e||H(),O("failed",{reflected:e}),J}const Z=B.errors.find(e=>{const n=V(e);return null!=n?n===F.stableId:o.has(e.code)});if(!Z)return O("applied",X()),{type:"success"};if("ConfirmChangeViolation"===Z.type&&!l.has(Z.code))return O("applied",X()),{type:"success"};throw d(B.violations)&&(x.value=["stockProblems"]),B.errors.some(e=>{if(!l.has(e.code))return!1;const n=V(e);return null==n||n===F.stableId})||H(),O("refused"),G.remember(B,F),new n(`${g} ${Z.nonLocalizedMessage}`,{cause:{code:Z.code}})};export{x 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.