import BigNumber from 'bignumber.js';
import _ from 'lodash';
import md5 from 'md5';
import { AreaCode, Estate } from '@/types/common';
import moment, { Moment } from 'moment';

export function encodePhone(code: AreaCode, phone: string): string {
  if (code && code.areacode) return `+${code.areacode}-${phone || ''}`;
  return phone;
}

export function showRate(value: BigNumber) {
  return `${value.times(100).decimalPlaces(2)}%`;
}

export function calcEstate(it: Estate) {
  const profit = new BigNumber(it.profit || '0');
  const taxfee = new BigNumber(it.taxfee || '0');
  const inPrice = new BigNumber(it.inPrice || '0');

  it.profitShow = showRate(profit);
  it.taxfeeShow = showRate(taxfee);

  const taxfeeAmount = inPrice.times(taxfee).decimalPlaces(2);
  it.taxfeeAmount = taxfeeAmount.toString();
  const outPrice = inPrice.times(profit.plus(1)).decimalPlaces(2);
  it.outPrice = outPrice.toString();
  it.profitPrice = outPrice.minus(inPrice).minus(taxfeeAmount).toString();
}

export const supportedLangs = [
  {
    lang: 'zh-CN',
    text: '中文',
  },
  {
    lang: 'en-US',
    text: 'English',
  },
];

export function fromLangToText(lang: string) {
  const target = _.find(supportedLangs, { lang });
  return target && target.text;
}

export function encryptPwd(pwd: string): string {
  return md5(pwd);
}

export function displayPhone(phone: string): string {
  return `1******${_.takeRight(phone, 4).join('')}`;
}

export function displayTime(time: string | Moment): string {
  return moment(time).format('YYYY-MM-DD HH:mm');
}

export function displayLabel(label?: string | null): string | null | undefined {
  if (label && label.toLowerCase().startsWith('usdt')) {
    return 'VIC';
  }
  return label;
}
