{"size": 2263, "ext": "js", "max_stars_repo_path": "server/util/helpers/haravan.helper.js", "max_stars_repo_name": "Hoangtt4696/Project-2", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/util/helpers/haravan.helper.js", "max_issues_repo_name": "Hoangtt4696/Project-2", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/util/helpers/haravan.helper.js", "max_forks_repo_name": "Hoangtt4696/Project-2", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0918367347, "max_line_length": 91, "alphanum_fraction": 0.5965532479}
import { validateEmail } from './common.helper';

const cultures = [
  {
    code: 'vi-VN',
    thousands: ',',
    decimal: '.',
    numberdecimal: 0,
    money_format: '{{amount}}',
  },
  {
    code: 'en-US',
    thousands: ',',
    decimal: '.',
    numberdecimal: 2,
    money_format: '{{amount}}',
  },
];

const getCulture = (code = '') => {
  const foundCultures = cultures.filter(culture => culture.code === code);

  return foundCultures.length ? foundCultures[0] : cultures[0];
};

const haravanFormat = getCulture();
const moneyFormat = '{{amount}}';

export const floatToString = ({ numeric, decimals }) => {
  const amount = numeric.toFixed(decimals).toString();

  amount.replace('.', haravanFormat.decimal);

  if (amount.match(`^[\.${haravanFormat.decimal}]\d+`)) {
    return `0${amount}`;
  }

  return amount;
};

export const formatMoney = ({ number, format }) => {
  let value = '';
  const patt = /\{\{\s*(\w+)\s*\}\}/;
  const formatString = (format || moneyFormat);
  const addCommas = (moneyString) => {
    return moneyString.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, `$1${haravanFormat.thousands}`);
  };

  number = number * 100;
  number = number / 100;

  if (typeof number === 'string') {
    number = number.replace(haravanFormat.thousands, '');
  }

  switch (formatString.match(patt)[1]) {
    case 'amount':
      value = addCommas(floatToString({
        numeric: number,
        decimals: haravanFormat.numberdecimal,
      }));
      break;
    case 'amount_no_decimals':
      value = addCommas(floatToString({
        numeric: number,
        decimals: 0,
      }));
      break;
    case 'amount_with_comma_separator':
      value = floatToString({
        numeric: number,
        decimals: haravanFormat.numberdecimal,
      }).replace(/\./, ',');
      break;
    case 'amount_no_decimals_with_comma_separator':
      value = addCommas(floatToString({
        numeric: number,
        decimals: 0,
      })).replace(/\./, ',');
      break;
  }

  return formatString.replace(patt, value);
};

export const validateOrderEmail = email => {
  const ignoredEmail = [
    'guest+23523535@haravan.com',
    'guest@haravan.com',
  ];

  if (ignoredEmail.includes(email)) {
    return false;
  }

  return validateEmail(email);
};
