/* 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/utils/actions.ts
var actions_exports = {};
__export(actions_exports, {
  createWildcardRegex: () => createWildcardRegex,
  generateKey: () => generateKey,
  getFieldActionsMap: () => getFieldActionsMap,
  getFilteredEventActionComponents: () => getFilteredEventActionComponents,
  isFuzzySubset: () => isFuzzySubset,
  isInternalField: () => isInternalField,
  isJsonString: () => isJsonString,
  isTimeField: () => isTimeField,
  isWildcardMatch: () => isWildcardMatch,
  rawString: () => rawString,
  shouldShowActionAfterEventtypeFilter: () => shouldShowActionAfterEventtypeFilter,
  shouldShowEventActionAfterFieldFilter: () => shouldShowEventActionAfterFieldFilter,
  testStringWithWildCardString: () => testStringWithWildCardString
});
module.exports = __toCommonJS(actions_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;

// src/utils/actions.ts
var EVENT_ID_TUPLE = ["splunk_server", "index", "_cd"];
var INTERNAL_FIELDS_ARRAY = [
  "_bkt",
  "_cd",
  "_eventtype_color",
  "_fulllinecount",
  "_has_time",
  "_indextime",
  "_kv",
  "_mkv_child",
  "_raw",
  "_serial",
  "_si",
  "_sourcetype",
  "_span",
  "_subsecond"
];
var internalFields = new Set(INTERNAL_FIELDS_ARRAY);
var isInternalField = (field) => internalFields.has(field);
var isTimeField = (field) => field === "_time";
var testStringWithWildCardString = (wildCardString, stringToTest) => {
  const w = wildCardString.replace(/[.+^${}()|[\]\\]/g, "\\$&");
  const re = new RegExp(`^${w.replace(/\*/g, ".*").replace(/\?/g, ".")}$`, "i");
  return re.test(stringToTest);
};
var createWildcardRegex = (value) => {
  if (typeof value !== "string") {
    return false;
  }
  const wordBoundary = "\\b";
  const pattern = value === "*" ? ".*" : `${wordBoundary}${value.replace(/\*/g, ".*")}${wordBoundary}`;
  try {
    return new RegExp(pattern, "g");
  } catch (e) {
    return false;
  }
};
var isFuzzySubset = (subsetArray = [], supersetArray = []) => {
  const intersection = subsetArray.filter((subsetValue) => {
    const subsetValueRegex = createWildcardRegex(subsetValue.toString());
    return subsetValueRegex ? supersetArray.find(
      (supersetValue) => supersetValue && supersetValue.toString().match(subsetValueRegex) !== null
    ) : false;
  });
  return intersection.length === subsetArray.length;
};
var shouldShowActionAfterEventtypeFilter = (event, eventtypeValues = []) => {
  const eventtypeValuesCount = eventtypeValues.length;
  const isStarValue = eventtypeValuesCount === 1 && eventtypeValues[0] === "*";
  if (eventtypeValuesCount === 0 || isStarValue) {
    return true;
  }
  if ("eventtype" in event) {
    const eventtypeArray = Array.isArray(event.eventtype) ? event.eventtype : [event.eventtype];
    return isFuzzySubset(eventtypeValues, eventtypeArray);
  }
  return false;
};
var shouldShowEventActionAfterFieldFilter = (event, fieldFilters = []) => {
  const fieldFiltersCount = fieldFilters.length;
  const isStarValue = fieldFiltersCount === 1 && fieldFilters[0] === "*";
  if (fieldFiltersCount === 0 || isStarValue) {
    return true;
  }
  const fields = Object.keys(event);
  return isFuzzySubset(fieldFilters, fields);
};
var isWildcardMatch = (substring, superstring) => {
  const substringRegex = createWildcardRegex(substring);
  if (!substringRegex) {
    return false;
  }
  return substringRegex.exec(superstring) !== null;
};
var getFieldActionsMap = (event, renderFieldActions = {}, fields = []) => {
  const fieldActionsMap = {};
  Object.keys(renderFieldActions).forEach((fieldActionFieldRaw) => {
    fields.forEach((field) => {
      const currentActions = fieldActionsMap[field] || [];
      const fieldActionsByField = renderFieldActions[fieldActionFieldRaw];
      fieldActionsByField.forEach(({ eventtypeValues, fieldActionComponent }) => {
        if (isWildcardMatch(fieldActionFieldRaw, field) && shouldShowActionAfterEventtypeFilter(event, eventtypeValues)) {
          currentActions.push(fieldActionComponent);
          fieldActionsMap[field] = currentActions;
        }
      });
    });
  });
  return fieldActionsMap;
};
var getFilteredEventActionComponents = (event, renderEventActions = []) => {
  const renderEventActionComponents = [];
  renderEventActions.forEach((eventAction) => {
    const { eventtypeValues, fieldFilters, eventActionComponent } = eventAction;
    if (shouldShowActionAfterEventtypeFilter(event, eventtypeValues) && shouldShowEventActionAfterFieldFilter(event, fieldFilters)) {
      renderEventActionComponents.push(eventActionComponent);
    }
  });
  return renderEventActionComponents;
};
function generateKey(event, index, pageNumber) {
  let key = "";
  for (const field of EVENT_ID_TUPLE) {
    if (event[field]) {
      key = key === "" ? event[field].toString() : `${key}-${event[field]}`;
    }
  }
  return pageNumber ? `${key}-${index}-${pageNumber}` : `${key}-${index}`;
}
function rawString(event) {
  if (lodash_default.isObject(event)) {
    if (event._raw !== void 0) {
      if (lodash_default.isString(event._raw)) {
        return event._raw;
      }
      if (lodash_default.isObject(event._raw) && lodash_default.isString(event._raw.value)) {
        return event._raw.value;
      }
    }
  } else if (lodash_default.isString(event)) {
    return event;
  }
  return JSON.stringify(event);
}
function isJsonString(str) {
  if (!lodash_default.isString(str)) {
    return false;
  }
  try {
    JSON.parse(str);
  } catch (e) {
    return false;
  }
  return true;
}
//# sourceMappingURL=actions.js.map
