export const isValidDateString = (str) => {
  const strVal = String(str);
  const re = /^\d{2}\/\d{2}\/\d{4}$/;
  if (!re.test(strVal)) {
    return false;
  }
  return !isNaN(new Date(strVal));
};

export const isValidEmailString = (str) => {
  const re =
    /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(String(str).toLowerCase());
};

export const isValidZipCodeString = (str) => {
  const re = /^\d{5}/;
  return re.test(String(str));
};

export const isValidTelephoneString = (str) => {
  const re = /^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/;
  return re.test(String(str));
};

export const capFirstChar = (string) => {
  return string?.charAt(0).toUpperCase() + string?.slice(1);
};

export const remArtifacts = (string) => {
  string.replace("request no.", "");
};

export const capMonths = (string) => {
  if (!string) {
    return;
  }
  const months = [
    "january",
    "february",
    "march",
    "april",
    "may",
    "june",
    "july",
    "august",
    "september",
    "october",
    "november",
    "december",
  ];
  let pre = null;
  let post = null;
  months.forEach((month) => {
    if (string?.includes(month)) {
      pre = string.substring(0, string?.indexOf(month));
      const temp = string.slice(string.indexOf(month));
      post = temp?.charAt(0).toUpperCase() + temp?.slice(1);
    }
  });
  if (pre) {
    if (post) {
      return pre + post;
    }
  } else {
    return string;
  }
};

export const remSig = (string) => {
  if (!string) {
    return;
  }
  if (!string?.includes("respectfully")) {
    return string;
  }
  const temp = string?.substring(0, string.indexOf("respectfully"));
  return temp;
};

export const scrubAll = (string) => {
  if (!string) {
    return;
  }
  const all = capFirstChar(remSig(capMonths(string)));
  const final = all.replace("first set of admissions", "");
  const final2 = final.replace("request", "");
  return final2;
};

export const insertAt = (string) => {
  if (!string) {
    return;
  }
  const toFind = "RESPONDENT";
  const char = "\n";
  function findPosition(string, toFind) {
    return string?.lastIndexOf(toFind);
  }
  const pos = findPosition(string, toFind);

  return string.slice(0, pos) + char + string.slice(pos);
};
