import { ConfigService } from "@nestjs/config";

const configService = new ConfigService

interface ToNumberOptions {
  default?: number;
  min?: number;
  max?: number;
}

export function toLowerCase(value: string): string {
  return value.toLowerCase();
}

export function trim(value: string): string {
  return value.trim();
}

export function toDate(value: string): Date {
  return new Date(value);
}

export function toBoolean(value: string): boolean {
  if (typeof value === 'boolean') {
    return value  
  }
  value = value.toLowerCase();

  return value === "true" || value === "1" ? true : false;
}

export function toNumber(value: string, opts: ToNumberOptions = {}): number {
  let newValue: number = Number.parseInt(value || String(opts.default), 10);

  if (Number.isNaN(newValue)) {
    newValue = opts.default;
  }

  if (opts.min) {
    if (newValue < opts.min) {
      newValue = opts.min;
    }

    if (newValue > opts.max) {
      newValue = opts.max;
    }
  }

  return newValue;
}

export function defaultBoolean(_default: boolean, value?: any) {
  if (value) {
    return toBoolean(value);
  }
  return _default;
}

export function toSplit(value: string) {
  if (value) return value.split(",");
  return value;
}

export function toUrl(path: string) {
  if (path) {
    const url = configService.get("AWS_S3_URL") + "/" + path
    return url;
  }
  return null;
}

export function toRupiah(number){
  return new Intl.NumberFormat("id-ID", {
    style: "currency",
    currency: "IDR"
  }).format(number);
}

export function numberFormat(val) {
  // remove sign if negative
  var sign = 1;
  if (val < 0) {
    sign = -1;
    val = -val;
  }
  // trim the number decimal point if it exists
  let num = val.toString().includes(',') ? val.toString().split(',')[0] : val.toString();
  let len = num.toString().length;
  let result = '';
  let count = 1;

  for (let i = len - 1; i >= 0; i--) {
    result = num.toString()[i] + result;
    if (count % 3 === 0 && count !== 0 && i !== 0) {
      result = '.' + result;
    }
    count++;
  }

  // add number after decimal point
  if (val.toString().includes(',')) {
    result = result + ',' + val.toString().split(',')[1];
  }
  // return result with - sign if negative
  return sign < 0 ? '-' + result : result;
}

export function rupiahFormat(val) {
  // remove sign if negative
  var sign = 1;
  if (val < 0) {
    sign = -1;
    val = -val;
  }
  // trim the number decimal point if it exists
  let num = val.toString().includes(',') ? val.toString().split(',')[0] : val.toString();
  let len = num.toString().length;
  let result = '';
  let count = 1;

  for (let i = len - 1; i >= 0; i--) {
    result = num.toString()[i] + result;
    if (count % 3 === 0 && count !== 0 && i !== 0) {
      result = '.' + result;
    }
    count++;
  }

  // add number after decimal point
  if (val.toString().includes(',')) {
    result = result + ',' + val.toString().split(',')[1];
  }
  // return result with - sign if negative
  return sign < 0 ? '(Rp. ' + result + ',-)' : 'Rp. ' + result + ',-';
}