import { Component, ChangeDetectionStrategy } from '@angular/core';
import { DomainAvailabilityService } from '@domain/availability';
import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { PurchasingOptionsModalStore } from '../purchasing-options-modal.store';

@Component({
  selector: 'page-b2b-delivery-option',
  templateUrl: 'b2b-delivery-option.component.html',
  styleUrls: ['../option.scss', 'b2b-delivery-option.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class B2BDeliveryOptionComponent {
  readonly item$ = this._purchasingOptionsModalStore.selectItem;

  readonly availability$ = this._purchasingOptionsModalStore.selectAvailabilities.pipe(map((ava) => ava['b2b-delivery']));

  readonly price$ = combineLatest([this.availability$, this.item$]).pipe(
    map(([availability, item]) => this._availabilityService.getPriceForAvailability('b2b-delivery', item.catalogAvailability, availability))
  );

  constructor(private _purchasingOptionsModalStore: PurchasingOptionsModalStore, private _availabilityService: DomainAvailabilityService) {}

  select() {
    this._purchasingOptionsModalStore.setOption('b2b-delivery');
  }
}
