import moment from "moment";

const resetTime = () => {
  return Math.round(Number(moment().format("X")) / 86400 - 1) * 86400 - 0;
};

export const parseFilters = (filterNew: string, filterDate: string) => {
  const filters: any = {};

  if (filterNew === "Новые") filters.new = true;
  if (filterDate.length !== 0 && filterDate !== "За всё время") {
    filters.to = new Date();

    let resetedCurrentTime = Number(resetTime());
    if (filterDate === "Сегодня")
      filters.from = moment.unix(resetedCurrentTime)._d;
    if (filterDate === "Вчера")
      filters.from = moment.unix(resetedCurrentTime - 86400)._d;
    if (filterDate === "Последние 7 дней")
      filters.from = moment.unix(resetedCurrentTime - 604800)._d;
    if (filterDate === "Последние 30 дней")
      filters.from = moment.unix(resetedCurrentTime - 2592000)._d;
    if (filterDate === "Этот месяц") {
      let date = new Date(),
        y = date.getFullYear(),
        m = date.getMonth();
      filters.from = new Date(y, m, 1);
    }
  }
  return filters;
};

export const DateDefinition = (result: string) => {
  //определяем когда был получен результат - вчера, сегодня или число и месяц
  let restime = new Date(result);
  let timeCompleting = Date.parse(String(result));
  const timeNow = Date.now();
  let dayResult;
  if (timeNow - timeCompleting < 86400000) {
    dayResult = "Сегодня";
  }
  if (172800000 > timeNow - timeCompleting > 86400000) {
    dayResult = "Вчера";
  } else {
    dayResult = restime.toLocaleDateString();
  }
  return dayResult;
};
export const TimeDefinition = (result: string) => {
  //достаём время
  let timeResult = new Date(result).toLocaleTimeString().slice(0, -3);
  return timeResult;
};
