import {
  normalizeIntegrationContext,
  normalizeRunParams
} from "./chunk-UZIX7M7D.js";
import {
  compactRecord
} from "./chunk-CUBYSWZT.js";

// 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)
  });
}

export {
  parseOpenClawAssertions,
  parseOpenClawJsonObjectOrArray,
  openClawIntegrationContext,
  toRiddleProofRunParams
};
