"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/proof-session.ts
var proof_session_exports = {};
__export(proof_session_exports, {
  RIDDLE_PROOF_VISUAL_SESSION_FINGERPRINT_VERSION: () => RIDDLE_PROOF_VISUAL_SESSION_FINGERPRINT_VERSION,
  RIDDLE_PROOF_VISUAL_SESSION_VERSION: () => RIDDLE_PROOF_VISUAL_SESSION_VERSION,
  buildVisualProofSession: () => buildVisualProofSession,
  compareVisualProofSessionFingerprint: () => compareVisualProofSessionFingerprint,
  parseVisualProofSession: () => parseVisualProofSession,
  visualSessionFingerprint: () => visualSessionFingerprint,
  visualSessionFingerprintBasis: () => visualSessionFingerprintBasis
});
module.exports = __toCommonJS(proof_session_exports);
var import_node_crypto = require("crypto");
var RIDDLE_PROOF_VISUAL_SESSION_VERSION = "riddle-proof.visual-session.v1";
var RIDDLE_PROOF_VISUAL_SESSION_FINGERPRINT_VERSION = "riddle-proof.visual-session.fingerprint.v1";
function trim(value) {
  const text = String(value ?? "").trim();
  return text || void 0;
}
function hashString(value) {
  const text = trim(value);
  if (!text) return void 0;
  return (0, import_node_crypto.createHash)("sha256").update(text).digest("hex");
}
function stableJson(value) {
  if (value === void 0) return "null";
  if (value === null || typeof value !== "object") return JSON.stringify(value);
  if (Array.isArray(value)) return `[${value.map((item) => stableJson(item)).join(",")}]`;
  const record = value;
  return `{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`).join(",")}}`;
}
function withoutUndefined(record) {
  for (const key of Object.keys(record)) {
    if (record[key] === void 0) delete record[key];
  }
  return record;
}
function visualSessionFingerprintBasis(input) {
  return withoutUndefined({
    version: RIDDLE_PROOF_VISUAL_SESSION_FINGERPRINT_VERSION,
    repo: trim(input.repo),
    route: trim(input.route),
    wait_for_selector: trim(input.wait_for_selector),
    reference: trim(input.reference),
    verification_mode: trim(input.verification_mode)?.toLowerCase(),
    target_image_url: trim(input.target_image_url),
    target_image_hash: trim(input.target_image_hash),
    viewport_matrix: input.viewport_matrix,
    deterministic_setup: input.deterministic_setup,
    assertions: input.assertions,
    capture_script_hash: hashString(input.capture_script)
  });
}
function visualSessionFingerprint(input) {
  const basis = input.version === RIDDLE_PROOF_VISUAL_SESSION_FINGERPRINT_VERSION ? input : visualSessionFingerprintBasis(input);
  return (0, import_node_crypto.createHash)("sha256").update(stableJson(basis)).digest("hex");
}
function buildVisualProofSession(input) {
  const basis = visualSessionFingerprintBasis(input);
  const fingerprint = visualSessionFingerprint(basis);
  const session = {
    version: RIDDLE_PROOF_VISUAL_SESSION_VERSION,
    session_id: `rps_${(/* @__PURE__ */ new Date()).toISOString().replace(/[-:.TZ]/g, "").slice(0, 14)}_${(0, import_node_crypto.randomUUID)().slice(0, 8)}`,
    run_id: trim(input.run_id),
    parent_session_id: input.parent?.session_id || null,
    parent_fingerprint: input.parent?.fingerprint || null,
    created_at: (/* @__PURE__ */ new Date()).toISOString(),
    fingerprint,
    fingerprint_basis: basis,
    repo: trim(input.repo),
    branch: trim(input.branch),
    route: withoutUndefined({
      path: trim(input.route),
      observed_after_path: trim(input.observed_after_path)
    }),
    reference: trim(input.reference),
    verification_mode: trim(input.verification_mode),
    target_image: withoutUndefined({
      url: trim(input.target_image_url),
      hash: trim(input.target_image_hash)
    }),
    viewport_matrix: input.viewport_matrix,
    deterministic_setup: input.deterministic_setup,
    capture: withoutUndefined({
      proof_plan: trim(input.proof_plan),
      capture_script: trim(input.capture_script),
      wait_for_selector: trim(input.wait_for_selector)
    }),
    assertions: input.assertions,
    artifacts: input.artifacts,
    evidence: input.evidence,
    status: trim(input.status)
  };
  return JSON.parse(JSON.stringify(session));
}
function parseVisualProofSession(value) {
  const parsed = typeof value === "string" ? JSON.parse(value) : value;
  if (!parsed || typeof parsed !== "object") throw new Error("proof session must be a JSON object");
  const session = parsed;
  if (session.version !== RIDDLE_PROOF_VISUAL_SESSION_VERSION) {
    throw new Error(`unsupported proof session version: ${String(session.version || "")}`);
  }
  if (!session.session_id) throw new Error("proof session missing session_id");
  if (!session.fingerprint) throw new Error("proof session missing fingerprint");
  return session;
}
function compareVisualProofSessionFingerprint(parent, input) {
  const actual = visualSessionFingerprintBasis(input);
  const expected = parent.fingerprint_basis || {};
  const keys = /* @__PURE__ */ new Set([...Object.keys(expected), ...Object.keys(actual)]);
  const expectedRecord = expected;
  const actualRecord = actual;
  const mismatches = [];
  for (const key of keys) {
    const expectedValue = expectedRecord[key];
    const actualValue = actualRecord[key];
    if (stableJson(expectedValue) !== stableJson(actualValue)) {
      mismatches.push({ key, expected: expectedValue, actual: actualValue });
    }
  }
  return mismatches;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  RIDDLE_PROOF_VISUAL_SESSION_FINGERPRINT_VERSION,
  RIDDLE_PROOF_VISUAL_SESSION_VERSION,
  buildVisualProofSession,
  compareVisualProofSessionFingerprint,
  parseVisualProofSession,
  visualSessionFingerprint,
  visualSessionFingerprintBasis
});
