import{h}from"@stencil/core";import{convertLineItemsToLineItemData}from"../../../functions/line-items";export class ScLineItemsProvider{constructor(){this.order=void 0,this.syncItems=[]}handleLineItemToggle(e){const t=e.detail;this.addSyncItem("toggle",t)}handleLineItemRemove(e){const t=e.detail;this.addSyncItem("remove",t)}handleLineItemAdd(e){const t=e.detail;this.addSyncItem("add",t)}handleLineItemUpdate(e){const t=e.detail;this.addSyncItem("update",t)}async syncItemsHandler(e){(null==e?void 0:e.length)&&setTimeout((()=>{var e;if(!(null===(e=this.syncItems)||void 0===e?void 0:e.length))return;const t=this.processSyncItems();this.scUpdateLineItems.emit(t),this.syncItems=[]}),100)}addSyncItem(e,t){this.syncItems=[...this.syncItems,{type:e,payload:t}]}processSyncItems(){var e;let t=convertLineItemsToLineItemData((null===(e=null==this?void 0:this.order)||void 0===e?void 0:e.line_items)||[]);const i={toggle:this.toggleItem,add:this.addItem,remove:this.removeItem,update:this.updateItem};return(this.syncItems||[]).forEach((e=>{t=i[e.type](e.payload,t)})),t}addItem(e,t){return[...t,e]}toggleItem(e,t){var i;const s=null===(i=t.find((t=>t.price_id===e.price_id)))||void 0===i?void 0:i.price_id;return s?t.filter((e=>s!==e.price_id)):[...t,e]}removeItem(e,t){return e.price_id?t.filter((t=>t.price_id!==e.price_id)):t}updateItem(e,t){const i=t.findIndex((t=>t.price_id===e.price_id));return-1===i?[...t,e]:(t[i]=e,t)}render(){return h("slot",{key:"c52582c8cb4ba6030b71e372bc85f0031376f8eb"})}static get is(){return"sc-line-items-provider"}static get encapsulation(){return"shadow"}static get properties(){return{order:{type:"unknown",mutable:!1,complexType:{original:"Checkout",resolved:"Checkout",references:{Checkout:{location:"import",path:"../../../types",id:"src/types.ts::Checkout"}}},required:!1,optional:!1,docs:{tags:[],text:"Order Object"}}}}static get states(){return{syncItems:{}}}static get events(){return[{method:"scUpdateLineItems",name:"scUpdateLineItems",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:"Update line items event"},complexType:{original:"Array<LineItemData>",resolved:"LineItemData[]",references:{Array:{location:"global",id:"global::Array"},LineItemData:{location:"import",path:"../../../types",id:"src/types.ts::LineItemData"}}}}]}static get watchers(){return[{propName:"syncItems",methodName:"syncItemsHandler"}]}static get listeners(){return[{name:"scToggleLineItem",method:"handleLineItemToggle",target:void 0,capture:!1,passive:!1},{name:"scRemoveLineItem",method:"handleLineItemRemove",target:void 0,capture:!1,passive:!1},{name:"scAddLineItem",method:"handleLineItemAdd",target:void 0,capture:!1,passive:!1},{name:"scUpdateLineItem",method:"handleLineItemUpdate",target:void 0,capture:!1,passive:!1}]}}