/* 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 __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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/TypeSafeValue.ts
var TypeSafeValue_exports = {};
__export(TypeSafeValue_exports, {
  TypeSafeValue: () => TypeSafeValue
});
module.exports = __toCommonJS(TypeSafeValue_exports);

// esm-externals:@splunk/moment
var moment_exports = {};
__export(moment_exports, {
  default: () => moment_default
});
var defaultImport = __toESM(require("@splunk/moment"));
__reExport(moment_exports, require("@splunk/moment"));
var moment_default = "default" in defaultImport ? defaultImport.default : defaultImport;

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

// esm-externals:@splunk/visualization-schemas
var visualization_schemas_exports = {};
__export(visualization_schemas_exports, {
  default: () => visualization_schemas_default
});
var defaultImport3 = __toESM(require("@splunk/visualization-schemas"));
__reExport(visualization_schemas_exports, require("@splunk/visualization-schemas"));
var visualization_schemas_default = "default" in defaultImport3 ? defaultImport3.default : defaultImport3;

// src/utils/namedColors.ts
var namedColors_default = [
  "transparent",
  "aliceblue",
  "antiquewhite",
  "aqua",
  "aquamarine",
  "azure",
  "beige",
  "bisque",
  "black",
  "blanchedalmond",
  "blue",
  "blueviolet",
  "brown",
  "burlywood",
  "cadetblue",
  "chartreuse",
  "chocolate",
  "coral",
  "cornflowerblue",
  "cornsilk",
  "crimson",
  "cyan",
  "darkblue",
  "darkcyan",
  "darkgoldenrod",
  "darkgray",
  "darkgreen",
  "darkgrey",
  "darkkhaki",
  "darkmagenta",
  "darkolivegreen",
  "darkorange",
  "darkorchid",
  "darkred",
  "darksalmon",
  "darkseagreen",
  "darkslateblue",
  "darkslategray",
  "darkslategrey",
  "darkturquoise",
  "darkviolet",
  "deeppink",
  "deepskyblue",
  "dimgray",
  "dimgrey",
  "dodgerblue",
  "firebrick",
  "floralwhite",
  "forestgreen",
  "fuchsia",
  "gainsboro",
  "ghostwhite",
  "gold",
  "goldenrod",
  "gray",
  "green",
  "greenyellow",
  "grey",
  "honeydew",
  "hotpink",
  "indianred",
  "indigo",
  "ivory",
  "khaki",
  "lavender",
  "lavenderblush",
  "lawngreen",
  "lemonchiffon",
  "lightblue",
  "lightcoral",
  "lightcyan",
  "lightgoldenrodyellow",
  "lightgray",
  "lightgreen",
  "lightgrey",
  "lightpink",
  "lightsalmon",
  "lightseagreen",
  "lightskyblue",
  "lightslategray",
  "lightslategrey",
  "lightsteelblue",
  "lightyellow",
  "lime",
  "limegreen",
  "linen",
  "magenta",
  "maroon",
  "mediumaquamarine",
  "mediumblue",
  "mediumorchid",
  "mediumpurple",
  "mediumseagreen",
  "mediumslateblue",
  "mediumspringgreen",
  "mediumturquoise",
  "mediumvioletred",
  "midnightblue",
  "mintcream",
  "mistyrose",
  "moccasin",
  "navajowhite",
  "navy",
  "oldlace",
  "olive",
  "olivedrab",
  "orange",
  "orangered",
  "orchid",
  "palegoldenrod",
  "palegreen",
  "paleturquoise",
  "palevioletred",
  "papayawhip",
  "peachpuff",
  "peru",
  "pink",
  "plum",
  "powderblue",
  "purple",
  "rebeccapurple",
  "red",
  "rosybrown",
  "royalblue",
  "saddlebrown",
  "salmon",
  "sandybrown",
  "seagreen",
  "seashell",
  "sienna",
  "silver",
  "skyblue",
  "slateblue",
  "slategray",
  "slategrey",
  "snow",
  "springgreen",
  "steelblue",
  "tan",
  "teal",
  "thistle",
  "tomato",
  "turquoise",
  "violet",
  "wheat",
  "white",
  "whitesmoke",
  "yellow",
  "yellowgreen"
];

// src/utils/colorUtils.ts
function isColor(color = "") {
  if (typeof color !== "string") {
    return false;
  }
  if (color.startsWith("#")) {
    color = color.substring(1);
    return [3, 4, 6, 8].indexOf(color.length) > -1 && !Number.isNaN(parseInt(color, 16));
  }
  const trimmedColor = color.trim();
  if (visualization_schemas_exports.rgbColorPattern.test(trimmedColor) || visualization_schemas_exports.rgbaColorPattern.test(trimmedColor)) {
    return true;
  }
  return namedColors_default.includes(color.toLowerCase());
}

// src/DataPoint.ts
var _DataPoint = class _DataPoint {
  static isDataPoint(o) {
    return o instanceof _DataPoint;
  }
  static fromRaw(value) {
    return new _DataPoint("", TypeSafeValue.fromRaw(value));
  }
  /**
   *
   * @param {string} field data field
   * @param {object} value data value + it's type (number, string, time, color, geojson)
   */
  constructor(field, value) {
    this.field = field;
    this.setValue(value);
    _DataPoint.count += 1;
  }
  getValue() {
    return this.value;
  }
  /**
   * Sets the data point's value to a static TypedValue.
   * @param {TypedValue} v
   */
  setValue(v) {
    this.value = TypeSafeValue.from(v);
  }
  /**
   * Get only value of the data point.
   * @returns {string|number|GeoJsonDataType|null}
   */
  getRawValue() {
    return this.value.toRawValue();
  }
  /**
   * Get only the coerced value of the data point.
   * @returns {string|number|null}
   */
  getCoercedValue() {
    return this.value.toRawCoercedValue();
  }
  /**
   * Returns the data field of the point.
   * @public
   * @returns {DataPoint<'string'>}
   */
  getField() {
    return _DataPoint.fromRaw(this.field);
  }
  /**
   * Returns the data type of the point.
   * @public
   * @returns {string}
   */
  getType() {
    return this.value.type;
  }
};
_DataPoint.count = 0;
var DataPoint = _DataPoint;

