//import { FtTagType } from "../components/fancy-table/fancy-row-column.model";
//import { AddressInfo } from "../models/contactInfo.model";

import { AddressInfo } from "../entity/PastoralDomain";

export class StringUtils {

  // Sorting function for SemVer
  // Returns 1 if a is greater, -1 if b is greater, 0 if equal
  public static compareSemVer(a: string, b: string): number {
    if (a === b) {
      return 0;
    }

    var a_components = a.split(".");
    var b_components = b.split(".");

    var len = Math.min(a_components.length, b_components.length);

    // loop while the components are equal
    for (var i = 0; i < len; i++) {
      // A bigger than B
      if (parseInt(a_components[i]) > parseInt(b_components[i])) {
        return 1;
      }

      // B bigger than A
      if (parseInt(a_components[i]) < parseInt(b_components[i])) {
        return -1;
      }
    }

    // If one's a prefix of the other, the longer one is greater.
    if (a_components.length > b_components.length) {
      return 1;
    }

    if (a_components.length < b_components.length) {
      return -1;
    }

    // Otherwise they are the same.
    return 0;
  }

  public static isNullOrWhitespace(input: string): boolean {
    return !input || !input.trim();
  }

  public static getTrimmedValue(input: string): string {
    if (input) {
      return input.trim();
    }
    return '';
  }


  public static firstIsVowel(s: string): boolean {
    return ['a', 'e', 'i', 'o', 'u'].indexOf(s[0].toLowerCase()) !== -1
  }

  public static makeCommaSeparatedString(arr: string[], useOxfordComma: boolean, usingOr: boolean = false) {
    let term = usingOr ? 'or' : 'and';
    arr = arr.filter(s => s);
    const listStart = arr.slice(0, -1).join(', ');
    const listEnd = arr.slice(-1);
    const conjunction = arr.length <= 1 ? '' :
      useOxfordComma && arr.length > 2 ? `, ${term} ` : ` ${term} `;

    return [listStart, listEnd].join(conjunction);
  }



  public static getFormattedTerm(input: string, emptyFormatter: string = 'Empty'): string {
    if (false == this.isNullOrWhitespace(input)) {
      return input.trim();
    }
    return emptyFormatter;
  }
  public static camelToTitle(camelCase) {
    // no side-effects
    return camelCase
      // inject space before the upper case letters
      .replace(/([A-Z])/g, function (match) {
        return " " + match;
      })
      // replace first char with upper case
      .replace(/^./, function (match) {
        return match.toUpperCase();
      });
  }
  public static replaceAll(str: string, find: string, replace: string) {
    return str.replace(new RegExp(find, 'g'), replace);
  }

  /**
   * status could be `info` , `primary` , `danger` , `warning` , `success`, `light`, `dark`
   */
  public static getHtmlBadge(text: string, status: string) {
    return `<label class="badge badge-${status} mr-1 my-0 py-1">${text}</label>`;

  }
  /**
   * status could be `info` , `primary` , `danger` , `warning` , `success`, `light`, `dark`
   */
  public static getHtmlBadges(text: string[], status: string) {

    return "".concat(...text.map(p => this.getHtmlBadge(p, status)));

  }
  public static getHtmlInnerText(htmlText: string) {
    return htmlText ? htmlText.replace(/<[^>]+>/g, '') : '';

    let HTML_INNERTEXT_REGEX = />((.|\n*?))<\//g;

    var match = HTML_INNERTEXT_REGEX.exec(htmlText);
    if (match) {
      return match[1];
    } else {
      return htmlText;
    }
  }

  public static toUpperString(text: string) {
    if (text?.trim) {

      return text?.trim().toUpperCase();
    }
    return null;
  }

  public static arrayToUrlParams(paramName: string, arr: string[]) {

    let urlParam = '';

    for (let i = 0; i < arr.length; i++) {
      const value = arr[i];
      if (i > 0) {

        urlParam += ','
      }

      urlParam += `${paramName}[${i}]=${value}`;

    }
    return urlParam;
  }
  /**
   * Try to parse city sate zip string like `Monrovia, CA  91016` to AddressInfo, if failed will return `null` instead.
   */
  public static tryParseCityStateZip(cityStateZip: string): AddressInfo {

    let addressInfo = new AddressInfo();
    var regex = /([\w\s]*),\s*([A-Z]{2})\s*(\d*-?\d*)/g;
    var match = regex.exec(cityStateZip);
    if (match) {
      addressInfo = new AddressInfo();
      addressInfo.city = match[1];
      addressInfo.state = match[2];
      addressInfo.zip = match[3];
      return addressInfo;
    } else {
      return null;
    }
  }

  public static AddressCszToString(info: AddressInfo): string {

    let result = '';
    let secondPart = '';
    if (info) {

      if (!this.isNullOrWhitespace(info.city)) result += info.city.trim();

      secondPart = `${info.city} ${info.zip}`;
      if (!this.isNullOrWhitespace(secondPart)) result += `, ${secondPart.trim()}`
    }

    return result;
  }
}
