// Angular Core
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'stripNDC',
  standalone: false
})
export class StripNDCPipe implements PipeTransform {

  transform(value: string, ...args: unknown[]): string {
    /**
    * Strips Special characters from Start and end of Search Input.
    * 
    * @param {string} input
    *
    * @returns {string}
    */
    let input = value;
    if (input !== '' && input !== null && input !== undefined) {
      return this.trimSpecialChars(input);
    }
    return input;
  }

  /**
     * Strips Special characters from Start and end of Search Input,
     * the process of filtering Special characters is repeated until
     * all Special characters are removed/filtered.
     * 
     * @param {string} input
     *
     * @returns {string}
    */
  trimSpecialChars(input: string): any {
    input = input.replace(/@/g, '');
    if (/^[^a-zA-Z0-9]|[^a-zA-Z0-9]$/.test(input)) {
      input = input.replace(/^[^a-zA-Z0-9]|[^a-zA-Z0-9]$/g, '');
      return this.trimSpecialChars(input);
    }
    return input;
  };

}
