/**
* open_purchase_link - Purchase link generator tool
*
* Generates Joker.com purchase links with optional affiliate tracking.
* Supports single or multiple domains with configurable URL opening behavior.
*/
import { generateJokerLink, generateMultiDomainLinks, calculateTotal } from '../affiliate-links.js';
import { priceCache } from '../price-cache.js';
/**
* Generate purchase link(s) for domain(s)
*/
export async function openPurchaseLink(args: {
domain?: string;
domains?: string[];
open_all?: boolean;
}): Promise<{
content: Array<{
type: string;
text: string;
}>;
}> {
const { domain, domains, open_all = false } = args;
// Validation: either domain XOR domains must be provided
if (!domain && !domains) {
throw new Error('Either domain or domains parameter must be provided');
}
if (domain && domains) {
throw new Error('Cannot specify both domain and domains parameters - choose one');
}
try {
// Single domain case
if (domain) {
const normalizedDomain = domain.toLowerCase().trim();
// Try to get pricing from cache for estimate
const pricingData = priceCache.get(normalizedDomain);
const link = generateJokerLink(normalizedDomain, pricingData || undefined);
return {
content: [{
type: 'text',
text: JSON.stringify({
message: 'Purchase link generated',
link,
suggestion: 'Use mcp__opener__open_browser to open this URL in Firefox',
}, null, 2),
}],
};
}
// Multiple domains case
if (domains) {
if (!Array.isArray(domains) || domains.length === 0) {
throw new Error('domains parameter must be a non-empty array');
}
// Get pricing data from cache for all domains
const pricingMap = new Map();
for (const domainName of domains) {
const normalized = domainName.toLowerCase().trim();
const pricing = priceCache.get(normalized);
if (pricing) {
pricingMap.set(normalized, pricing);
}
}
const links = generateMultiDomainLinks(domains, pricingMap);
const urlsToOpen = open_all ? links.map(l => l.url) : [links[0].url];
// Calculate total if pricing available
const totalInfo = calculateTotal(links);
return {
content: [{
type: 'text',
text: JSON.stringify({
message: `Purchase links generated for ${links.length} domain(s)`,
links,
urls_to_open: urlsToOpen,
open_all,
total_estimate: totalInfo,
suggestion: `Use mcp__opener__open_browser to open ${urlsToOpen.length} URL(s) in Firefox`,
note: totalInfo && totalInfo.domains_without_pricing > 0
? `${totalInfo.domains_without_pricing} domain(s) missing pricing data. Call get_domain_pricing first for better estimates.`
: undefined,
}, null, 2),
}],
};
}
// Should never reach here due to validation
throw new Error('Unexpected state: no domain or domains provided');
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to generate purchase link(s): ${message}`);
}
}