import { trim } from "lodash-es";
import prettyMs from "pretty-ms";
import { dayjs } from "../../utils/dayjs.mjs";
const DEFAULT_QUICK_RANGES = [
  5 * 60,
  15 * 60,
  30 * 60,
  60 * 60,
  3 * 60 * 60,
  12 * 60 * 60,
  24 * 60 * 60,
  2 * 24 * 60 * 60,
  3 * 24 * 60 * 60
];
const DEFAULT_TIME_RANGE = {
  type: "relative",
  value: 30 * 60
};
const formatDuration = (seconds, short = false) => {
  if (short) {
    return prettyMs(seconds * 1e3, { compact: true });
  } else {
    return prettyMs(seconds * 1e3, { verbose: true });
  }
};
const toTimeRangeValue = (timeRange, offset = 0) => {
  if (timeRange.type === "absolute") {
    return timeRange.value.map((t) => t + offset);
  } else {
    const isFuture = timeRange.isFuture;
    const now = dayjs().unix();
    return isFuture ? [now + offset, now + timeRange.value + offset] : [now - timeRange.value + offset, now + offset];
  }
};
function fromTimeRangeValue(v) {
  return {
    type: "absolute",
    value: [...v]
  };
}
const toURLTimeRange = (timeRange) => {
  if (timeRange.type === "relative") {
    const isFuture = timeRange.isFuture;
    return isFuture ? { from: "now", to: `${timeRange.value}` } : { from: `${timeRange.value}`, to: "now" };
  }
  const timeRangeValue = toTimeRangeValue(timeRange);
  return { from: `${timeRangeValue[0]}`, to: `${timeRangeValue[1]}` };
};
const urlToTimeRange = (urlTimeRange) => {
  if (urlTimeRange.to === "now" || urlTimeRange.from === "now") {
    return { type: "relative", value: Number(urlTimeRange.from), isFuture: urlTimeRange.from === "now" };
  }
  return { type: "absolute", value: [Number(urlTimeRange.from), Number(urlTimeRange.to)] };
};
const urlToTimeRangeValue = (urlTimeRange) => {
  return toTimeRangeValue(urlToTimeRange(urlTimeRange));
};
const DEFAULT_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
const DEFAULT_TIME_FORMAT_WITH_TIMEZONE = "YYYY-MM-DD HH:mm:ss Z";
function addOffsetUTC(time, utcOffset) {
  const targetTime = new Date(time);
  const tzDifference = utcOffset * 60 + targetTime.getTimezoneOffset();
  return new Date(targetTime.getTime() + tzDifference * 60 * 1e3);
}
const getUTCString = (offset) => {
  const offsetStrs = offset.toString().split(".");
  let mm = "00";
  let hh = "";
  const offsetH = Number.parseInt(offsetStrs[0] || "0");
  const offsetM = Number.parseFloat(`0.${offsetStrs[1]}` || "0") * 60;
  hh = Math.abs(offsetH).toString().padStart(2, "0");
  mm = offsetM === 0 ? "00" : offsetM.toString();
  if (offset > 0) {
    return `UTC+${hh}:${mm}`;
  }
  if (offset < 0) {
    return `UTC-${hh}:${mm}`;
  }
  return "UTC±00:00";
};
const timeFormatter = (timeValue, utcOffset, format = DEFAULT_TIME_FORMAT) => {
  if (!timeValue) return "-";
  const time = timeValue instanceof Date || isNaN(Number(timeValue)) ? timeValue : Number(timeValue) * 1e3;
  const currentTZOffsetInHours = utcOffset === void 0 || utcOffset === null ? -((/* @__PURE__ */ new Date()).getTimezoneOffset() / 60) : utcOffset;
  const withUTC = format.indexOf("Z") >= 0;
  return dayjs(addOffsetUTC(time, currentTZOffsetInHours)).format(withUTC ? trim(format, "Z") : format) + (withUTC ? getUTCString(currentTZOffsetInHours) : "");
};
export {
  DEFAULT_QUICK_RANGES,
  DEFAULT_TIME_FORMAT,
  DEFAULT_TIME_FORMAT_WITH_TIMEZONE,
  DEFAULT_TIME_RANGE,
  addOffsetUTC,
  formatDuration,
  fromTimeRangeValue,
  getUTCString,
  timeFormatter,
  toTimeRangeValue,
  toURLTimeRange,
  urlToTimeRange,
  urlToTimeRangeValue
};
