import{r as t,c as e,h as s}from"./p-b891c2f1.js";import{c as i}from"./p-eaac2001.js";const n=class{constructor(s){t(this,s),this.scUpdateLineItems=e(this,"scUpdateLineItems",7),this.syncItems=[]}handleLineItemToggle(t){this.addSyncItem("toggle",t.detail)}handleLineItemRemove(t){this.addSyncItem("remove",t.detail)}handleLineItemAdd(t){this.addSyncItem("add",t.detail)}handleLineItemUpdate(t){this.addSyncItem("update",t.detail)}async syncItemsHandler(t){(null==t?void 0:t.length)&&setTimeout((()=>{var t;if(!(null===(t=this.syncItems)||void 0===t?void 0:t.length))return;const e=this.processSyncItems();this.scUpdateLineItems.emit(e),this.syncItems=[]}),100)}addSyncItem(t,e){this.syncItems=[...this.syncItems,{type:t,payload:e}]}processSyncItems(){var t;let e=i((null===(t=null==this?void 0:this.order)||void 0===t?void 0:t.line_items)||[]);const s={toggle:this.toggleItem,add:this.addItem,remove:this.removeItem,update:this.updateItem};return(this.syncItems||[]).forEach((t=>{e=s[t.type](t.payload,e)})),e}addItem(t,e){return[...e,t]}toggleItem(t,e){var s;const i=null===(s=e.find((e=>e.price_id===t.price_id)))||void 0===s?void 0:s.price_id;return i?e.filter((t=>i!==t.price_id)):[...e,t]}removeItem(t,e){return t.price_id?e.filter((e=>e.price_id!==t.price_id)):e}updateItem(t,e){const s=e.findIndex((e=>e.price_id===t.price_id));return-1===s?[...e,t]:(e[s]=t,e)}render(){return s("slot",null)}static get watchers(){return{syncItems:["syncItemsHandler"]}}};export{n as sc_line_items_provider}