function extractStatusFromSnapshot(snapshot) {
  if (!snapshot || typeof snapshot !== "object") return null;
  const s = snapshot;
  if (typeof s.status === "string") return s.status;
  if (s.order && typeof s.order === "object") {
    const order = s.order;
    const orderStatus = typeof order.status === "string" ? order.status : null;
    const fulfillmentStatus = typeof order.fulfillment_status === "string" ? order.fulfillment_status : null;
    const paymentStatus = typeof order.payment_status === "string" ? order.payment_status : null;
    return orderStatus ?? fulfillmentStatus ?? paymentStatus ?? null;
  }
  if (s.quote && typeof s.quote === "object") {
    const quote = s.quote;
    if (typeof quote.status === "string") return quote.status;
  }
  return null;
}
function detectStatusChange(log) {
  const after = extractStatusFromSnapshot(log.snapshotAfter);
  const before = log.snapshotBefore ? extractStatusFromSnapshot(log.snapshotBefore) : null;
  if (!log.snapshotBefore) {
    return { statusFrom: null, statusTo: after ?? null };
  }
  if (before !== after && (before !== null || after !== null)) {
    return { statusFrom: before, statusTo: after ?? null };
  }
  return null;
}
function normalizeActionLogToHistoryEntry(log, kind, displayUsers) {
  const statusChange = detectStatusChange(log);
  let entryKind = "action";
  let action = log.actionLabel || log.commandId;
  let metadata = { documentKind: kind, commandId: log.commandId };
  if (statusChange) {
    const hasStatusValues = statusChange.statusFrom != null || statusChange.statusTo != null;
    if (hasStatusValues) {
      entryKind = "status";
      action = statusChange.statusTo ?? "unknown";
      metadata = { ...metadata, statusFrom: statusChange.statusFrom, statusTo: statusChange.statusTo };
    }
  }
  const actorLabel = log.actorUserId ? displayUsers?.[log.actorUserId] ?? log.actorUserId : "system";
  return {
    id: log.id,
    occurredAt: log.createdAt.toISOString(),
    kind: entryKind,
    action,
    actor: { id: log.actorUserId, label: actorLabel },
    source: "action_log",
    metadata
  };
}
function normalizeNoteToHistoryEntry(note, kind, displayUsers) {
  const actorLabel = note.authorUserId ? displayUsers?.[note.authorUserId] ?? note.authorUserId : "system";
  return {
    id: note.id,
    occurredAt: note.createdAt.toISOString(),
    kind: "comment",
    action: note.body,
    actor: { id: note.authorUserId ?? null, label: actorLabel },
    source: "note",
    metadata: { documentKind: kind }
  };
}
function buildHistoryEntries(input) {
  const logEntries = input.actionLogs.map(
    (log) => normalizeActionLogToHistoryEntry(log, input.kind, input.displayUsers)
  );
  const noteEntries = (input.notes ?? []).map(
    (note) => normalizeNoteToHistoryEntry(note, input.kind, input.displayUsers)
  );
  return [...logEntries, ...noteEntries].sort(
    (a, b) => new Date(b.occurredAt).getTime() - new Date(a.occurredAt).getTime()
  );
}
export {
  buildHistoryEntries,
  detectStatusChange,
  normalizeActionLogToHistoryEntry,
  normalizeNoteToHistoryEntry
};
//# sourceMappingURL=historyHelpers.js.map
