'use strict';

var dayjs = require('dayjs');
var customParseFormat = require('dayjs/plugin/customParseFormat');
var UTC = require('dayjs/plugin/utc');
var _ = require('lodash');

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

var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);
var customParseFormat__default = /*#__PURE__*/_interopDefault(customParseFormat);
var UTC__default = /*#__PURE__*/_interopDefault(UTC);
var ___default = /*#__PURE__*/_interopDefault(_);

dayjs__default.default.extend(UTC__default.default);
dayjs__default.default.extend(customParseFormat__default.default);
const DEFAULT_TIME = dayjs__default.default("00:00", "HH:mm");
function getTime(cronTime, isUTC = false) {
  const { hour, minute } = cronTime.unitMap;
  const _hour = Object.keys(hour)[0];
  const _minute = Object.keys(minute)[0];
  if (isUTC) {
    return dayjs__default.default.utc(`${_hour}:${_minute}`, "HH:mm").local();
  }
  return dayjs__default.default(`${_hour}:${_minute}`, "HH:mm");
}
function getMode(source) {
  if (source.startsWith("@x-every-n-day")) {
    return "day";
  }
  if (source.startsWith("@x-every-n-week")) {
    return "week";
  }
  const [, , , monthDay, month] = source.split(" ");
  if (!monthDay || !month) {
    return "day";
  }
  if (!monthDay.includes("*") || month !== "*") {
    return "month";
  }
  return "day";
}
function getDaily(mode, source, time, defaultTime = DEFAULT_TIME) {
  const matchedArr = source.match(/@x-every-n-day\(\d+,\d+,(\d+)\)/);
  const step = matchedArr ? parseInt(matchedArr[1], 10) : 1;
  return {
    step,
    time: mode === "day" ? time : defaultTime
  };
}
function toDailyString(step, time) {
  return `@x-every-n-day(${time.minute()},${time.hour()},${step || 1})`;
}
function getWeekly(mode, source, time, defaultTime = DEFAULT_TIME) {
  const matchedArr = source.match(/@x-every-n-week\(\d+,\d+,(\d+),(.+)\)/);
  const step = matchedArr ? parseInt(matchedArr[1], 10) : 1;
  const days = matchedArr ? matchedArr[2] === "*" ? [] : matchedArr[2].split(",").map((v) => parseInt(v, 10)) : [];
  return {
    step,
    days,
    time: mode === "week" ? time : defaultTime
  };
}
function toWeeklyString(step, time, days) {
  return `@x-every-n-week(${time.minute()},${time.hour()},${step || 1},${days.join(",") || "*"})`;
}
function getMonthly(mode, month, day, time, defaultTime = DEFAULT_TIME) {
  return {
    step: month === "*" ? 1 : parseInt(___default.default.last(month) || "1", 10),
    days: day === "*" || mode !== "month" || !month ? [] : day.split(",").map((v) => parseInt(v, 10)),
    time: mode === "month" ? time : defaultTime
  };
}
function toMonthlyString(step, time, days) {
  return [
    0,
    time.minute(),
    time.hour(),
    days.join(",") || "*",
    step > 1 ? `*/${step}` : "*",
    "*"
  ].join(" ");
}

exports.DEFAULT_TIME = DEFAULT_TIME;
exports.getDaily = getDaily;
exports.getMode = getMode;
exports.getMonthly = getMonthly;
exports.getTime = getTime;
exports.getWeekly = getWeekly;
exports.toDailyString = toDailyString;
exports.toMonthlyString = toMonthlyString;
exports.toWeeklyString = toWeeklyString;
