export const convertShortTimeToSecond = (shortTime: string) => {
  if (typeof shortTime !== 'string') return;
  let seconds = null;
  const unit = shortTime[shortTime.length - 1];
  if (unit === 'm') {
    const val = shortTime.split('m')[0];
    seconds = +val * 60;
  } else if (unit === 'd') {
    const val = shortTime.split('d')[0];
    seconds = +val * 24 * 60 * 60;
  }
  return seconds;
};

export const insertTextToString = (str, index, value): string => {
  return str.substr(0, index) + value + str.substr(index);
};

export const convertPermission = (premission: string): number => {
  let digit = parseInt(premission, 2);
  if (isNaN(digit)) {
    const setPermission = new Set(premission.split(''));
    digit = Array.from(setPermission).reduce((pre, current) => {
      switch (current) {
        case 'w':
          return pre + 2;
        case 'r':
          return pre + 4;
        default:
          return pre + 1;
      }
    }, 0);
  }
  return digit;
};
export const URLtoLatLng = (url: string) => {
  const lat = url.replace(/^.+!3d(.+)!4d.+$/, '$1');
  const lng = url.replace(/^.+!4d(.+)!16.+$/, '$1');
  const latLng = {
    lat: parseFloat(lat),
    lng: parseFloat(lng),
  };
  if (isNaN(latLng.lat)) {
    latLng.lat = 0;
  }
  if (isNaN(latLng.lng)) {
    latLng.lng = 0;
  }
  return latLng;
}

export const convertAddressToDetail = (address: string) => {
  const addressArr = address.split(',');
  const city = addressArr.pop().replace(/\d+/g, '') || "";
  const district = addressArr.pop() || '';
  const ward = addressArr.pop() || '';

  const detail = {
    city, district, ward, address

  };
  return detail;
}
export const convertDetailToAddress = (detail: any) => {
  const address = [detail.country, detail.state, detail.city, detail.district, detail.street].filter(e => e).join(', ');
  return address;
}

export const to24HourFormat = (timeStr: string) => {
  const [time, period] = timeStr.split(' ');
  // eslint-disable-next-line prefer-const
  let [hours, minutes] = time.split(':');
  if (!minutes) minutes = '00';
  let hoursNumber = parseInt(hours);

  if (period === 'PM' && hoursNumber !== 12) hoursNumber += 12;
  if (period === 'AM' && hoursNumber === 12) hoursNumber = 0;

  return `${hoursNumber.toString().padStart(2, '0')}:${minutes}`;
}

export const convertWorkdayTiming = (workday_timing: string) => {
  if (!workday_timing) return { open: '00:00', close: '23:59' };
  if (workday_timing == 'Open 24 hours')
    return { open: '00:00', close: '23:59' };
  // convet 8 AM-11 PM to 08:00-23:00
  const [openTime, closeTime] = workday_timing.split('-');
  const openTimeString = to24HourFormat(openTime.trim());
  const closeTimeString = to24HourFormat(closeTime.trim());
  return { open: openTimeString, close: closeTimeString };
}

export const generateOTP = (length = 6) => {
  const digits = '0123456789';
  let OTP = '';
  for (let i = 0; i < length; i++) {
    OTP += digits.charAt(Math.floor(Math.random() * digits.length));
  }
  return OTP;
}

export const removeVietnameseTones = (str: string): string => {
  str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
  str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
  str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
  str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
  str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
  str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
  str = str.replace(/đ/g, "d");
  str = str.replace(/À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ/g, "A");
  str = str.replace(/È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ/g, "E");
  str = str.replace(/Ì|Í|Ị|Ỉ|Ĩ/g, "I");
  str = str.replace(/Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ/g, "O");
  str = str.replace(/Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ/g, "U");
  str = str.replace(/Ỳ|Ý|Ỵ|Ỷ|Ỹ/g, "Y");
  str = str.replace(/Đ/g, "D");
  // Some system encode vietnamese combining accent as individual utf-8 characters
  // Một vài bộ encode coi các dấu mũ, dấu chữ như một kí tự riêng biệt nên thêm hai dòng này
  str = str.replace(/\u0300|\u0301|\u0303|\u0309|\u0323/g, ""); // ̀ ́ ̃ ̉ ̣  huyền, sắc, ngã, hỏi, nặng
  str = str.replace(/\u02C6|\u0306|\u031B/g, ""); // ˆ ̆ ̛  Â, Ê, Ă, Ơ, Ư
  // Remove extra spaces
  // Bỏ các khoảng trắng liền nhau
  // str = str.replace(/ + /g, " ");
  str = str.trim();
  // Remove punctuations
  // Bỏ dấu câu, kí tự đặc biệt
  // str = str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'|\"|\&|\#|\[|\]|~|\$|_|`|-|{|}|\||\\/g," ");
  return str;
}