/* Copyright © 2020 Splunk Inc.
SPLUNK CONFIDENTIAL – Use or disclosure of this material in whole or
in part without a valid written license from Splunk Inc. is PROHIBITED. */
import {
  __export,
  __pow,
  __reExport,
  __spreadProps,
  __spreadValues
} from "./chunk-XC4WXK3L.js";

// src/utilities/scaleTime.ts
var timeScaleMap = {
  ["w" /* Weeks */]: { unit: "w", scaleUp: null, scaleUpValue: null, decimalPlaces: 0 },
  ["d" /* Days */]: {
    unit: "d",
    scaleUp: "w" /* Weeks */,
    scaleUpValue: 7,
    decimalPlaces: 0
  },
  ["h" /* Hours */]: { unit: "h", scaleUp: "d" /* Days */, scaleUpValue: 24, decimalPlaces: 1 },
  ["m" /* Minutes */]: {
    unit: "m",
    scaleUp: "h" /* Hours */,
    scaleUpValue: 60,
    decimalPlaces: 1
  },
  ["s" /* Seconds */]: {
    unit: "s",
    scaleUp: "m" /* Minutes */,
    scaleUpValue: 60,
    decimalPlaces: 2
  },
  ["ms" /* Milliseconds */]: {
    unit: "ms",
    scaleUp: "s" /* Seconds */,
    scaleUpValue: 1e3,
    decimalPlaces: 0
  },
  ["us" /* Microseconds */]: {
    unit: `${String.fromCharCode(181)}s`,
    scaleUp: "ms" /* Milliseconds */,
    scaleUpValue: 1e3,
    decimalPlaces: 0
  },
  ["ns" /* Nanoseconds */]: {
    unit: "ns",
    scaleUp: "us" /* Microseconds */,
    scaleUpValue: 1e3,
    decimalPlaces: 0
  }
};
function scaleTime(time, startingScale = "us" /* Microseconds */, decimalPlaces) {
  if (!time || time === 0) {
    return {
      scale: timeScaleMap["ms" /* Milliseconds */].unit,
      value: 0
    };
  }
  let iScale = timeScaleMap[startingScale];
  let timeValue = time;
  let sign = 1;
  if (time < 0) {
    sign = -1;
    timeValue = Math.abs(time);
  }
  while (iScale.scaleUpValue !== null && timeValue >= iScale.scaleUpValue && iScale.scaleUp) {
    timeValue /= iScale.scaleUpValue;
    iScale = timeScaleMap[iScale.scaleUp];
  }
  timeValue *= sign;
  return {
    scale: iScale.unit,
    value: timeValue.toFixed(typeof decimalPlaces !== "undefined" ? decimalPlaces : iScale.decimalPlaces)
  };
}

// src/utilities/formatDuration.ts
var formatDurationMicros = (tickValue, decimalPlaces) => {
  const { value, scale } = scaleTime(tickValue, "us" /* Microseconds */, decimalPlaces || void 0);
  return parseFloat(value.toString()).toString() + scale;
};

// esm-externals:lodash
var lodash_exports = {};
__export(lodash_exports, {
  default: () => lodash_default
});
__reExport(lodash_exports, lodash_star);
import * as defaultImport from "lodash";
import * as lodash_star from "lodash";
var lodash_default = "default" in defaultImport ? defaultImport.default : defaultImport;

// esm-externals:temporal-polyfill
var temporal_polyfill_exports = {};
__export(temporal_polyfill_exports, {
  default: () => temporal_polyfill_default
});
__reExport(temporal_polyfill_exports, temporal_polyfill_star);
import * as defaultImport2 from "temporal-polyfill";
import * as temporal_polyfill_star from "temporal-polyfill";
var temporal_polyfill_default = "default" in defaultImport2 ? defaultImport2.default : defaultImport2;

// esm-externals:string-format-obj
var string_format_obj_exports = {};
__export(string_format_obj_exports, {
  default: () => string_format_obj_default
});
__reExport(string_format_obj_exports, string_format_obj_star);
import * as defaultImport3 from "string-format-obj";
import * as string_format_obj_star from "string-format-obj";
var string_format_obj_default = "default" in defaultImport3 ? defaultImport3.default : defaultImport3;

