//@ts-nocheck
import moment from 'moment';
import { AppEventsLogger } from 'react-native-fbsdk-next';
import analytics from '@react-native-firebase/analytics';

export const formatMoney = (x) => {
  try {
    // if (x) {
    x = x
      .toString()
      .replace(/,/g, '')
      .match(/^[-+]?[0-9]*?[0-9]+$/g)
      .join('');
    // x = Number(x);
    // x = Number.isNaN(x) ? 0 : x;
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    // } else {
    //   return '0';
    // }
  } catch (e) {
    return '';
  }
};

export const getMoney = (x) => {
  try {
    x = x.toString().replace(/,|\./g, '');
    return Number(x);
  } catch (e) {
    return 0;
  }
};

export const removeAccent = (str) => {
  str = str?.toLowerCase();
  str = str.replace(/A|Á|À|Ã|Ạ|Â|Ấ|Ầ|Ẫ|Ậ|Ă|Ắ|Ằ|Ẵ|Ặ/g, 'A');
  str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, 'a');
  str = str.replace(/E|É|È|Ẽ|Ẹ|Ê|Ế|Ề|Ễ|Ệ/, 'E');
  str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, 'e');
  str = str.replace(/I|Í|Ì|Ĩ|Ị/g, 'I');
  str = str.replace(/ì|í|ị|ỉ|ĩ/g, 'i');
  str = str.replace(/O|Ó|Ò|Õ|Ọ|Ô|Ố|Ồ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ỡ|Ợ/g, 'O');
  str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, 'o');
  str = str.replace(/U|Ú|Ù|Ũ|Ụ|Ư|Ứ|Ừ|Ữ|Ự/g, 'U');
  str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, 'u');
  str = str.replace(/Y|Ý|Ỳ|Ỹ|Ỵ/g, 'Y');
  str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, 'y');
  str = str.replace(/Đ/g, 'D');
  str = str.replace(/đ/g, 'd');
  // Some system encode vietnamese combining accent as individual utf-8 characters
  str = str.replace(/\u0300|\u0301|\u0303|\u0309|\u0323/g, ''); // Huyền sắc hỏi ngã nặng
  str = str.replace(/\u02C6|\u0306|\u031B/g, ''); // Â, Ê, Ă, Ơ, Ư
  return str;
};

export const formatDDMMYYY = (date) => {
  if (date) {
    return moment(date).format('DD/MM/YYYY');
  }
  return null;
};

export const EnumGender = [
  {
    id: 0,
    name: 'Nam',
  },
  {
    id: 1,
    name: 'Nữ',
  },
];

export const EnumMaritalStatus = [
  {
    id: 0,
    name: 'Chưa kết hôn',
  },
  {
    id: 1,
    name: 'Đã kết hôn',
  },
];

export const EnumPurpose = [
  {
    id: 3008,
    name: 'Mua sắm trang thiết bị, vật dụng gia đình',
  },
];

export const diffDays = (fromDate, toDate) => {
  const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
  const firstDate = new Date(fromDate);
  const secondDate = new Date(toDate);

  //@ts-ignore
  return Math.round(Math.abs((firstDate - secondDate) / oneDay));
};

export const EnumTypeCar = [
  {
    id: 1,
    name: 'Xe con/xe bán tải',
  },
  {
    id: 2,
    name: 'Xe tải/ xe khách',
  },
];

export const EnumTypeAsset = [
  {
    id: 1,
    name: 'Cá nhân',
  },
  {
    id: 2,
    name: 'Doanh nghiệp',
  },
];

export const EnumRelationshipOption1 = [
  {
    id: 1,
    name: 'Chồng/Vợ',
  },
  {
    id: 2,
    name: 'Mẹ',
  },
  {
    id: 3,
    name: 'Bố',
  },
  {
    id: 4,
    name: 'Anh/em trai',
  },
  {
    id: 5,
    name: 'Chị/em gái',
  },
  {
    id: 13,
    name: 'Con',
  },
];

export const EnumRelationshipOption2 = [
  {
    id: 6,
    name: 'Đồng nghiệp',
  },
  {
    id: 10,
    name: 'Cô/Dì',
  },
  {
    id: 9,
    name: 'Chú',
  },

  {
    id: 8,
    name: 'Bác',
  },
  {
    id: 15,
    name: 'Bạn bè',
  },
];

export const logEventFb = (action: string) => {
  try {
    if (__DEV__) return;
    AppEventsLogger.logEvent(action);
    console.log(`Logged action: ${action}`);
  } catch (error) {
    console.error(`Failed to log action: ${action}`, error);
  }
};

export const logEvent = async (eventName: string, params?: object) => {
  try {
    if (__DEV__) return;
    await analytics().logEvent(eventName, params);
    console.log(`Logged event: ${eventName}`, params);
  } catch (error) {
    console.error(`Failed to log event: ${eventName}`, error);
  }
};
