"use strict";
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/domain/delegates/communications/upsert.ts
var upsert_exports = {};
__export(upsert_exports, {
  upsertCommunications: () => upsertCommunications
});
module.exports = __toCommonJS(upsert_exports);

// src/utils/uuid.ts
function uuidv4() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
    return v.toString(16);
  });
}

// src/domain/delegates/communications/upsert.ts
async function upsertCommunications(channel, formattedMessage, status, taskList, comType, owner, client, questionPrompt, tokens, id = uuidv4()) {
  console.log(`upsertCommunications id: ${id}`);
  const token = await client.auth.signIn();
  const apiKey = token.access_token;
  const url = `${client.url}/api/v2/ontologies/${client.ontologyRid}/actions/upsert-communication/apply`;
  const headers = {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json"
  };
  const body = JSON.stringify({
    parameters: {
      channel,
      formattedMessage,
      status,
      taskList,
      comType,
      owner,
      questionPrompt,
      tokens,
      id
    },
    options: {
      returnEdits: "ALL"
    }
  });
  const apiResult = await fetch(url, {
    method: "POST",
    headers,
    body
  });
  const result = await apiResult.json();
  if (!result.edits || result.edits.edits.length === 0) {
    throw new Error("Failed to upsert communications to the ontolgoy.");
  }
  console.log(`upsert communications action returned: ${result?.edits?.edits?.[0]}`);
  const commsId = result.edits.edits[0].primaryKey;
  const getUrl = `${client.url}/api/v2/ontologies/${client.ontologyRid}/objects/Communications/${commsId}`;
  const fetchResults = await fetch(getUrl, {
    method: "GET",
    headers
  });
  const comms = await fetchResults.json();
  console.log(`the machine execution ontology returned: ${JSON.stringify(comms)}`);
  return comms;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  upsertCommunications
});