// src/utilities/formatTimestamp.ts
var EPOCH_SECONDS_MAX_VALUE = Date.now() / 100;
var TWELVEHOUR = {
  AM: "AM",
  PM: "PM"
};
var datePartInput = {
  name: "date",
  valueFormatString: "{day} {month} {year}",
  labelFormatString: "{month} {day} {year}"
};
var dateCompactPartInput = {
  name: "date",
  valueFormatString: "{day} {month}",
  labelFormatString: "{month} {day}"
};
var dateVerbosePartInput = {
  name: "date",
  valueFormatString: "{weekday} {day} {month} {year}",
  labelFormatString: "{weekday} {month} {day} {year}"
};
var timePartInput = {
  name: "time",
  valueFormatString: "{hour}:{minute}{meridiem}",
  labelFormatString: "{hour} {minute}{meridiem}"
};
var timePrecisePartInput = {
  name: "time",
  valueFormatString: "{hour}:{minute}:{second}{meridiem}",
  labelFormatString: "{hour} {minute} and {second} seconds{meridiem}"
};
var timeExtraPrecisePartInput = {
  name: "time",
  valueFormatString: "{hour}:{minute}:{second}.{fractionalSecond}{meridiem}",
  labelFormatString: "{hour} {minute} and {second} point {fractionalSecond} seconds{meridiem}"
};
var timezonePartInput = {
  name: "timeZone",
  valueFormatString: "{timeZoneName}",
  labelFormatString: "{timeZoneName}"
};
var formatPartsMap = {
  date: [datePartInput],
  dateCompact: [dateCompactPartInput],
  dateVerbose: [dateVerbosePartInput],
  time: [timePartInput, timezonePartInput],
  timeCompact: [timePartInput],
  timePrecise: [timePrecisePartInput, timezonePartInput],
  timeExtraPrecise: [timeExtraPrecisePartInput, timezonePartInput],
  dateTime: [datePartInput, timePartInput, timezonePartInput],
  dateTimeNoTimezone: [datePartInput, timePartInput],
  dateCompactTime: [dateCompactPartInput, timePartInput, timezonePartInput],
  dateTimeCompact: [datePartInput, timePartInput],
  dateCompactTimeCompact: [dateCompactPartInput, timePartInput],
  dateTimePrecise: [datePartInput, timePrecisePartInput, timezonePartInput],
  dateTimePreciseNoTimezone: [datePartInput, timePrecisePartInput],
  dateTimeExtraPrecise: [datePartInput, timeExtraPrecisePartInput, timezonePartInput],
  dateVerboseTime: [dateVerbosePartInput, timePartInput, timezonePartInput],
  dateVerboseTimePrecise: [dateVerbosePartInput, timePrecisePartInput, timezonePartInput],
  dateVerboseTimeExtraPrecise: [dateVerbosePartInput, timeExtraPrecisePartInput, timezonePartInput],
  isoDate: [
    {
      name: "date",
      valueFormatString: "{year}-{monthNumeric}-{dayPadded}",
      labelFormatString: ""
    }
  ],
  isoTime: [
    {
      name: "time",
      valueFormatString: "{hourPadded}:{minute}:{second}",
      labelFormatString: ""
    }
  ],
  isoLocal: [
    {
      name: "dateTime",
      valueFormatString: "{year}-{monthNumeric}-{dayPadded}T{hourPadded}:{minute}:{second}.{fractionalSecond}",
      labelFormatString: ""
    }
  ],
  isoWithOffset: [
    {
      name: "dateTime",
      valueFormatString: "{year}-{monthNumeric}-{dayPadded}T{hourPadded}:{minute}:{second}.{fractionalSecond}{timeZoneOffset}",
      labelFormatString: ""
    }
  ]
};
var zdtToValueObject = (zdt, twelveHourTimeFormat) => {
  const fmt = new Intl.DateTimeFormat("en-US", {
    hour: "numeric",
    minute: "2-digit",
    second: "2-digit",
    fractionalSecondDigits: 3,
    year: "numeric",
    month: "short",
    day: "numeric",
    weekday: "short",
    timeZone: zdt.timeZone.toString(),
    hourCycle: "h23"
  });
  const timeZoneName = (0, lodash_exports.last)(
    Intl.DateTimeFormat(void 0, {
      timeZoneName: "short",
      timeZone: zdt.timeZone.toString()
    }).format(zdt.epochMilliseconds).split(" ")
  ) || "";
  const parts = fmt.formatToParts(zdt.epochMilliseconds);
  const obj = Object.fromEntries(parts.map((part) => [part.type, part.value]));
  const hour = twelveHourTimeFormat ? String(zdt.hour % 12 || 12) : obj.hour;
  const twelveFormatMeridiem = ` ${zdt.hour >= 12 ? TWELVEHOUR.PM : TWELVEHOUR.AM}`;
  return __spreadProps(__spreadValues({}, obj), {
    timeZoneName,
    hour,
    hourPadded: obj.hour,
    dayPadded: parseInt(obj.day, 10) > 9 ? obj.day : `0${obj.day}`,
    monthNumeric: zdt.month > 9 ? String(zdt.month) : `0${zdt.month}`,
    meridiem: twelveHourTimeFormat ? twelveFormatMeridiem : "",
    timeZoneOffset: zdt.offset
  });
};
var formatTimestamp = (ts, formatName, localeInfo) => {
  const tsAsMillis = Math.abs(ts) <= EPOCH_SECONDS_MAX_VALUE ? ts * 1e3 : ts;
  const tsAsInstant = temporal_polyfill_exports.Temporal.Instant.fromEpochMilliseconds(tsAsMillis);
  const tsAsZdt = tsAsInstant.toZonedDateTimeISO(localeInfo.tz);
  const formatString = formatPartsMap[formatName].map((part) => part.valueFormatString).join(" ");
  const obj = zdtToValueObject(tsAsZdt, localeInfo.twelveHourTimeFormat);
  return string_format_obj_default(formatString, obj);
};

