'use strict';

var core = require('@linaria/core');
var index = require('../../core/Typo/index.js');
var dayjs = require('dayjs');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);

const BASIC_RELATIVE_TIME_CONFIG = [
  {
    unit: "m",
    value: 5
  },
  {
    unit: "m",
    value: 15
  },
  {
    unit: "m",
    value: 30
  },
  {
    unit: "h",
    value: 1
  },
  {
    unit: "h",
    value: 3
  },
  {
    unit: "h",
    value: 6
  },
  {
    unit: "h",
    value: 12
  },
  {
    unit: "d",
    value: 1
  },
  {
    unit: "d",
    value: 2
  },
  {
    unit: "d",
    value: 7
  },
  {
    unit: "d",
    value: 15
  },
  {
    unit: "d",
    value: 30
  }
];
function getTime(year, month, date) {
  return dayjs__default.default(`${year}-${month}-${date}`, "YYYY-M-D");
}
function trimTime(time) {
  return dayjs__default.default(time.format("YYYY-MM-DD"), "YYYY-MM-DD");
}
function isSameDate(first, second) {
  return trimTime(first).isSame(trimTime(second));
}
function getClassNameForDateBlock(range, date, mapOfHighlightDay, disabled) {
  const className = ["date", index.Typo.Label.l1_regular];
  const today = dayjs__default.default(dayjs__default.default().format("YYYY-MM-DD"), "YYY-MM-DD");
  if (today.diff(date, "day") === 0) {
    className.push("today");
  }
  if (range[0] && isSameDate(range[0], date) || range[1] && isSameDate(range[1], date)) {
    className.push("checked");
  }
  if (disabled) {
    className.push("disabled");
  }
  if (mapOfHighlightDay.has(date.format("YYYY-MM-DD"))) {
    className.push("highlight");
  }
  return core.cx(...className);
}
function getDiffMonthAndDate(first, second) {
  const diffDayList = [];
  let max = first;
  let min = second;
  if (first.valueOf() > second.valueOf()) {
    max = first;
    min = second;
  } else if (second.valueOf() > first.valueOf()) {
    max = second;
    min = first;
  }
  const diffDayCount = max.diff(min, "day");
  if (!diffDayCount) {
    return [];
  }
  let time = min;
  diffDayList.push(time.format("YYYY-MM-DD"));
  for (let day = 0; day < diffDayCount; day++) {
    time = time.add(1, "day");
    diffDayList.push(time.format("YYYY-MM-DD"));
  }
  return diffDayList;
}
function time2stringByUnit(time, type) {
  const _time = parseInt(time);
  if (Number.isInteger(_time)) {
    switch (type) {
      case "h": {
        if (_time > 23) {
          return "23";
        } else if (/^0/.test(time)) {
          return time;
        } else {
          return _time.toString();
        }
      }
      case "s":
      case "m": {
        if (_time > 59) {
          return "59";
        } else if (/^0/.test(time)) {
          return time;
        } else {
          return _time.toString();
        }
      }
    }
  }
  return "";
}
function time2string(time) {
  const _time = time.toString();
  if (_time.length === 0) {
    return "00";
  } else if (_time.length === 1) {
    return `0${_time}`;
  } else {
    return _time;
  }
}
function getDateText(date, t) {
  const count = date.value;
  const dateString = {
    d: t("common.day_count", { count }),
    h: t("common.hour_count", { count }),
    m: t("common.minute_count", { count }),
    M: t("common.month_measure_count", { count })
  }[date.unit];
  return `${t("components.past")} ${dateString}`;
}
function checkDateNotInRange(date, minDate, maxDate) {
  if (!minDate && !maxDate) {
    return true;
  }
  if (minDate) {
    const _minDate = dayjs__default.default(minDate);
    if (!_minDate.isValid()) {
      return true;
    }
    if (isSameDate(_minDate, date)) {
      return true;
    }
    if (_minDate.isAfter(date)) {
      return false;
    }
  }
  if (maxDate) {
    const _maxDate = dayjs__default.default(maxDate);
    if (!_maxDate.isValid()) {
      return true;
    }
    if (isSameDate(_maxDate, date)) {
      return true;
    }
    if (_maxDate.isBefore(date)) {
      return false;
    }
  }
  return true;
}
function copyDay(day) {
  return day.clone().set("millisecond", 0);
}

exports.BASIC_RELATIVE_TIME_CONFIG = BASIC_RELATIVE_TIME_CONFIG;
exports.checkDateNotInRange = checkDateNotInRange;
exports.copyDay = copyDay;
exports.getClassNameForDateBlock = getClassNameForDateBlock;
exports.getDateText = getDateText;
exports.getDiffMonthAndDate = getDiffMonthAndDate;
exports.getTime = getTime;
exports.isSameDate = isSameDate;
exports.time2string = time2string;
exports.time2stringByUnit = time2stringByUnit;
exports.trimTime = trimTime;