// src/utils/types.ts
var supportedDateFormats = [
  "YYYY-MM-DD",
  // HTML5 date
  moment_default.ISO_8601,
  "YYYY-MM-DDTHH:mm",
  // HTML5 date local
  "YYYY-MM-DDTHH:mm:ss.SSS",
  // HTML5 date local milliseconds
  "YYYY-MM-DDTHH:mm:ss",
  // HTML5 date local seconds
  "YYYY-MM-DD HH:MM",
  "YYYY-MM-DD HH:MM:SS",
  "YYYY-MM-DD HH:MM:SS.SSS"
];
function isNumber(dataPoint) {
  return dataPoint !== null && !(0, lodash_exports.isBoolean)(dataPoint) && dataPoint !== "" && (0, lodash_exports.isFinite)(+dataPoint) && (0, lodash_exports.isNumber)(+dataPoint);
}
function isGeoJsonObject(dataPoint) {
  if (!dataPoint) return false;
  let parsedDataPoint;
  try {
    parsedDataPoint = (0, lodash_exports.isObject)(dataPoint) ? dataPoint : JSON.parse(dataPoint);
  } catch (e) {
    return false;
  }
  if (parsedDataPoint == null) {
    return false;
  }
  return Object.prototype.hasOwnProperty.call(parsedDataPoint, "coordinates") && parsedDataPoint.coordinates != null && Array.isArray(parsedDataPoint.coordinates) && Object.prototype.hasOwnProperty.call(parsedDataPoint, "type") && parsedDataPoint.type === "MultiPolygon";
}
function isTime(dataPoint) {
  if (!dataPoint) {
    return false;
  }
  return typeof dataPoint === "string" ? moment_default(dataPoint, supportedDateFormats, true).isValid() : moment_default(dataPoint).isValid();
}
function isString(dataPoint) {
  return typeof dataPoint === "string" && dataPoint.length > 0;
}
var getDataTypeForPoint = (dataPoint, metaData) => {
  if (canInferTypeFromMeta(metaData)) {
    return getDataTypeForMeta(metaData);
  }
  return memoizedGetDataTypeForValue(dataPoint);
};
var getDataTypeForValue = (dataPoint) => {
  if (Array.isArray(dataPoint)) {
    if (dataPoint.length > 1 && dataPoint[0] === "##__SPARKLINE__##") {
      return "sparkline";
    }
    return "array";
  }
  if (isGeoJsonObject(dataPoint)) {
    return "geojson";
  }
  if ((0, lodash_exports.isObject)(dataPoint)) {
    return "unknown";
  }
  if (isNumber(dataPoint)) {
    return "number";
  }
  if (isString(dataPoint) && isColor(dataPoint)) {
    return "color";
  }
  if (isTime(dataPoint)) {
    return "time";
  }
  if (isString(dataPoint)) {
    return "string";
  }
  if (dataPoint === null) {
    return "null";
  }
  return "unknown";
};
var memoizedGetDataTypeForValue = (0, lodash_exports.memoize)(getDataTypeForValue);
var splTypesToDSLDataTypes = {
  str: "string",
  num: "number"
};
var canInferTypeFromMeta = (metaData = {}) => {
  const { name, type = "" } = metaData;
  return splTypesToDSLDataTypes[type] !== void 0 || name === "_time";
};
var getDataTypeForMeta = (metaData = {}) => {
  const { name, type } = metaData;
  if (name === "_time") {
    return "time";
  }
  const typeMatch = splTypesToDSLDataTypes[type];
  if (typeMatch !== void 0) {
    return typeMatch;
  }
  return "unknown";
};

