/* 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 __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
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);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);

// src/utils/DataSet.ts
var DataSet_exports = {};
__export(DataSet_exports, {
  default: () => DataSet
});
module.exports = __toCommonJS(DataSet_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/DataSet.ts
var DataSet = class _DataSet {
  /**
   * @constructor
   * @param {Field[]} [fields= []] List of field name or field objects
   * @param {ColumnValue[][]} [columns=[]] List of column values related to fields
   * @returns DataSet
   */
  constructor(fields = [], columns = []) {
    __publicField(this, "fields");
    __publicField(this, "columns");
    this.fields = fields.map((field) => {
      if ((0, lodash_exports.isString)(field)) {
        return {
          name: field
        };
      }
      return field;
    });
    this.columns = columns;
  }
  /**
   * Returns a empty Dataset
   *
   * Examples:
   * ```js
   * const empty = DataSet.empty();
   * ```
   * @returns {DataSet} DataSet
   * @public
   */
  static empty() {
    return new _DataSet();
  }
  /**
   * Construct a Dataset with data in json array format
   *
   * Examples:
   * ```js
   *  const dataset = DataSet.fromJSONArray(
   *     [{ name: 'x' }, { name: 'y' }, { name: 'z' }],
   *     [{ x: 'a', y: 4, z: 70 }, { x: 'b', y: 5, z: 80 }, { x: 'c', y: 6, z: 90 }]
   *  );
   * ```
   * @param {FieldObj[]} [fields=[]] List of objects containing field names
   * @param {RowItem[]} [results=[]] List of objects containing results for each field
   * @returns {DataSet}
   * @public
   */
  static fromJSONArray(fields = [], results = []) {
    let fieldList = fields;
    if (fieldList == null || fieldList.length === 0) {
      if (results.length > 0) {
        const rowSample = results[0];
        fieldList = Object.keys(rowSample).map((field) => ({
          name: field
        }));
      } else {
        fieldList = [];
      }
    }
    const columns = fieldList.map(
      ({ name }) => results.reduce((col, row) => {
        var _a;
        col.push((_a = row[name]) != null ? _a : null);
        return col;
      }, [])
    );
    return new _DataSet(fieldList, columns);
  }
  /**
   * Construct a Dataset with data in json columns format
   *
   * Examples:
   * ```js
   *  const dataset = DataSet.fromJSONCols(
   *     [{ name: 'x' }, { name: 'y' }, { name: 'z' }],
   *     [['a', 'b', 'c'], [4, 5, 6], [70, 80, 90]];
   *  );
   * ```
   * @param {Field[]} [fields=[]] List of fields
   * @param {ColumnValue[][]} [columns=[]] list of column values
   * @returns {DataSet}
   * @public
   */
  static fromJSONCols(fields = [], columns = []) {
    return new _DataSet(fields, columns);
  }
  /**
   * Construct a Dataset with data in json rows format
   *
   * Examples:
   * ```js
   *  const dataset = DataSet.fromJSONRows(
   *     [{ name: 'x' }, { name: 'y' }, { name: 'z' }],
   *     [['a', 4, 70], ['b', 5, 80], ['c', 6, 90]];
   *  );
   * ```
   * @param {Field[]} [fields=[]] List of fields
   * @param {ColumnValue[][]} [columns=[]] list of column values
   * @returns {DataSet}
   * @public
   */
  static fromJSONRows(fields = [], rows = []) {
    return new _DataSet(fields, (0, lodash_exports.zip)(...rows));
  }
  /**
   * Convert data to json array
   * @return {JSONArray} data in json array format
   * @public
   */
  toJSONArray() {
    return {
      fields: this.fields,
      results: (0, lodash_exports.zip)(...this.columns).map(
        (row) => (0, lodash_exports.zipObject)(
          this.fields.map((field) => field.name),
          row
        )
      )
    };
  }
  /**
   * Convert data to json columns
   * @return {JSONCols} data in json columns format
   * @public
   */
  toJSONCols() {
    const { fields, columns } = this;
    return { fields, columns };
  }
  /**
   * Convert data to json rows
   * @return {JSONRows} data in json rows format
   * @public
   */
  toJSONRows() {
    return {
      fields: this.fields,
      rows: (0, lodash_exports.zip)(...this.columns)
    };
  }
  /**
   * List all fields
   * @return {FieldObj[]} fields array
   * @public
   */
  getFields() {
    return this.fields;
  }
  /**
   * List data columns
   * @return {Object} columns array
   * @public
   */
  getColumns() {
    return this.columns;
  }
  /**
   *
   * @param {String} fieldName
   * @return {ColumnValue[]} column data
   * @public
   */
  getColumnByField(fieldName) {
    const index = (0, lodash_exports.findIndex)(this.fields, ({ name }) => name === fieldName);
    return this.columns[index];
  }
  /**
   *
   * @param {String} fieldName
   * @return {Boolean}
   * @public
   */
  hasField(fieldName) {
    return (0, lodash_exports.findIndex)(this.fields, ({ name }) => name === fieldName) !== -1;
  }
  /**
   * @return {Boolean} true if DataSet has no data
   * @public
   */
  isEmpty() {
    return this.columns.length === 0;
  }
  /**
   *
   * @param {DataSet} dataSet DataSet to compare
   * @return {Boolean} true if another dataset is equals to current one
   * @public
   */
  equals(dataSet) {
    return (0, lodash_exports.isEqual)(this.fields, dataSet.fields) && (0, lodash_exports.isEqual)(this.columns, dataSet.columns);
  }
  /**
   * Returns a slice of the dataset, useful for pagination.
   * @param options {Object}
   * @param options.count {Number} number of rows
   * @param options.offset {Number} starting row index
   * @returns {DataSet}
   * @public
   */
  getPage({ count = 0, offset = 0 }) {
    const end = count <= 0 ? void 0 : offset + count;
    return _DataSet.fromJSONCols(
      this.getFields(),
      this.getColumns().map((column) => column.slice(offset, end))
    );
  }
  /**
   * Get total number of rows. Note this is not affected by the pagination.
   * @public
   */
  getTotalCount() {
    var _a, _b;
    return (_b = (_a = this.columns[0]) == null ? void 0 : _a.length) != null ? _b : 0;
  }
};
//# sourceMappingURL=DataSet.js.map
