/* 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 __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/EncodingSerializer.ts
var EncodingSerializer_exports = {};
__export(EncodingSerializer_exports, {
  EncodingSerializer: () => EncodingSerializer
});
module.exports = __toCommonJS(EncodingSerializer_exports);
var EncodingSerializer = class _EncodingSerializer {
  /**
   *
   * @param {AST} ast
   * @returns {Encoding}
   */
  static serialize(ast) {
    const encoding = Object.keys(ast.dimensions).reduce(
      (encodingEntry, name) => {
        encodingEntry.dimensions[name] = _EncodingSerializer.dimension(ast.dimensions[name]);
        return encodingEntry;
      },
      { dimensions: {}, context: {} }
    );
    Object.assign(encoding.context, ast.context);
    return encoding;
  }
  static dimension(dimension) {
    const { value } = dimension;
    return { value: _EncodingSerializer.dsl(value) };
  }
  static expr(expr) {
    if (expr.type === "method") {
      return _EncodingSerializer.method(expr);
    }
    const p = expr;
    if (p.type === "string") {
      return `'${p.v}'`;
    }
    return p.v.toString();
  }
  static method(op) {
    const { name, args } = op;
    const methodCallOpening = `${name}(`;
    return `${args.reduce((acc, curArg, i) => {
      const maybeComma = i < args.length - 1 ? "," : "";
      return acc + _EncodingSerializer.expr(curArg) + maybeComma;
    }, methodCallOpening)})`;
  }
  static dsl(exprs) {
    return exprs.reduce((commandStr, expr, i) => {
      commandStr += (i > 0 ? "|" : "") + _EncodingSerializer.expr(expr);
      return commandStr;
    }, "");
  }
};
//# sourceMappingURL=EncodingSerializer.js.map