// src/utils/formatterUtils.ts
var timeToSplunkMoment = (time) => {
  if (moment_default.getDefaultSplunkTimezone()) {
    return moment_default.newSplunkTime({ time });
  }
  return moment_default(time);
};
var formatTimeWithTimezoneCorrection = (time, format = moment_default.defaultFormat) => {
  const utcOffset = moment_default.parseZone(time).utcOffset();
  const momentTime = timeToSplunkMoment(time).utcOffset(utcOffset);
  return momentTime.format(format);
};

// src/TypeSafeValue.ts
var TypeSafeValue = class _TypeSafeValue {
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  constructor(type, value, coercedValue) {
    /**
     * isTypeSafe. This is a marker field. The presence of this field can be used at runtime to determine if TypedValue is rigorous,
     * loose. Loose TypedValue such as {type:'number', value:'cat'} are handy for testing, but can lie. Obviously 'cat'
     * is not a number. If the isTypeSafe field is present then the instance is a TypeSafeValue
     * @type {boolean}
     */
    this.isTypeSafe = true;
    this.type = type;
    this.value = value;
    this.coercedValue = coercedValue;
  }
  /**
   * returns a TypeSafeValue, either by converting the non TypeSafeValue to
   * a TypeSafeValue, or by simply returning the passed-in TypeSafeValue
   * @param {TypedValue<T>} typedValue
   * @returns {TypeSafeValue<T>}
   */
  static from(typedValue) {
    if (typedValue.isTypeSafe) {
      return typedValue;
    }
    const { value, type } = typedValue;
    const [coercedValue] = _TypeSafeValue.coerceValue(typedValue);
    return new _TypeSafeValue(type, value, coercedValue);
  }
  /**
   * Creates a TypeSafeValue from a raw value
   * @param value
   * @returns {TypeSafeValue<DataType>}
   */
  static fromRaw(value) {
    const type = getDataTypeForPoint(value);
    return new _TypeSafeValue(type, value, value);
  }
  /**
   * attempts to coerce the provided value to the provided type. Returns tuple
   * of the coerced value and a boolean telling if the coercion was clean (true)
   * or if the coercion was likely produced an unusable result, such as NaN for
   * a number, or '' for a color.
   * @param {TypedValue<T>} typedValue
   * @returns {[any]}
   */
  static coerceValue(typedValue) {
    const { type, value } = typedValue;
    let coercedValue = null;
    try {
      switch (type) {
        case "number": {
          coercedValue = Number(value);
          break;
        }
        case "time": {
          const isOk = isTime(value);
          if (value instanceof Date) {
            coercedValue = value;
          } else if (!isOk) {
            coercedValue = "Invalid Date";
          } else {
            const timeString = formatTimeWithTimezoneCorrection(value);
            coercedValue = timeString;
          }
          break;
        }
        case "string": {
          coercedValue = value.toString();
          break;
        }
        case "color": {
          coercedValue = value;
          break;
        }
        case "sparkline": {
          coercedValue = value;
          break;
        }
        case "array": {
          coercedValue = value;
          break;
        }
        case "null": {
          coercedValue = value;
          break;
        }
        case "geojson": {
          coercedValue = value;
          break;
        }
        default: {
          coercedValue = "";
        }
      }
    } catch (e) {
    }
    return [coercedValue];
  }
  toRawCoercedValue() {
    switch (this.type) {
      case "time":
      case "sparkline":
      case "array":
      case "number":
      case "string":
      case "color":
      case "geojson":
      default:
        return this.coercedValue;
    }
  }
  toRawValue() {
    switch (this.type) {
      case "time":
      case "sparkline":
      case "array":
      case "number":
      case "string":
      case "color":
      case "geojson":
      default:
        return this.value;
    }
  }
};
//# sourceMappingURL=TypeSafeValue.js.map
