/* 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. */
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __pow = Math.pow;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/getFormattedDataColumns.ts
var getFormattedDataColumns_exports = {};
__export(getFormattedDataColumns_exports, {
  getFormattedDataColumns: () => getFormattedDataColumns
});
module.exports = __toCommonJS(getFormattedDataColumns_exports);

// esm-externals:lodash
var lodash_exports = {};
__export(lodash_exports, {
  default: () => lodash_default
});
var defaultImport = __toESM(require("lodash"));
__reExport(lodash_exports, require("lodash"));
var lodash_default = "default" in defaultImport ? defaultImport.default : defaultImport;

// esm-externals:react
var react_exports = {};
__export(react_exports, {
  default: () => react_default
});
var defaultImport2 = __toESM(require("react"));
__reExport(react_exports, require("react"));
var react_default = "default" in defaultImport2 ? defaultImport2.default : defaultImport2;

// 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;
};

// 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;
}

// 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;
}

// esm-externals:lodash/some
var some_exports = {};
__export(some_exports, {
  default: () => some_default
});
var defaultImport3 = __toESM(require("lodash/some"));
__reExport(some_exports, require("lodash/some"));
var some_default = "default" in defaultImport3 ? defaultImport3.default : defaultImport3;

// esm-externals:uplot
var uplot_exports = {};
__export(uplot_exports, {
  default: () => uplot_default
});
var defaultImport4 = __toESM(require("uplot"));
__reExport(uplot_exports, require("uplot"));
var uplot_default = "default" in defaultImport4 ? defaultImport4.default : defaultImport4;

// esm-externals:temporal-polyfill
var temporal_polyfill_exports = {};
__export(temporal_polyfill_exports, {
  default: () => temporal_polyfill_default
});
var defaultImport5 = __toESM(require("temporal-polyfill"));
__reExport(temporal_polyfill_exports, require("temporal-polyfill"));
var temporal_polyfill_default = "default" in defaultImport5 ? defaultImport5.default : defaultImport5;

// esm-externals:string-format-obj
var string_format_obj_exports = {};
__export(string_format_obj_exports, {
  default: () => string_format_obj_default
});
var defaultImport6 = __toESM(require("string-format-obj"));
__reExport(string_format_obj_exports, require("string-format-obj"));
var string_format_obj_default = "default" in defaultImport6 ? defaultImport6.default : defaultImport6;

// src/utilities/formatTimestamp.ts
var EPOCH_SECONDS_MAX_VALUE = Date.now() / 100;

// src/renderers/uplot-utils.ts
var { bars } = uplot_default.paths;
var bars90PercentGap100MaxWidthPaths = bars ? bars({ size: [0.9, 100] }) : void 0;
var isTimeSeriesData = (dataSet) => dataSet && dataSet.fields[0] && dataSet.fields[0] === "_time";

// src/utilities/utils.ts
var DAY = 24 * 60 * 60;
function exists(value) {
  return value !== void 0 && value !== null;
}
function getMergedSeriesConfigForField(seriesConfigMap = {}, scaleConfigMap = {}, field) {
  const defaults = { scale: "y" };
  if (!exists(field)) {
    return defaults;
  }
  const seriesConfig = seriesConfigMap[field];
  const scale = (seriesConfig == null ? void 0 : seriesConfig.scale) || "y";
  const scaleConfig = scaleConfigMap[scale] || {};
  const mergedSeriesConfig = __spreadValues(__spreadValues(__spreadValues({}, defaults), scaleConfig.seriesConfig), seriesConfig);
  return mergedSeriesConfig;
}
var formatKibBytes = (bytes, decimals = 2) => {
  if (!+bytes) {
    return "0B";
  }
  const k = 1024;
  const dm = decimals < 0 ? 0 : decimals;
  const sizes = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return `${parseFloat((bytes / __pow(k, i)).toFixed(dm))}${sizes[i]}`;
};
var valueFormatters = {
  durationMicros: (value) => value,
  rate: (value) => value,
  count: (value) => value,
  bytes: (value) => value,
  percentage: (value) => +value.toFixed(value < 10 ? 2 : 1),
  kibBytes: (value) => value
};
var namedFormatters = {
  durationMicros: (value) => formatDurationMicros(value),
  rate: (value, xAxisResolution) => `${shortenNumber(value / (xAxisResolution || 1))} /s`,
  count: (value) => `${shortenNumber(value)}`,
  bytes: (value) => `${shortenByte(value)}`,
  percentage: (value) => `${shortenNumber(value)}%`,
  kibBytes: (value) => `${formatKibBytes(value, 0)}`
};
var scaleNamedFormatters = __spreadProps(__spreadValues({}, namedFormatters), {
  percentage: (value) => `${shortenNumber(value, value < 10 ? 3 : 2)}%`
});
var getXAxisResolution = (data) => {
  if ((0, lodash_exports.isEmpty)(data.columns)) {
    return 0;
  }
  const timeSeries = data.columns[0];
  const firstNonNullIndex = timeSeries.findIndex((val, i) => val !== null && timeSeries[i + 1] !== null);
  if (firstNonNullIndex === -1) {
    return 0;
  }
  return timeSeries[firstNonNullIndex + 1] - timeSeries[firstNonNullIndex];
};
var formatValueForPlottingWithSeriesConfig = (value, seriesConfig, data) => {
  const formatter = seriesConfig.valueFmt;
  if (!formatter) {
    return value;
  }
  const formatFn = typeof formatter === "string" ? valueFormatters[formatter] : formatter;
  const xAxisResolution = isTimeSeriesData(data) ? getXAxisResolution(data) : null;
  return formatFn(value, xAxisResolution);
};

// src/getFormattedDataColumns.ts
var getFormattedDataColumns = (data, seriesConfig, scaleConfig) => {
  const { fields, columns: originalColumns } = data;
  const [, ...dataColumns] = originalColumns;
  let fieldsIndex = 1;
  const formattedDataColumns = dataColumns.map((dataColumn) => {
    const mergedSeriesConfig = getMergedSeriesConfigForField(
      seriesConfig,
      scaleConfig,
      fields[fieldsIndex]
    );
    if (mergedSeriesConfig.scale !== "y") {
      return dataColumn;
    }
    const formattedDataColumn = dataColumn.map(
      (dataPoint) => dataPoint !== null ? formatValueForPlottingWithSeriesConfig(dataPoint, mergedSeriesConfig, data) : "0"
    );
    fieldsIndex += 1;
    return formattedDataColumn;
  });
  return formattedDataColumns;
};
//# sourceMappingURL=getFormattedDataColumns.js.map
