import { Directive, Input, Output, EventEmitter, SimpleChanges, ChangeDetectorRef } from '@angular/core';
import { DecimalPipe } from '@angular/common';

/*
    Created By    : Arun Joy
    Created On    : 20-03-2020
    Created For   : For Amount feilds decimal formatting.
*/

@Directive({
  selector: '[ictAmount]',
  host: {
    '[value]': 'ictAmount',
    '(change)': 'change($event.target.value)',
  }
})
export class AmountDirective {

  @Input() ictAmount: number;
  @Output() ictAmountChange: EventEmitter<string> = new EventEmitter<string>();

  amtDecPts = 2;

  constructor(
    private _cdr: ChangeDetectorRef,
    private _decimalPipe: DecimalPipe,
  ) { }

  ngOnChanges(changes: SimpleChanges): void {
    this.format(this.ictAmount);
  }

  change(value) {
    this.format(value, "C");
  }

  format(value, flag?: string) {
    value = (value && value.toString()) ? (value.toString().replace(/,/gi, "")) : 0;
    let pattern = "1." + this.amtDecPts + "-" + this.amtDecPts;
    value = this._decimalPipe.transform(value, pattern);
    value = value.replace(/,/gi, "");
    // this.ictAmountChange.next(value);
    if (!flag || flag != "C")
      this._cdr.detectChanges();
  }
}
