/*
* Copyright (C) 2020 Robert Bosch GmbH Copyright (C) 2020 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
*/
/* eslint-disable @angular-eslint/use-pipe-transform-interface */

import { Inject, Optional, Pipe, PipeTransform } from '@angular/core';
import { defaultPlaceholder, NO_VALUE_PLACEHOLDER } from './no-value-placeholder';

/**
 * Shows a placeholder to highlight that no data is available. The following cases are treated as if there is no data:
 * empty strings, empty arrays, `null`, `undefined`, and `NaN`.
 *
 * The placeholder can be globally changed to an arbitrary symbol or text by overriding {@link NO_VALUE_PLACEHOLDER},
 * or by passing a symbol directly into the pipe. By default {@link defaultPlaceholder the default placeholder} is used.
 */
@Pipe({
  name: 'noValue',
})
export class NoValuePipe implements PipeTransform {
  constructor(@Inject(NO_VALUE_PLACEHOLDER) @Optional() private readonly placeholder: string | null) {
    if (this.placeholder == null) {
      this.placeholder = defaultPlaceholder;
    }
  }

  transform(value: any, placeholder?: string): any {
    if (value == null || value.length === 0 || Number.isNaN(value)) {
      return placeholder != null ? placeholder : this.placeholder;
    }
    return value;
  }
}
