"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// ../packages/timeline/dist/index.js
function createTimelineSequencer(options) {
  let seq = _nullishCoalesce(options.startSeq, () => ( 0));
  const now = _nullishCoalesce(options.now, () => ( Date.now));
  return {
    append(item, rawRef) {
      seq += 1;
      return appendTimelineItem({
        sessionId: options.sessionId,
        provider: options.provider,
        epoch: options.epoch,
        seq,
        timestamp: now(),
        item,
        rawRef
      });
    }
  };
}
function appendTimelineItem(envelope) {
  return {
    ...envelope,
    rawRef: envelope.rawRef ? { ...envelope.rawRef } : void 0
  };
}
function createTimelineCursor(cursor) {
  return {
    epoch: cursor.epoch,
    afterSeq: cursor.afterSeq
  };
}
function fetchTimeline(timeline, request = {}) {
  const ordered = sortTimeline(timeline);
  const cursor = _nullishCoalesce(request.cursor, () => ( cursorFromTimelineStart(ordered)));
  const items = ordered.filter((item) => item.epoch === cursor.epoch && item.seq > cursor.afterSeq).slice(0, request.limit);
  const lastSeq = _nullishCoalesce(_optionalChain([items, 'access', _ => _.at, 'call', _2 => _2(-1), 'optionalAccess', _3 => _3.seq]), () => ( cursor.afterSeq));
  const firstSeq = _optionalChain([items, 'access', _4 => _4[0], 'optionalAccess', _5 => _5.seq]);
  return {
    items,
    nextCursor: {
      epoch: cursor.epoch,
      afterSeq: lastSeq
    },
    hasGap: firstSeq !== void 0 && firstSeq > cursor.afterSeq + 1
  };
}
function mergeTimeline(existing, incoming) {
  const byKey = /* @__PURE__ */ new Map();
  for (const item of [...existing, ...incoming]) {
    byKey.set(timelineKey(item), item);
  }
  return sortTimeline([...byKey.values()]);
}
function cursorFromTimelineStart(timeline) {
  return {
    epoch: _nullishCoalesce(_optionalChain([timeline, 'access', _6 => _6[0], 'optionalAccess', _7 => _7.epoch]), () => ( "default")),
    afterSeq: 0
  };
}
function sortTimeline(timeline) {
  return [...timeline].sort((left, right) => {
    if (left.epoch !== right.epoch) return left.epoch.localeCompare(right.epoch);
    return left.seq - right.seq;
  });
}
function timelineKey(item) {
  return `${item.epoch}:${item.seq}`;
}







exports.createTimelineSequencer = createTimelineSequencer; exports.appendTimelineItem = appendTimelineItem; exports.createTimelineCursor = createTimelineCursor; exports.fetchTimeline = fetchTimeline; exports.mergeTimeline = mergeTimeline;
