import { c as stringEnum, i as jsonResult, r as errorResult, s as readStringParam, u as Type } from "../../tool-helpers-CVAcqiCy.mjs";
import { hasBankrApi } from "../services/bankr-api.mjs";
//#region extensions/crypto/src/tools/bankr-polymarket.ts
/**
* Bankr Polymarket Tool — prediction markets via Bankr Agent API.
*
* Search markets, place bets, view positions, and redeem winnings.
* All operations go through Bankr's prompt API. Executes on Polygon.
* Requires Bankr wallet (/connect_bankr).
*/
const BankrPolymarketSchema = Type.Object({
	action: stringEnum([
		"search",
		"bet",
		"positions",
		"redeem"
	], { description: "search: find prediction markets. bet: place a bet. positions: view your current positions. redeem: claim winnings from resolved markets." }),
	query: Type.Optional(Type.String({ description: "Search query for finding markets (e.g. \"eagles win\", \"bitcoin price\", \"election\")." })),
	market: Type.Optional(Type.String({ description: "Market description or ID to bet on." })),
	outcome: Type.Optional(Type.String({ description: "Outcome to bet on: \"yes\", \"no\", or a specific outcome name." })),
	amount: Type.Optional(Type.String({ description: "Dollar amount to bet (e.g. \"10\", \"50\")." }))
});
const SAFE_QUERY_RE = /^[a-zA-Z0-9 ?!,.\-'":;$%#@()]{1,200}$/;
const SAFE_AMOUNT_RE = /^\$?[0-9][0-9,._]*$/;
const SAFE_OUTCOME_RE = /^[a-zA-Z0-9 .\-]{1,60}$/;
function sanitizeQuery(input) {
	const trimmed = input.trim();
	if (SAFE_QUERY_RE.test(trimmed)) return trimmed;
	throw new Error(`Invalid query: contains disallowed characters. Keep it to simple text.`);
}
function sanitizeMarket(input) {
	const trimmed = input.trim();
	if (SAFE_QUERY_RE.test(trimmed)) return trimmed;
	throw new Error(`Invalid market: contains disallowed characters.`);
}
function sanitizeOutcome(input) {
	const trimmed = input.trim();
	if (SAFE_OUTCOME_RE.test(trimmed)) return trimmed;
	throw new Error(`Invalid outcome: "${trimmed.slice(0, 20)}". Use "yes", "no", or a simple name.`);
}
function sanitizeAmount(input) {
	const trimmed = input.trim();
	if (SAFE_AMOUNT_RE.test(trimmed)) return trimmed;
	throw new Error(`Invalid amount: "${trimmed.slice(0, 20)}". Use a number like "10" or "50".`);
}
function buildPrompt(action, params) {
	const query = readStringParam(params, "query") ? sanitizeQuery(readStringParam(params, "query")) : "";
	const market = readStringParam(params, "market") ? sanitizeMarket(readStringParam(params, "market")) : "";
	const outcome = readStringParam(params, "outcome") ? sanitizeOutcome(readStringParam(params, "outcome")) : "";
	const amount = readStringParam(params, "amount") ? sanitizeAmount(readStringParam(params, "amount")) : "";
	switch (action) {
		case "search": return `search polymarket for ${query}`;
		case "bet": return `bet $${amount} on ${outcome} for ${market}`;
		case "positions": return "show my Polymarket positions";
		case "redeem": return "redeem my winning polymarket positions";
		default: return action;
	}
}
function createBankrPolymarketTool() {
	return {
		name: "bankr_polymarket",
		label: "Bankr Polymarket",
		ownerOnly: true,
		description: "Prediction markets via Polymarket. Search for markets on any topic, place bets (yes/no), view your positions, and redeem winnings. Executes on Polygon via Bankr. Requires Bankr wallet (/connect_bankr).",
		parameters: BankrPolymarketSchema,
		execute: async (_toolCallId, args) => {
			const params = args;
			const action = readStringParam(params, "action", { required: true });
			if (!hasBankrApi()) return errorResult("Bankr API key not configured. Connect via /connect_bankr first.");
			if (action === "search" && !readStringParam(params, "query")) return errorResult("\"query\" is required for search (e.g. \"Will Bitcoin reach $100k?\").");
			if (action === "bet") {
				if (!readStringParam(params, "market")) return errorResult("\"market\" is required for bet. Search for markets first.");
				if (!readStringParam(params, "outcome")) return errorResult("\"outcome\" is required for bet (e.g. \"yes\" or \"no\").");
				if (!readStringParam(params, "amount")) return errorResult("\"amount\" is required for bet (dollar amount).");
			}
			try {
				const { bankrPromptAndPoll } = await import("../services/bankr-api.mjs");
				const result = await bankrPromptAndPoll(buildPrompt(action, params), { timeoutMs: action === "search" || action === "positions" ? 3e4 : 6e4 });
				if (result.status === "failed") return errorResult(`Polymarket ${action} failed: ${result.error ?? "Unknown error"}`);
				return jsonResult({
					status: "success",
					action,
					chain: "polygon",
					response: result.response,
					richData: result.richData,
					transactions: result.transactions
				});
			} catch (err) {
				return errorResult(`Polymarket ${action} failed: ${err instanceof Error ? err.message : String(err)}`);
			}
		}
	};
}
//#endregion
export { createBankrPolymarketTool };

//# sourceMappingURL=bankr-polymarket.mjs.map