import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
} from '@angular/core';
import {
  SelectedRewardShoppingCartResource,
  ShoppingCartItem,
} from '@isa/checkout/data-access';
import { RewardShoppingCartItemComponent } from '../reward-shopping-cart-item/reward-shopping-cart-item.component';

@Component({
  selector: 'checkout-reward-shopping-cart-items',
  templateUrl: './reward-shopping-cart-items.component.html',
  styleUrls: ['./reward-shopping-cart-items.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RewardShoppingCartItemComponent],
})
export class RewardShoppingCartItemsComponent {
  #rewardShoppingCartResource = inject(SelectedRewardShoppingCartResource);

  items = computed(() => {
    const cart = this.#rewardShoppingCartResource.resource.value();
    return (
      cart?.items
        ?.map((item) => item.data!)
        .filter((item) => item.product != null) ?? []
    );
  });
}
