"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/adapters/openclaw.ts
var openclaw_exports = {};
__export(openclaw_exports, {
  openClawIntegrationContext: () => openClawIntegrationContext,
  parseOpenClawAssertions: () => parseOpenClawAssertions,
  parseOpenClawJsonObjectOrArray: () => parseOpenClawJsonObjectOrArray,
  toRiddleProofRunParams: () => toRiddleProofRunParams
});
module.exports = __toCommonJS(openclaw_exports);

// src/result.ts
function compactRecord(input) {
  return Object.fromEntries(Object.entries(input).filter(([, value]) => value !== void 0 && value !== null && value !== ""));
}
function nonEmptyString(value) {
  return typeof value === "string" && value.trim() ? value.trim() : void 0;
}
function recordValue(value) {
  return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
}

// src/state.ts
function normalizeIntegrationContext(input, fallbackSource) {
  const value = recordValue(input);
  if (!value) {
    return fallbackSource ? { source: fallbackSource } : void 0;
  }
  const metadata = recordValue(value.metadata);
  return compactRecord({
    source: nonEmptyString(value.source) || fallbackSource,
    channel_id: nonEmptyString(value.channel_id),
    thread_id: nonEmptyString(value.thread_id),
    message_id: nonEmptyString(value.message_id),
    source_url: nonEmptyString(value.source_url),
    metadata: metadata && Object.keys(metadata).length ? metadata : void 0
  });
}
function normalizeRunParams(input) {
  return compactRecord({
    repo: input.repo,
    branch: input.branch,
    change_request: input.change_request,
    commit_message: input.commit_message,
    prod_url: input.prod_url,
    capture_script: input.capture_script,
    success_criteria: input.success_criteria,
    assertions: input.assertions,
    verification_mode: input.verification_mode,
    resume_session: input.resume_session,
    target_image_url: input.target_image_url,
    target_image_hash: input.target_image_hash,
    viewport_matrix: input.viewport_matrix,
    deterministic_setup: input.deterministic_setup,
    reference: input.reference,
    base_branch: input.base_branch,
    before_ref: input.before_ref,
    allow_static_preview_fallback: input.allow_static_preview_fallback,
    context: input.context,
    reviewer: input.reviewer,
    mode: input.mode,
    implementation_mode: input.implementation_mode,
    require_diff: input.require_diff,
    allow_code_changes: input.allow_code_changes,
    build_command: input.build_command,
    build_output: input.build_output,
    server_image: input.server_image,
    server_command: input.server_command,
    server_port: input.server_port,
    server_path: input.server_path,
    use_auth: input.use_auth,
    auth_localStorage_json: input.auth_localStorage_json,
    auth_cookies_json: input.auth_cookies_json,
    auth_headers_json: input.auth_headers_json,
    color_scheme: input.color_scheme,
    wait_for_selector: input.wait_for_selector,
    ship_mode: input.ship_mode,
    leave_draft: input.leave_draft,
    engine_state_path: input.engine_state_path,
    harness_state_path: input.harness_state_path,
    riddle_engine_module_url: input.riddle_engine_module_url,
    max_iterations: input.max_iterations,
    auto_approve: input.auto_approve,
    dry_run: input.dry_run,
    integration_context: normalizeIntegrationContext(input.integration_context)
  });
}

// src/openclaw.ts
function parseOpenClawAssertions(value) {
  if (value === void 0 || value === null || value === "") return void 0;
  if (typeof value !== "string") return value;
  const trimmed = value.trim();
  if (!trimmed) return void 0;
  try {
    return JSON.parse(trimmed);
  } catch {
    return trimmed;
  }
}
function parseOpenClawJsonObjectOrArray(value, name) {
  if (value === void 0 || value === null || value === "") return void 0;
  if (typeof value !== "string") return value;
  const trimmed = value.trim();
  if (!trimmed) return void 0;
  let parsed;
  try {
    parsed = JSON.parse(trimmed);
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);
    throw new Error(`${name} is not valid JSON: ${message}`);
  }
  if (parsed === null || typeof parsed !== "object" && !Array.isArray(parsed)) {
    throw new Error(`${name} must decode to a JSON object or array.`);
  }
  return parsed;
}
function normalizeOpenClawReference(value) {
  if (value !== "prod" && value !== "before" && value !== "both") return void 0;
  return value;
}
function ignoredOpenClawReference(value) {
  if (value === void 0 || value === null || value === "") return void 0;
  return normalizeOpenClawReference(value) ? void 0 : String(value);
}
function openClawIntegrationContext(params) {
  const hasDiscordContext = Boolean(params.discord_channel || params.discord_thread_id || params.discord_message_id || params.discord_source_url);
  return normalizeIntegrationContext({
    source: hasDiscordContext ? "discord" : "openclaw",
    channel_id: params.discord_channel,
    thread_id: params.discord_thread_id,
    message_id: params.discord_message_id,
    source_url: params.discord_source_url,
    metadata: compactRecord({
      wrapper: "openclaw",
      tool: "riddle_proof_change",
      reference_input_ignored: ignoredOpenClawReference(params.reference)
    })
  }, "openclaw");
}
function toRiddleProofRunParams(params) {
  return normalizeRunParams({
    repo: params.repo,
    branch: params.branch,
    change_request: params.change_request,
    commit_message: params.commit_message,
    prod_url: params.prod_url,
    capture_script: params.capture_script,
    success_criteria: params.success_criteria,
    assertions: parseOpenClawAssertions(params.assertions_json),
    verification_mode: params.verification_mode,
    resume_session: params.resume_session,
    target_image_url: params.target_image_url,
    target_image_hash: params.target_image_hash,
    viewport_matrix: parseOpenClawJsonObjectOrArray(params.viewport_matrix_json, "viewport_matrix_json"),
    deterministic_setup: parseOpenClawJsonObjectOrArray(params.deterministic_setup_json, "deterministic_setup_json"),
    reference: normalizeOpenClawReference(params.reference),
    base_branch: params.base_branch,
    before_ref: params.before_ref,
    allow_static_preview_fallback: params.allow_static_preview_fallback,
    context: params.context,
    reviewer: params.reviewer,
    mode: params.mode,
    implementation_mode: params.implementation_mode,
    require_diff: params.require_diff,
    allow_code_changes: params.allow_code_changes,
    build_command: params.build_command,
    build_output: params.build_output,
    server_image: params.server_image,
    server_command: params.server_command,
    server_port: params.server_port,
    server_path: params.server_path,
    use_auth: params.use_auth,
    auth_localStorage_json: params.auth_localStorage_json,
    auth_cookies_json: params.auth_cookies_json,
    auth_headers_json: params.auth_headers_json,
    color_scheme: params.color_scheme,
    wait_for_selector: params.wait_for_selector,
    ship_mode: params.ship_mode || (params.ship_after_verify ? "ship" : void 0),
    leave_draft: params.leave_draft,
    engine_state_path: params.state_path,
    harness_state_path: params.harness_state_path,
    max_iterations: params.max_iterations,
    auto_approve: params.auto_approve,
    dry_run: params.dry_run,
    integration_context: openClawIntegrationContext(params)
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  openClawIntegrationContext,
  parseOpenClawAssertions,
  parseOpenClawJsonObjectOrArray,
  toRiddleProofRunParams
});
