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);

// index.ts
var index_exports = {};
__export(index_exports, {
  RecordsClient: () => RecordsClient
});
module.exports = __toCommonJS(index_exports);

// ../aux-records/RecordsClient.js
var RecordsClient = class {
  get sessionKey() {
    return this._sessionKey;
  }
  set sessionKey(value) {
    this._sessionKey = value;
  }
  /**
   * The additional headers that will be sent with each request.
   */
  get headers() {
    return this._headers;
  }
  get endpoint() {
    return this._endpoint;
  }
  constructor(endpoint) {
    this._headers = {};
    this._endpoint = endpoint;
    this._sessionKey = null;
    Object.defineProperties(this, {
      then: {
        value: void 0,
        configurable: false,
        writable: false
      },
      catch: {
        value: void 0,
        configurable: false,
        writable: false
      }
    });
  }
  /**
   * Calls the procedure with the given name, using the given argument.
   * @param name The name of the procedure to call.
   * @param input The input to the procedure.
   * @param options The options to use for the procedure.
   * @param query The query to use for the procedure.
   */
  async callProcedure(name, input, options, query) {
    const response = await fetch(`${options?.endpoint ?? this._endpoint}/api/v3/callProcedure`, {
      method: "POST",
      body: JSON.stringify({ procedure: name, input, query }),
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        Accept: "application/json,application/x-ndjson",
        ...this._headers,
        ...options?.headers ?? {},
        ...this._authenticationHeaders(options)
      }
    });
    if (response.headers?.get("Content-Type")?.indexOf("application/x-ndjson") >= 0) {
      return streamJsonLines(response.body, new TextDecoder());
    } else {
      return await response.json();
    }
  }
  _authenticationHeaders(options) {
    const key = options?.sessionKey ?? this._sessionKey;
    if (key) {
      return {
        Authorization: `Bearer ${key}`
      };
    } else {
      return {};
    }
  }
};
async function* streamJsonLines(stream, decoder) {
  let buffer = "";
  const reader = stream.getReader();
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      if (buffer.length > 0) {
        return JSON.parse(buffer);
      }
      break;
    }
    let chunk = decoder.decode(value, { stream: true });
    let newlinePosition;
    while ((newlinePosition = chunk.indexOf("\n")) >= 0) {
      let carriageReturnPosition = chunk.indexOf("\r", newlinePosition - 1);
      if (carriageReturnPosition >= 0) {
        const beforeCarriageReturn = chunk.substring(0, carriageReturnPosition);
        if (buffer.length + beforeCarriageReturn.length > 0) {
          const jsonLine = buffer + beforeCarriageReturn;
          yield JSON.parse(jsonLine);
        }
        const afterNewline = chunk.substring(carriageReturnPosition + 2);
        chunk = afterNewline;
        buffer = "";
      } else {
        const beforeNewline = chunk.substring(0, newlinePosition);
        if (buffer.length + beforeNewline.length > 0) {
          const jsonLine = buffer + beforeNewline;
          yield JSON.parse(jsonLine);
        }
        const afterNewline = chunk.substring(newlinePosition + 1);
        chunk = afterNewline;
        buffer = "";
      }
    }
    buffer += chunk;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RecordsClient
});