// src/utilities/shortenNumber.ts
var generateShortenNumberComponents = (num, decimalPlaces) => {
  let str;
  let suffix = "";
  let value;
  if (num === 0 || !num) {
    value = 0;
    return { value, str: "0", suffix };
  }
  if (typeof decimalPlaces === "undefined") {
    decimalPlaces = num < 10 ? 2 : 1;
  }
  num = +num.toFixed(decimalPlaces);
  const factor = __pow(10, decimalPlaces);
  if (num === 0) {
    if (decimalPlaces === 0) {
      str = "<1";
    } else {
      str = `<0.${"0".repeat(decimalPlaces - 1)}1`;
    }
  } else if (num < 1e3) {
    value = num;
    str = value;
  } else if (num < 1e6) {
    value = Math.floor(num / (1e3 / factor)) / factor;
    str = value;
    suffix = "k";
  } else if (num < 1e9) {
    value = Math.floor(num / (1e6 / factor)) / factor;
    str = value;
    suffix = "m";
  } else if (num < 1e12) {
    value = Math.floor(num / (1e9 / factor)) / factor;
    str = value;
    suffix = "b";
  } else if (num < 1e15) {
    value = Math.floor(num / (1e12 / factor)) / factor;
    str = value;
    suffix = "t";
  }
  return { value, str: String(str), suffix };
};
function shortenNumber(num, decimalPlaces) {
  const { str, suffix } = generateShortenNumberComponents(num, decimalPlaces);
  return str + suffix;
}

// src/utilities/shortenByte.ts
function shortenByte(num, decimalPlaces = 1) {
  let str;
  let suffix = "";
  if (!num) {
    return "0B";
  }
  decimalPlaces = decimalPlaces || 0;
  num = +num.toFixed(decimalPlaces);
  const factor = __pow(10, decimalPlaces);
  if (num < 1e3) {
    str = num;
    suffix = "B";
  } else if (num < 1e6) {
    str = Math.floor(num / (1e3 / factor)) / factor;
    suffix = "KB";
  } else if (num < 1e9) {
    str = Math.floor(num / (1e6 / factor)) / factor;
    suffix = "MB";
  } else if (num < 1e12) {
    str = Math.floor(num / (1e9 / factor)) / factor;
    suffix = "GB";
  } else if (num < 1e15) {
    str = Math.floor(num / (1e12 / factor)) / factor;
    suffix = "TB";
  } else if (num < 1e18) {
    str = Math.floor(num / (1e15 / factor)) / factor;
    suffix = "PB";
  }
  return str + suffix;
}

export {
  scaleTime,
  formatDurationMicros,
  lodash_exports,
  temporal_polyfill_exports,
  formatTimestamp,
  shortenNumber,
  shortenByte
};
//# sourceMappingURL=chunk-FWZM3EJJ.js.map
