import { ChangeDetectionStrategy, Component, computed, input, output } from '@angular/core'; import { QuantitySelectorComponent } from '../quantity-selector/quantity-selector.component'; import { IconButtonComponent } from '../icon-button/icon-button.component'; export interface CartItemAttribute { label: string; value: string; } @Component({ selector: 'app-cart-item', standalone: true, imports: [QuantitySelectorComponent, IconButtonComponent], templateUrl: './cart-item.component.html', styleUrl: './cart-item.component.scss', changeDetection: ChangeDetectionStrategy.OnPush }) export class CartItemComponent { readonly imageUrl = input(null); readonly product = input(''); readonly originalPrice = input(null); readonly discountedPrice = input(0); readonly discountPercentage = input(null); readonly attributes = input([]); readonly quantity = input(1); readonly quantityChange = output(); readonly remove = output(); readonly increase = output(); readonly decrease = output(); protected onQuantityChange(newQuantity: number): void { this.quantityChange.emit(newQuantity); } protected onRemove(): void { this.remove.emit(); } protected onIncrease(): void { this.increase.emit(); } protected onDecrease(): void { this.decrease.emit(); } protected readonly formattedOriginalPrice = computed(() => { const price = this.originalPrice(); return price === null ? null : this.formatCurrency(price); }); protected readonly formattedDiscountedPrice = computed(() => this.formatCurrency(this.discountedPrice())); private formatCurrency(value: number): string { const rounded = Math.round(value); const parts = rounded.toString().split('.'); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, '.'); return `$ ${parts.join(',')}`; } }