function rootToken(instance) {
  return { kind: "root", instance };
}
function branchToken(instance, branch) {
  return { kind: "branch", instance, branch };
}
function tokenInstanceId(token) {
  return token.instance.id;
}
function tokenDefinitionId(token) {
  return token.instance.definitionId;
}
function tokenTenantId(token) {
  return token.instance.tenantId;
}
function tokenOrganizationId(token) {
  return token.instance.organizationId;
}
function tokenBranchInstanceId(token) {
  return token.kind === "branch" ? token.branch.id : null;
}
function tokenCurrentStepId(token) {
  return token.kind === "branch" ? token.branch.currentStepId : token.instance.currentStepId;
}
function setTokenCurrentStepId(token, stepId) {
  if (token.kind === "branch") token.branch.currentStepId = stepId;
  else token.instance.currentStepId = stepId;
}
function tokenReadContext(token) {
  if (token.kind === "branch") {
    return { ...token.instance.context || {}, ...token.branch.contextNamespace || {} };
  }
  return token.instance.context || {};
}
function applyTokenContextWrites(token, workflowContext, activityOutputs) {
  if (token.kind === "branch") {
    token.branch.contextNamespace = {
      ...token.branch.contextNamespace || {},
      ...activityOutputs
    };
    return;
  }
  token.instance.context = {
    ...token.instance.context || {},
    ...workflowContext,
    ...activityOutputs
  };
}
function setTokenWaitingForActivities(token) {
  if (token.kind === "branch") token.branch.status = "WAITING_FOR_ACTIVITIES";
  else token.instance.status = "WAITING_FOR_ACTIVITIES";
}
function setTokenPaused(token, at) {
  if (token.kind === "branch") {
    token.branch.status = "PAUSED";
  } else {
    token.instance.status = "PAUSED";
    token.instance.pausedAt = at;
  }
}
function getTokenPendingTransition(token) {
  return (token.kind === "branch" ? token.branch.pendingTransition : token.instance.pendingTransition) ?? null;
}
function setTokenPendingTransition(token, pending) {
  if (token.kind === "branch") token.branch.pendingTransition = pending;
  else token.instance.pendingTransition = pending;
}
function touchToken(token, now) {
  if (token.kind === "branch") token.branch.updatedAt = now;
  else token.instance.updatedAt = now;
}
function mergeTokenContext(token, patch) {
  if (token.kind === "branch") {
    token.branch.contextNamespace = { ...token.branch.contextNamespace || {}, ...patch };
  } else {
    token.instance.context = { ...token.instance.context || {}, ...patch };
  }
}
export {
  applyTokenContextWrites,
  branchToken,
  getTokenPendingTransition,
  mergeTokenContext,
  rootToken,
  setTokenCurrentStepId,
  setTokenPaused,
  setTokenPendingTransition,
  setTokenWaitingForActivities,
  tokenBranchInstanceId,
  tokenCurrentStepId,
  tokenDefinitionId,
  tokenInstanceId,
  tokenOrganizationId,
  tokenReadContext,
  tokenTenantId,
  touchToken
};
//# sourceMappingURL=execution-token.js.map
