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

@Pipe({ name: 'ucapStringEmptycheck' })
export class StringEmptyCheckPipe implements PipeTransform {
  transform(value: string): string {
    return value.trim().length > 0 ? value.trim() : '-';
  }
}

@Pipe({ name: 'ucapStringFormatterPhone' })
export class StringFormatterPhonePipe implements PipeTransform {
  transform(value: string, hidden?: boolean): string {
    const num = value.trim().replace(/[-, ]/g, '');
    let formatNum = '';
    if (num.length === 0) {
      return num;
    }

    if (num.length === 11) {
      if (!!hidden && hidden) {
        formatNum = num.replace(/(\d{3})(\d{4})(\d{4})/, '$1-****-$3');
      } else {
        formatNum = num.replace(/(\d{3})(\d{4})(\d{4})/, '$1-$2-$3');
      }
    } else if (num.length === 8) {
      formatNum = num.replace(/(\d{4})(\d{4})/, '$1-$2');
    } else {
      if (num.indexOf('02') === 0) {
        if (!!hidden && hidden) {
          formatNum = num.replace(/(\d{2})(\d{4})(\d{4})/, '$1-****-$3');
        } else {
          formatNum = num.replace(/(\d{2})(\d{4})(\d{4})/, '$1-$2-$3');
        }
      } else {
        if (!!hidden && hidden) {
          formatNum = num.replace(/(\d{3})(\d{3})(\d{4})/, '$1-***-$3');
        } else {
          formatNum = num.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
        }
      }
    }
    return formatNum;
  }
}
