import {
  AnalyticalScope,
  NotNull,
  ObjectRef,
  Text,
  Visible
} from "@webbpm/base-package";
import {ReplaceValueTextFormatter} from "./ReplaceValueTextFormatter";
import {FormatterUtils} from "./FormatterUtils";

@AnalyticalScope(Text)
export class UnitsLabelReplaceValueTextFormatter extends ReplaceValueTextFormatter {

  @NotNull()
  @ObjectRef()
  public unitsText: Text;

  public replaceDataLabel: string = '';

  public unitsLabel: string = '';
  public thousandsLabel: string = '';
  public millionsLabel: string = '';
  public billionsLabel: string = '';

  public decimalDelimiter: string;

  @NotNull('decimalDelimiter != null')
  @Visible('decimalDelimiter != null')
  public decimalLength: number = 0;

  format(value: string): string {
    let newValue = super.format(value);
    if (newValue !== value) {
      this.unitsText.setLabel(this.replaceDataLabel);
      return newValue;
    }

    let result = FormatterUtils.formatNumber({
                                               value: value,

                                               unitsLabel: this.unitsLabel,
                                               thousandsLabel: this.thousandsLabel,
                                               millionsLabel: this.millionsLabel,
                                               billionsLabel: this.billionsLabel,

                                               decimalDelimiter: this.decimalDelimiter,
                                               decimalLength: this.decimalLength
                                             });

    this.unitsText.setLabel(result.label);
    return result.formattedNumber;
  }
}