import {
  bufferToArrayBuffer,
  toUint8Array
} from "./chunk-IZ4M5TJU.js";

// dist/schemas/transport/v1.ts
import * as bare from "@rivetkit/bare-ts";
var config = /* @__PURE__ */ bare.Config({});
function readWorkflowCbor(bc) {
  return bare.readData(bc);
}
function writeWorkflowCbor(bc, x) {
  bare.writeData(bc, x);
}
function readWorkflowNameIndex(bc) {
  return bare.readU32(bc);
}
function writeWorkflowNameIndex(bc, x) {
  bare.writeU32(bc, x);
}
function readWorkflowLoopIterationMarker(bc) {
  return {
    loop: readWorkflowNameIndex(bc),
    iteration: bare.readU32(bc)
  };
}
function writeWorkflowLoopIterationMarker(bc, x) {
  writeWorkflowNameIndex(bc, x.loop);
  bare.writeU32(bc, x.iteration);
}
function readWorkflowPathSegment(bc) {
  const offset = bc.offset;
  const tag = bare.readU8(bc);
  switch (tag) {
    case 0:
      return { tag: "WorkflowNameIndex", val: readWorkflowNameIndex(bc) };
    case 1:
      return { tag: "WorkflowLoopIterationMarker", val: readWorkflowLoopIterationMarker(bc) };
    default: {
      bc.offset = offset;
      throw new bare.BareError(offset, "invalid tag");
    }
  }
}
function writeWorkflowPathSegment(bc, x) {
  switch (x.tag) {
    case "WorkflowNameIndex": {
      bare.writeU8(bc, 0);
      writeWorkflowNameIndex(bc, x.val);
      break;
    }
    case "WorkflowLoopIterationMarker": {
      bare.writeU8(bc, 1);
      writeWorkflowLoopIterationMarker(bc, x.val);
      break;
    }
  }
}
function readWorkflowLocation(bc) {
  const len = bare.readUintSafe(bc);
  if (len === 0) {
    return [];
  }
  const result = [readWorkflowPathSegment(bc)];
  for (let i = 1; i < len; i++) {
    result[i] = readWorkflowPathSegment(bc);
  }
  return result;
}
function writeWorkflowLocation(bc, x) {
  bare.writeUintSafe(bc, x.length);
  for (let i = 0; i < x.length; i++) {
    writeWorkflowPathSegment(bc, x[i]);
  }
}
function readWorkflowEntryStatus(bc) {
  const offset = bc.offset;
  const tag = bare.readU8(bc);
  switch (tag) {
    case 0:
      return "PENDING" /* PENDING */;
    case 1:
      return "RUNNING" /* RUNNING */;
    case 2:
      return "COMPLETED" /* COMPLETED */;
    case 3:
      return "FAILED" /* FAILED */;
    case 4:
      return "EXHAUSTED" /* EXHAUSTED */;
    default: {
      bc.offset = offset;
      throw new bare.BareError(offset, "invalid tag");
    }
  }
}
function writeWorkflowEntryStatus(bc, x) {
  switch (x) {
    case "PENDING" /* PENDING */: {
      bare.writeU8(bc, 0);
      break;
    }
    case "RUNNING" /* RUNNING */: {
      bare.writeU8(bc, 1);
      break;
    }
    case "COMPLETED" /* COMPLETED */: {
      bare.writeU8(bc, 2);
      break;
    }
    case "FAILED" /* FAILED */: {
      bare.writeU8(bc, 3);
      break;
    }
    case "EXHAUSTED" /* EXHAUSTED */: {
      bare.writeU8(bc, 4);
      break;
    }
  }
}
function readWorkflowSleepState(bc) {
  const offset = bc.offset;
  const tag = bare.readU8(bc);
  switch (tag) {
    case 0:
      return "PENDING" /* PENDING */;
    case 1:
      return "COMPLETED" /* COMPLETED */;
    case 2:
      return "INTERRUPTED" /* INTERRUPTED */;
    default: {
      bc.offset = offset;
      throw new bare.BareError(offset, "invalid tag");
    }
  }
}
function writeWorkflowSleepState(bc, x) {
  switch (x) {
    case "PENDING" /* PENDING */: {
      bare.writeU8(bc, 0);
      break;
    }
    case "COMPLETED" /* COMPLETED */: {
      bare.writeU8(bc, 1);
      break;
    }
    case "INTERRUPTED" /* INTERRUPTED */: {
      bare.writeU8(bc, 2);
      break;
    }
  }
}
function readWorkflowBranchStatusType(bc) {
  const offset = bc.offset;
  const tag = bare.readU8(bc);
  switch (tag) {
    case 0:
      return "PENDING" /* PENDING */;
    case 1:
      return "RUNNING" /* RUNNING */;
    case 2:
      return "COMPLETED" /* COMPLETED */;
    case 3:
      return "FAILED" /* FAILED */;
    case 4:
      return "CANCELLED" /* CANCELLED */;
    default: {
      bc.offset = offset;
      throw new bare.BareError(offset, "invalid tag");
    }
  }
}
function writeWorkflowBranchStatusType(bc, x) {
  switch (x) {
    case "PENDING" /* PENDING */: {
      bare.writeU8(bc, 0);
      break;
    }
    case "RUNNING" /* RUNNING */: {
      bare.writeU8(bc, 1);
      break;
    }
    case "COMPLETED" /* COMPLETED */: {
      bare.writeU8(bc, 2);
      break;
    }
    case "FAILED" /* FAILED */: {
      bare.writeU8(bc, 3);
      break;
    }
    case "CANCELLED" /* CANCELLED */: {
      bare.writeU8(bc, 4);
      break;
    }
  }
}
function read0(bc) {
  return bare.readBool(bc) ? readWorkflowCbor(bc) : null;
}
function write0(bc, x) {
  bare.writeBool(bc, x !== null);
  if (x !== null) {
    writeWorkflowCbor(bc, x);
  }
}
function read1(bc) {
  return bare.readBool(bc) ? bare.readString(bc) : null;
}
function write1(bc, x) {
  bare.writeBool(bc, x !== null);
  if (x !== null) {
    bare.writeString(bc, x);
  }
}
function readWorkflowStepEntry(bc) {
  return {
    output: read0(bc),
    error: read1(bc)
  };
}
function writeWorkflowStepEntry(bc, x) {
  write0(bc, x.output);
  write1(bc, x.error);
}
function readWorkflowLoopEntry(bc) {
  return {
    state: readWorkflowCbor(bc),
    iteration: bare.readU32(bc),
    output: read0(bc)
  };
}
function writeWorkflowLoopEntry(bc, x) {
  writeWorkflowCbor(bc, x.state);
  bare.writeU32(bc, x.iteration);
  write0(bc, x.output);
}
function readWorkflowSleepEntry(bc) {
  return {
    deadline: bare.readU64(bc),
    state: readWorkflowSleepState(bc)
  };
}
function writeWorkflowSleepEntry(bc, x) {
  bare.writeU64(bc, x.deadline);
  writeWorkflowSleepState(bc, x.state);
}
function readWorkflowMessageEntry(bc) {
  return {
    name: bare.readString(bc),
    messageData: readWorkflowCbor(bc)
  };
}
function writeWorkflowMessageEntry(bc, x) {
  bare.writeString(bc, x.name);
  writeWorkflowCbor(bc, x.messageData);
}
function readWorkflowRollbackCheckpointEntry(bc) {
  return {
    name: bare.readString(bc)
  };
}
function writeWorkflowRollbackCheckpointEntry(bc, x) {
  bare.writeString(bc, x.name);
}
function readWorkflowBranchStatus(bc) {
  return {
    status: readWorkflowBranchStatusType(bc),
    output: read0(bc),
    error: read1(bc)
  };
}
function writeWorkflowBranchStatus(bc, x) {
  writeWorkflowBranchStatusType(bc, x.status);
  write0(bc, x.output);
  write1(bc, x.error);
}
function read2(bc) {
  const len = bare.readUintSafe(bc);
  const result = /* @__PURE__ */ new Map();
  for (let i = 0; i < len; i++) {
    const offset = bc.offset;
    const key = bare.readString(bc);
    if (result.has(key)) {
      bc.offset = offset;
      throw new bare.BareError(offset, "duplicated key");
    }
    result.set(key, readWorkflowBranchStatus(bc));
  }
  return result;
}
function write2(bc, x) {
  bare.writeUintSafe(bc, x.size);
  for (const kv of x) {
    bare.writeString(bc, kv[0]);
    writeWorkflowBranchStatus(bc, kv[1]);
  }
}
function readWorkflowJoinEntry(bc) {
  return {
    branches: read2(bc)
  };
}
function writeWorkflowJoinEntry(bc, x) {
  write2(bc, x.branches);
}
function readWorkflowRaceEntry(bc) {
  return {
    winner: read1(bc),
    branches: read2(bc)
  };
}
function writeWorkflowRaceEntry(bc, x) {
  write1(bc, x.winner);
  write2(bc, x.branches);
}
function readWorkflowRemovedEntry(bc) {
  return {
    originalType: bare.readString(bc),
    originalName: read1(bc)
  };
}
function writeWorkflowRemovedEntry(bc, x) {
  bare.writeString(bc, x.originalType);
  write1(bc, x.originalName);
}
function readWorkflowEntryKind(bc) {
  const offset = bc.offset;
  const tag = bare.readU8(bc);
  switch (tag) {
    case 0:
      return { tag: "WorkflowStepEntry", val: readWorkflowStepEntry(bc) };
    case 1:
      return { tag: "WorkflowLoopEntry", val: readWorkflowLoopEntry(bc) };
    case 2:
      return { tag: "WorkflowSleepEntry", val: readWorkflowSleepEntry(bc) };
    case 3:
      return { tag: "WorkflowMessageEntry", val: readWorkflowMessageEntry(bc) };
    case 4:
      return { tag: "WorkflowRollbackCheckpointEntry", val: readWorkflowRollbackCheckpointEntry(bc) };
    case 5:
      return { tag: "WorkflowJoinEntry", val: readWorkflowJoinEntry(bc) };
    case 6:
      return { tag: "WorkflowRaceEntry", val: readWorkflowRaceEntry(bc) };
    case 7:
      return { tag: "WorkflowRemovedEntry", val: readWorkflowRemovedEntry(bc) };
    default: {
      bc.offset = offset;
      throw new bare.BareError(offset, "invalid tag");
    }
  }
}
function writeWorkflowEntryKind(bc, x) {
  switch (x.tag) {
    case "WorkflowStepEntry": {
      bare.writeU8(bc, 0);
      writeWorkflowStepEntry(bc, x.val);
      break;
    }
    case "WorkflowLoopEntry": {
      bare.writeU8(bc, 1);
      writeWorkflowLoopEntry(bc, x.val);
      break;
    }
    case "WorkflowSleepEntry": {
      bare.writeU8(bc, 2);
      writeWorkflowSleepEntry(bc, x.val);
      break;
    }
    case "WorkflowMessageEntry": {
      bare.writeU8(bc, 3);
      writeWorkflowMessageEntry(bc, x.val);
      break;
    }
    case "WorkflowRollbackCheckpointEntry": {
      bare.writeU8(bc, 4);
      writeWorkflowRollbackCheckpointEntry(bc, x.val);
      break;
    }
    case "WorkflowJoinEntry": {
      bare.writeU8(bc, 5);
      writeWorkflowJoinEntry(bc, x.val);
      break;
    }
    case "WorkflowRaceEntry": {
      bare.writeU8(bc, 6);
      writeWorkflowRaceEntry(bc, x.val);
      break;
    }
    case "WorkflowRemovedEntry": {
      bare.writeU8(bc, 7);
      writeWorkflowRemovedEntry(bc, x.val);
      break;
    }
  }
}
function readWorkflowEntry(bc) {
  return {
    id: bare.readString(bc),
    location: readWorkflowLocation(bc),
    kind: readWorkflowEntryKind(bc)
  };
}
function writeWorkflowEntry(bc, x) {
  bare.writeString(bc, x.id);
  writeWorkflowLocation(bc, x.location);
  writeWorkflowEntryKind(bc, x.kind);
}
function read3(bc) {
  return bare.readBool(bc) ? bare.readU64(bc) : null;
}
function write3(bc, x) {
  bare.writeBool(bc, x !== null);
  if (x !== null) {
    bare.writeU64(bc, x);
  }
}
function readWorkflowEntryMetadata(bc) {
  return {
    status: readWorkflowEntryStatus(bc),
    error: read1(bc),
    attempts: bare.readU32(bc),
    lastAttemptAt: bare.readU64(bc),
    createdAt: bare.readU64(bc),
    completedAt: read3(bc),
    rollbackCompletedAt: read3(bc),
    rollbackError: read1(bc)
  };
}
function writeWorkflowEntryMetadata(bc, x) {
  writeWorkflowEntryStatus(bc, x.status);
  write1(bc, x.error);
  bare.writeU32(bc, x.attempts);
  bare.writeU64(bc, x.lastAttemptAt);
  bare.writeU64(bc, x.createdAt);
  write3(bc, x.completedAt);
  write3(bc, x.rollbackCompletedAt);
  write1(bc, x.rollbackError);
}
function read4(bc) {
  const len = bare.readUintSafe(bc);
  if (len === 0) {
    return [];
  }
  const result = [bare.readString(bc)];
  for (let i = 1; i < len; i++) {
    result[i] = bare.readString(bc);
  }
  return result;
}
function write4(bc, x) {
  bare.writeUintSafe(bc, x.length);
  for (let i = 0; i < x.length; i++) {
    bare.writeString(bc, x[i]);
  }
}
function read5(bc) {
  const len = bare.readUintSafe(bc);
  if (len === 0) {
    return [];
  }
  const result = [readWorkflowEntry(bc)];
  for (let i = 1; i < len; i++) {
    result[i] = readWorkflowEntry(bc);
  }
  return result;
}
function write5(bc, x) {
  bare.writeUintSafe(bc, x.length);
  for (let i = 0; i < x.length; i++) {
    writeWorkflowEntry(bc, x[i]);
  }
}
function read6(bc) {
  const len = bare.readUintSafe(bc);
  const result = /* @__PURE__ */ new Map();
  for (let i = 0; i < len; i++) {
    const offset = bc.offset;
    const key = bare.readString(bc);
    if (result.has(key)) {
      bc.offset = offset;
      throw new bare.BareError(offset, "duplicated key");
    }
    result.set(key, readWorkflowEntryMetadata(bc));
  }
  return result;
}
function write6(bc, x) {
  bare.writeUintSafe(bc, x.size);
  for (const kv of x) {
    bare.writeString(bc, kv[0]);
    writeWorkflowEntryMetadata(bc, kv[1]);
  }
}
function readWorkflowHistory(bc) {
  return {
    nameRegistry: read4(bc),
    entries: read5(bc),
    entryMetadata: read6(bc)
  };
}
function writeWorkflowHistory(bc, x) {
  write4(bc, x.nameRegistry);
  write5(bc, x.entries);
  write6(bc, x.entryMetadata);
}
function encodeWorkflowHistory(x) {
  const bc = new bare.ByteCursor(
    new Uint8Array(config.initialBufferLength),
    config
  );
  writeWorkflowHistory(bc, x);
  return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset);
}
function decodeWorkflowHistory(bytes) {
  const bc = new bare.ByteCursor(bytes, config);
  const result = readWorkflowHistory(bc);
  if (bc.offset < bc.view.byteLength) {
    throw new bare.BareError(bc.offset, "remaining bytes");
  }
  return result;
}

// src/inspector/transport.ts
function encodeWorkflowHistoryTransport(history) {
  return bufferToArrayBuffer(encodeWorkflowHistory(history));
}
function decodeWorkflowHistoryTransport(data) {
  return decodeWorkflowHistory(toUint8Array(data));
}

export {
  encodeWorkflowHistoryTransport,
  decodeWorkflowHistoryTransport
};
//# sourceMappingURL=chunk-WTRB56DR.js.map