{"version":3,"file":"cart_controller.js","sources":["../../../assets/controllers/cart/cart_controller.ts"],"sourcesContent":["import { Controller } from '@hotwired/stimulus';\n\nexport default class extends Controller {\n    static targets = ['item', 'quantityInput', 'subtotal', 'discount', 'total', 'promoInput', 'promoError'];\n\n    declare readonly quantityInputTargets: HTMLInputElement[];\n    declare readonly promoInputTarget: HTMLInputElement;\n    declare readonly promoErrorTarget: HTMLElement;\n\n    connect() {\n        // Initialize if needed\n    }\n\n    async increaseQuantity(event: Event) {\n        const currentTarget = event.currentTarget as HTMLElement;\n        const key = currentTarget.dataset.key;\n        const input = this.quantityInputTargets.find(t => (t as HTMLElement).dataset.key === key) as HTMLInputElement;\n        if (input) {\n            const newQuantity = parseInt(input.value) + 1;\n            await this.updateQuantity(key, newQuantity);\n        }\n    }\n\n    async decreaseQuantity(event: Event) {\n        const currentTarget = event.currentTarget as HTMLElement;\n        const key = currentTarget.dataset.key;\n        const input = this.quantityInputTargets.find(t => (t as HTMLElement).dataset.key === key) as HTMLInputElement;\n        if (input) {\n            const newQuantity = Math.max(1, parseInt(input.value) - 1);\n            await this.updateQuantity(key, newQuantity);\n        }\n    }\n\n    async removeItem(event: Event) {\n        const currentTarget = event.currentTarget as HTMLElement;\n        const key = currentTarget.dataset.key;\n        if (confirm('Êtes-vous sûr de vouloir supprimer cet article ?')) {\n            try {\n                const response = await fetch(`/api/cart/${key}`, {\n                    method: 'DELETE',\n                    headers: {\n                        'Accept': 'application/json',\n                        'X-Requested-With': 'XMLHttpRequest'\n                    }\n                });\n\n                if (response.ok) {\n                    // Reload the page to reflect changes\n                    window.location.reload();\n                } else {\n                    console.error('Failed to remove item');\n                }\n            } catch (error) {\n                console.error('Error removing item:', error);\n            }\n        }\n    }\n\n    async updateQuantity(key: string | undefined, quantity: number) {\n        if (!key) return;\n        try {\n            const response = await fetch(`/api/cart/${key}`, {\n                method: 'PATCH',\n                headers: {\n                    'Content-Type': 'application/json',\n                    'Accept': 'application/json',\n                    'X-Requested-With': 'XMLHttpRequest'\n                },\n                body: JSON.stringify({ quantity })\n            });\n\n            if (response.ok) {\n                // Reload the page to reflect changes\n                window.location.reload();\n            } else {\n                console.error('Failed to update quantity');\n            }\n        } catch (error) {\n            console.error('Error updating quantity:', error);\n        }\n    }\n\n    async applyPromo(event: Event) {\n        event.preventDefault();\n        const input = this.promoInputTarget as HTMLInputElement;\n        const code = input.value.trim();\n\n        if (!code) return;\n\n        try {\n            const response = await fetch('/api/cart/promo', {\n                method: 'POST',\n                headers: {\n                    'Content-Type': 'application/json',\n                    'Accept': 'application/json',\n                    'X-Requested-With': 'XMLHttpRequest'\n                },\n                body: JSON.stringify({ code })\n            });\n\n            if (response.ok) {\n                window.location.reload();\n            } else {\n                const data = await response.json();\n                const errorTarget = this.promoErrorTarget as HTMLElement;\n                errorTarget.textContent = data.error || 'Code promo invalide';\n                errorTarget.classList.remove('hidden');\n            }\n        } catch (error) {\n            console.error('Error applying promo code:', error);\n            const errorTarget = this.promoErrorTarget as HTMLElement;\n            errorTarget.textContent = 'Une erreur est survenue';\n            errorTarget.classList.remove('hidden');\n        }\n    }\n}"],"names":[],"mappings":";AAEA,MAAA,wBAA6B,WAAW;AAAA,EACpC,OAAO,UAAU,CAAC,QAAQ,iBAAiB,YAAY,YAAY,SAAS,cAAc,YAAY;AAAA,EAMtG,UAAU;AAAA,EAEV;AAAA,EAEA,MAAM,iBAAiB,OAAc;AACjC,UAAM,gBAAgB,MAAM;AAC5B,UAAM,MAAM,cAAc,QAAQ;AAClC,UAAM,QAAQ,KAAK,qBAAqB,KAAK,OAAM,EAAkB,QAAQ,QAAQ,GAAG;AACxF,QAAI,OAAO;AACP,YAAM,cAAc,SAAS,MAAM,KAAK,IAAI;AAC5C,YAAM,KAAK,eAAe,KAAK,WAAW;AAAA,IAC9C;AAAA,EACJ;AAAA,EAEA,MAAM,iBAAiB,OAAc;AACjC,UAAM,gBAAgB,MAAM;AAC5B,UAAM,MAAM,cAAc,QAAQ;AAClC,UAAM,QAAQ,KA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