// Angular Core
import { Component, Input, OnInit } from '@angular/core';
import { SessionDataInterface } from 'src/app/interfaces/session-data-interface';

// Services
import { SessionService } from 'src/app/services/session/session.service';
import { environment } from 'src/environments/environment';

@Component({
  standalone: false,
  selector: 'app-rebate',
  templateUrl: './rebate.component.html',
  styleUrls: ['./rebate.component.scss']
})
export class RebateComponent implements OnInit {

  @Input() row !:any;
  imgUrl: string = environment.imageUrl;

  constructor(
    private sessionService: SessionService,
  ) { }

  session!: SessionDataInterface['data'];
  rebatePrice!:number;

  ngOnInit(): void {
    this.session = this.sessionService.getSession();
    /*
    * Below condition implemented as a part of TRX-7500.
    *
    * IPC rebate price should not be dependent on buyer monthly sales volume.
    * If IPC rebate value exists, we are showing the rebate amount on the tooltip.
    *
    * For GPO rebate - Buyer must be a trxade GPO member.
    * If GPO rebate value exists then we need to check buyer met monthly sales volume or not
    * If the buyer met the monthly sales volume, we will display the GPO rebate value on the tooltip.
    * Rebate information on tooltip located at the right of the price column.
    *
    * rebate column in rxPb_auctions table introduced as part of TRX-7191
    * Functionality not yet implemented for store the value in the rebate column(for GPO).
    */
    if (this.row.ipc_rebate_tier && this.row.ipc_rebate_tier != this.row.start_price) {
        this.rebatePrice = this.row.ipc_rebate_tier;
    } else if (this.row.buyer_sales_flag && this.session.trxade_gpo_member && this.row.rebate != this.row.start_price) {
        this.rebatePrice = this.row.rebate;
    }
  }

  isTooltipEnabled(text:string, count:number) {
    if (typeof text == 'string') {
        return text.length > count;
    } else {
        return false;
    }
  };

}
