// src/components/dynamodb/dynamodb-stream-record.ts
function dynamodbValueToJs(av) {
  if (av.S !== void 0) {
    return av.S;
  }
  if (av.N !== void 0) {
    return av.N.includes(".") ? Number.parseFloat(av.N) : Number.parseInt(av.N, 10);
  }
  if (av.BOOL !== void 0) {
    return av.BOOL;
  }
  if (av.NULL !== void 0) {
    return null;
  }
  if (av.M !== void 0) {
    return dynamodbImageToPlain(av.M);
  }
  if (av.L !== void 0) {
    return av.L.map((x) => dynamodbValueToJs(x));
  }
  if (av.SS !== void 0) {
    return av.SS;
  }
  if (av.NS !== void 0) {
    return av.NS.map(
      (n) => n.includes(".") ? Number.parseFloat(n) : Number.parseInt(n, 10)
    );
  }
  return void 0;
}
function dynamodbImageToPlain(image) {
  const out = {};
  for (const [k, v] of Object.entries(image)) {
    out[k] = dynamodbValueToJs(v);
  }
  return out;
}

// src/components/dynamodb/data-store-change-events.ts
var DATA_STORE_CHANGE_DETAIL_TYPE = "FhirCurrentResourceChanged";
var DATA_STORE_CHANGE_DETAIL_MAX_UTF8_BYTES = 250 * 1024;
var EXCLUDED_CHANGE_DETAIL_KEYS = /* @__PURE__ */ new Set([
  "PK",
  "SK",
  "GSI1PK",
  "GSI1SK",
  "GSI2PK",
  "GSI2SK",
  /** Full FHIR JSON may contain PII; never list or ship in the bus payload. */
  "resource"
]);
function shallowValueEqual(a, b) {
  return JSON.stringify(a) === JSON.stringify(b);
}
function changedNonResourceAttributeNames(oldImage, newImage) {
  if (!oldImage || !newImage) {
    return void 0;
  }
  const names = /* @__PURE__ */ new Set();
  const keys = /* @__PURE__ */ new Set([...Object.keys(oldImage), ...Object.keys(newImage)]);
  for (const k of keys) {
    if (EXCLUDED_CHANGE_DETAIL_KEYS.has(k)) {
      continue;
    }
    if (!shallowValueEqual(oldImage[k], newImage[k])) {
      names.add(k);
    }
  }
  return names.size > 0 ? [...names].sort() : void 0;
}
function presentMetadataAttributeNames(image) {
  if (!image) {
    return void 0;
  }
  const names = Object.keys(image).filter(
    (k) => !EXCLUDED_CHANGE_DETAIL_KEYS.has(k)
  );
  return names.length > 0 ? names.sort() : void 0;
}
function plainImage(image) {
  if (!image) {
    return void 0;
  }
  return dynamodbImageToPlain(image);
}
function buildFhirCurrentResourceChangeDetail(record, keys) {
  const rawName = record.eventName;
  const changeType = rawName === "INSERT" || rawName === "MODIFY" || rawName === "REMOVE" ? rawName : "MODIFY";
  const seq = record.dynamodb?.SequenceNumber;
  const approxEpochMs = record.dynamodb?.ApproximateCreationDateTime;
  const newPlain = plainImage(
    record.dynamodb?.NewImage
  );
  const oldPlain = plainImage(
    record.dynamodb?.OldImage
  );
  let changedAttributeNames;
  if (changeType === "MODIFY") {
    changedAttributeNames = changedNonResourceAttributeNames(
      oldPlain,
      newPlain
    );
  } else if (changeType === "INSERT") {
    changedAttributeNames = presentMetadataAttributeNames(newPlain);
  } else {
    changedAttributeNames = presentMetadataAttributeNames(oldPlain);
  }
  return {
    changeType,
    tenantId: keys.tenantId,
    workspaceId: keys.workspaceId,
    resourceType: keys.resourceType,
    resourceId: keys.resourceId,
    resourceVersion: keys.version,
    ...typeof seq === "string" && seq.length > 0 ? { streamSequenceNumber: seq } : {},
    ...typeof approxEpochMs === "number" && Number.isFinite(approxEpochMs) ? { approximateCreationEpochMs: approxEpochMs } : {},
    ...changedAttributeNames ? { changedAttributeNames } : {}
  };
}

export {
  dynamodbImageToPlain,
  DATA_STORE_CHANGE_DETAIL_TYPE,
  DATA_STORE_CHANGE_DETAIL_MAX_UTF8_BYTES,
  buildFhirCurrentResourceChangeDetail
};
//# sourceMappingURL=chunk-RC7HHZR6.mjs.map