"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/spec/public-state.ts
var public_state_exports = {};
__export(public_state_exports, {
  riddleProofPublicStateAllowsClaim: () => riddleProofPublicStateAllowsClaim,
  riddleProofPublicStateAllowsMergeRecommendation: () => riddleProofPublicStateAllowsMergeRecommendation,
  riddleProofPublicStateMergeRecommendation: () => riddleProofPublicStateMergeRecommendation,
  summarizeRiddleProofAgentSummarySurface: () => summarizeRiddleProofAgentSummarySurface,
  summarizeRiddleProofHostedProofViewSurface: () => summarizeRiddleProofHostedProofViewSurface,
  summarizeRiddleProofPublicConsumerSurface: () => summarizeRiddleProofPublicConsumerSurface,
  summarizeRiddleProofPublicState: () => summarizeRiddleProofPublicState
});
module.exports = __toCommonJS(public_state_exports);

// src/public-state.ts
function riddleProofPublicStateAllowsClaim(summary, claim) {
  return !summary?.prohibited_claims.includes(claim);
}
function riddleProofPublicStateAllowsMergeRecommendation(summary) {
  return riddleProofPublicStateAllowsClaim(summary, "merge_ready") && riddleProofPublicStateAllowsClaim(summary, "sync_allowed");
}
function riddleProofPublicStateMergeRecommendation(summary, recommendation) {
  return recommendation && riddleProofPublicStateAllowsMergeRecommendation(summary) ? recommendation : void 0;
}
function asRecord(value) {
  return value && typeof value === "object" && !Array.isArray(value) ? value : {};
}
function stringValue(value) {
  return typeof value === "string" && value.trim() ? value.trim() : void 0;
}
function booleanValue(value) {
  return typeof value === "boolean" ? value : void 0;
}
function numberValue(value) {
  return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
function firstStringValue(...values) {
  for (const value of values) {
    const text = stringValue(value);
    if (text) return text;
  }
  return void 0;
}
function firstBooleanValue(...values) {
  for (const value of values) {
    const bool = booleanValue(value);
    if (typeof bool === "boolean") return bool;
  }
  return void 0;
}
function firstRecordValue(...values) {
  for (const value of values) {
    const record = asRecord(value);
    if (Object.keys(record).length) return record;
  }
  return void 0;
}
function countValue(value) {
  const number = numberValue(value);
  return typeof number === "number" && number > 0 ? Math.trunc(number) : 0;
}
function checkpointSummaryFrom(...values) {
  const record = firstRecordValue(...values);
  if (!record) return void 0;
  const accepted = countValue(record.response_count);
  const rejected = countValue(record.rejected_response_count);
  const ignored = countValue(record.ignored_response_count);
  const duplicate = countValue(record.duplicate_response_count);
  const summary = {
    pending: booleanValue(record.pending),
    accepted_response_count: accepted,
    rejected_response_count: rejected,
    ignored_response_count: ignored,
    duplicate_response_count: duplicate,
    latest_decision: stringValue(record.latest_decision),
    audit_disclosure_required: rejected > 0 || ignored > 0 || duplicate > 0
  };
  return Object.values(summary).some((value) => typeof value !== "undefined") ? summary : void 0;
}
function uniqueStrings(values) {
  return [...new Set(values.filter(Boolean))];
}
function statusIsProofFailed(status) {
  return status === "failed" || status === "product_regression";
}
function statusIsProofBlocked(status) {
  return status === "blocked" || status === "proof_insufficient" || status === "environment_blocked" || status === "configuration_error" || status === "needs_human_review";
}
function statusRequiresDisclosure(status) {
  if (status === "proof_insufficient") return "proof_insufficient";
  if (status === "environment_blocked") return "environment_blocked";
  if (status === "configuration_error") return "configuration_error";
  if (status === "needs_human_review") return "human_review_required";
  return void 0;
}
function summarizeRiddleProofPublicState(input) {
  const record = asRecord(input);
  const runCard = asRecord(record.run_card);
  const stopCondition = asRecord(runCard.stop_condition);
  const raw = asRecord(record.raw);
  const request = asRecord(record.request);
  const requestMetadata = asRecord(record.request_metadata);
  const prState = asRecord(record.pr_state);
  const handoff = asRecord(record.pr_handoff_policy);
  const handoffState = stringValue(handoff.state);
  const status = firstStringValue(record.status, stopCondition.status);
  const ok = booleanValue(record.ok) ?? null;
  const shipMode = firstStringValue(request.ship_mode, requestMetadata.ship_mode, record.ship_mode, handoff.ship_mode);
  const explicitShippingDisabled = firstBooleanValue(
    record.shipping_disabled,
    stopCondition.shipping_disabled,
    raw.shipping_disabled,
    handoff.shipping_disabled
  );
  const shippingDisabled = explicitShippingDisabled === true || shipMode === "none" || handoffState === "proof_complete_ship_disabled";
  const explicitShipAuthorized = firstBooleanValue(
    record.ship_authorized,
    stopCondition.ship_authorized,
    raw.ship_authorized
  );
  const authorizationEvidence = Boolean(
    status === "shipped" || record.marked_ready === true || stringValue(prState.status) === "merged" || record.merge_commit || record.merged_at
  );
  const shipAuthorizedBeforeHold = explicitShipAuthorized ?? authorizationEvidence;
  const explicitShipHeld = firstBooleanValue(record.ship_held, stopCondition.ship_held, raw.ship_held);
  const inferredHeld = status === "ready_to_ship" && shippingDisabled && !shipAuthorizedBeforeHold;
  const shipHeld = explicitShipHeld === true || inferredHeld;
  const shipAuthorized = shipHeld ? false : shipAuthorizedBeforeHold === true;
  const proofComplete = Boolean(
    status === "ready_to_ship" || status === "shipped" || status === "completed" || status === "passed" || ok === true || handoff.proof_complete === true
  );
  const checkpointSummary = checkpointSummaryFrom(
    record.checkpoint_summary,
    stopCondition.checkpoint_summary,
    asRecord(record.details).checkpoint_summary,
    asRecord(raw.details).checkpoint_summary
  );
  const blockedOrWaiting = statusIsProofBlocked(status) || statusIsProofFailed(status) || status === "awaiting_checkpoint" || handoffState === "proof_blocked" || handoffState === "proof_review_required" || handoffState === "proof_failed" || handoffState === "proof_checkpoint_required";
  const proofPassed = Boolean(proofComplete && !blockedOrWaiting);
  const explicitMergeReady = firstBooleanValue(record.merge_ready, stopCondition.merge_ready, raw.merge_ready, handoff.merge_ready);
  const normalPrAllowed = firstBooleanValue(record.normal_pr_allowed, raw.normal_pr_allowed, handoff.normal_pr_allowed);
  const baseHandoffAllowed = !blockedOrWaiting && !shipHeld && !shippingDisabled;
  const mergeReady = baseHandoffAllowed && normalPrAllowed !== false && (explicitMergeReady ?? shipAuthorized);
  const syncAllowed = mergeReady;
  let policyState = "unknown";
  if (status === "awaiting_checkpoint" || handoffState === "proof_checkpoint_required") policyState = "awaiting_checkpoint";
  else if (statusIsProofFailed(status) || handoffState === "proof_failed") policyState = "proof_failed";
  else if (statusIsProofBlocked(status) || handoffState === "proof_blocked" || handoffState === "proof_review_required") policyState = "proof_blocked";
  else if (handoffState === "proof_complete_ship_disabled") policyState = "proof_complete_ship_disabled";
  else if (proofComplete && shipHeld && !shipAuthorized) policyState = "proof_passed_ship_held";
  else if (proofComplete && shippingDisabled && !shipAuthorized) policyState = "proof_complete_ship_disabled";
  else if (shipAuthorized) policyState = "ship_authorized";
  else if (proofPassed) policyState = "proof_passed";
  else if (status === "running") policyState = "proof_in_progress";
  const requiredDisclosures = [];
  if (shipHeld) requiredDisclosures.push("ship_held");
  if (shippingDisabled) requiredDisclosures.push("shipping_disabled");
  if (checkpointSummary?.audit_disclosure_required) requiredDisclosures.push("checkpoint_audit_counters");
  if (status === "awaiting_checkpoint" || handoffState === "proof_checkpoint_required") requiredDisclosures.push("checkpoint_required");
  const statusDisclosure = statusRequiresDisclosure(status);
  if (statusDisclosure) requiredDisclosures.push(statusDisclosure);
  const prohibitedClaims = [];
  if (!shipAuthorized || shipHeld || shippingDisabled) prohibitedClaims.push("ship_authorized", "shipped");
  if (!mergeReady) prohibitedClaims.push("merge_ready");
  if (!syncAllowed) prohibitedClaims.push("sync_allowed");
  if (blockedOrWaiting) {
    prohibitedClaims.push("proof_passed", "ready_to_ship");
  }
  if (checkpointSummary?.audit_disclosure_required) {
    prohibitedClaims.push("all_checkpoint_responses_accepted");
  }
  const resultLabel = status === "product_regression" ? "product_regression" : status === "proof_insufficient" ? "proof_insufficient" : status === "environment_blocked" ? "environment_blocked" : status === "configuration_error" ? "configuration_error" : status === "needs_human_review" ? "needs_human_review" : policyState === "awaiting_checkpoint" ? "checkpoint required" : policyState === "proof_blocked" ? "blocked" : policyState === "proof_failed" ? "failed" : policyState === "proof_complete_ship_disabled" ? "proof complete; shipping disabled" : policyState === "proof_passed_ship_held" ? "proof passed; ship held" : policyState === "ship_authorized" ? status === "shipped" ? "shipped" : "ship authorized" : policyState === "proof_passed" ? "passed" : policyState === "proof_in_progress" ? "running" : status || "recorded";
  return {
    status,
    ok,
    policy_state: policyState,
    result_label: resultLabel,
    proof_complete: proofComplete,
    proof_passed: proofPassed,
    ship_held: shipHeld,
    shipping_disabled: shippingDisabled,
    ship_authorized: shipAuthorized,
    merge_ready: mergeReady,
    sync_allowed: syncAllowed,
    checkpoint_summary: checkpointSummary,
    required_disclosures: uniqueStrings(requiredDisclosures),
    prohibited_claims: uniqueStrings(prohibitedClaims)
  };
}
function summarizeRiddleProofPublicConsumerSurface(input, options = {}) {
  const record = asRecord(input);
  const runCard = asRecord(record.run_card);
  const stopCondition = asRecord(runCard.stop_condition);
  const raw = asRecord(record.raw);
  const publicState = summarizeRiddleProofPublicState(input);
  const checkpointAudit = publicState.checkpoint_summary;
  const mergeRecommendation = riddleProofPublicStateMergeRecommendation(
    publicState,
    firstStringValue(
      options.merge_recommendation,
      record.merge_recommendation,
      stopCondition.merge_recommendation,
      raw.merge_recommendation
    )
  );
  const checkpointAuditRequired = publicState.required_disclosures.includes("checkpoint_audit_counters");
  return {
    kind: options.kind || "run_result",
    status: publicState.status,
    result_label: publicState.result_label,
    policy_state: publicState.policy_state,
    proof_complete: publicState.proof_complete,
    claims: {
      proof_passed: publicState.proof_passed,
      ship_authorized: publicState.ship_authorized,
      merge_ready: publicState.merge_ready,
      sync_allowed: publicState.sync_allowed,
      all_checkpoint_responses_accepted: checkpointAudit ? !checkpointAudit.audit_disclosure_required : false
    },
    ship_control: {
      ship_held: publicState.ship_held,
      shipping_disabled: publicState.shipping_disabled,
      ship_authorized: publicState.ship_authorized
    },
    handoff: {
      merge_ready: publicState.merge_ready,
      sync_allowed: publicState.sync_allowed,
      merge_recommendation: mergeRecommendation
    },
    checkpoint_audit: checkpointAudit,
    disclosures: {
      ship_control: true,
      checkpoint_audit: !checkpointAuditRequired || Boolean(checkpointAudit),
      required: [...publicState.required_disclosures],
      prohibited_claims: [...publicState.prohibited_claims]
    },
    public_state: publicState
  };
}
function summarizeRiddleProofHostedProofViewSurface(input, options = {}) {
  return summarizeRiddleProofPublicConsumerSurface(input, {
    ...options,
    kind: "hosted_proof_view"
  });
}
function summarizeRiddleProofAgentSummarySurface(input, options = {}) {
  return summarizeRiddleProofPublicConsumerSurface(input, {
    ...options,
    kind: "agent_summary"
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  riddleProofPublicStateAllowsClaim,
  riddleProofPublicStateAllowsMergeRecommendation,
  riddleProofPublicStateMergeRecommendation,
  summarizeRiddleProofAgentSummarySurface,
  summarizeRiddleProofHostedProofViewSurface,
  summarizeRiddleProofPublicConsumerSurface,
  summarizeRiddleProofPublicState
});
