import { Component, ChangeDetectionStrategy } from '@angular/core';
import { ApplicationService } from '@core/application';
import { UiModalRef } from '@ui/modal';
import { isBoolean } from '@utils/common';
import { CantAddCustomerToCartData } from './cant-add-customer-to-cart.data';

@Component({
  selector: 'pages-cant-add-customer-to-cart-modal',
  templateUrl: 'cant-add-customer-to-cart.component.html',
  styleUrls: ['cant-add-customer-to-cart.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CantAddCustomerToCartModalComponent {
  get upgradable(): boolean {
    return typeof this.option?.value === 'string';
  }

  get option() {
    return (
      this.ref.data.upgradeableTo?.options.values.find((upgradeOption) =>
        this.ref.data.required.options.values.some((requiredOption) => upgradeOption.key === requiredOption.key)
      ) || { value: this.queryParams }
    );
  }

  get queryParams() {
    let option = this.ref.data.required?.options.values.find((f) => f.selected);
    if (!option) {
      option = this.ref.data.required?.options.values.find((f) => (isBoolean(f.enabled) ? f.enabled : true));
    }
    return option ? { customertype: option.value } : {};
  }

  constructor(public ref: UiModalRef<any, CantAddCustomerToCartData>, public readonly applicationService: ApplicationService) {}
}
