import { hookable, hookBefore, hookAfter } from '../../../lib/util/hookable.js'; import { Cart, Item } from './cart/Cart.js'; async function removeCartItem(cart: Cart, uuid: string) { const items = cart.getItems(); const item = cart.getItem(uuid); const newItems = items.filter((i) => i.getData('uuid') !== uuid); if (item) { await cart.setData('items', newItems); return item; } else { throw new Error('Item not found'); } } /** Removes an item from the cart by its UUID. * @param {Cart} cart - The cart object. * @param {string} uuid - The UUID of the item to remove. * @returns {Promise} - The removed item. * @throws {Error} - If the item is not found in the cart. */ export default async ( cart: Cart, uuid: string, context: Record ): Promise => { const removedItem = await hookable(removeCartItem, context)(cart, uuid); return removedItem; }; export function hookBeforeRemoveCartItem( callback: ( this: Record, ...args: [ cart: Cart, uuid: string ] ) => void | Promise, priority: number = 10 ): void { hookBefore('removeCartItem', callback, priority); } export function hookAfterRemoveCartItem( callback: ( this: Record, ...args: [ cart: Cart, uuid: string ] ) => void | Promise, priority: number = 10 ): void { hookAfter('removeCartItem', callback, priority); }