"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchResources = matchResources;
exports.currencyMatchesCwAsset = currencyMatchesCwAsset;
const ethers_1 = require("ethers");
const resourceRegistry_1 = require("../resourceRegistry");
const ADDR_RE = /0x[a-fA-F0-9]{40}\b/g;
const CW_SYMBOL_RE = /\bcW[A-Z]{2,6}\b/g;
const C_SYMBOL_RE = /\bc[A-Z]{2,6}\b/g;
function fieldLabel(index, snippet) {
    return `text[${index}]:${snippet.slice(0, 40)}`;
}
function matchResources(registry, parsed) {
    const matches = [];
    const seen = new Set();
    const texts = [
        parsed.searchableText,
        ...(0, resourceRegistry_1.collectSearchableFields)(parsed.parsed),
    ].filter(Boolean);
    const push = (m) => {
        const key = `${m.kind}|${m.value.toLowerCase()}|${m.field}`;
        if (seen.has(key))
            return;
        seen.add(key);
        matches.push(m);
    };
    texts.forEach((text, idx) => {
        const lower = text.toLowerCase();
        for (const name of registry.chainNames) {
            if (lower.includes(name)) {
                push({ kind: 'chain138_name', value: name, field: fieldLabel(idx, text) });
            }
        }
        for (const prefix of registry.instructionPrefixes) {
            if (text.includes(prefix)) {
                push({ kind: 'instruction_prefix', value: prefix, field: fieldLabel(idx, text) });
            }
        }
        for (const kw of registry.remittanceKeywords) {
            if (lower.includes(kw)) {
                push({ kind: 'remittance_keyword', value: kw, field: fieldLabel(idx, text) });
            }
        }
        for (const sym of registry.symbols) {
            const re = new RegExp(`\\b${sym.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'i');
            if (re.test(text)) {
                const kind = sym.startsWith('cW') ? 'cw_symbol' : 'chain138_symbol';
                push({ kind, value: sym, symbol: sym, field: fieldLabel(idx, text), chainId: sym.startsWith('cW') ? undefined : 138 });
            }
        }
        for (const m of text.matchAll(CW_SYMBOL_RE)) {
            const sym = m[0];
            if (registry.cwSymbols.has(sym) || registry.symbols.has(sym)) {
                push({ kind: 'cw_symbol', value: sym, symbol: sym, field: fieldLabel(idx, text) });
            }
        }
        for (const m of text.matchAll(C_SYMBOL_RE)) {
            const sym = m[0];
            if (registry.symbols.has(sym)) {
                push({ kind: 'chain138_symbol', value: sym, symbol: sym, chainId: 138, field: fieldLabel(idx, text) });
            }
        }
        for (const m of text.matchAll(ADDR_RE)) {
            const addr = m[0];
            const meta = registry.addresses.get(addr.toLowerCase());
            if (meta) {
                push({
                    kind: meta.chainId === 138 ? 'chain138_address' : 'cw_address',
                    value: ethers_1.ethers.getAddress(addr),
                    symbol: meta.symbol,
                    chainId: meta.chainId,
                    field: fieldLabel(idx, text),
                });
            }
        }
    });
    for (const c of registry.contracts) {
        if (parsed.searchableText.toLowerCase().includes(c.toLowerCase())) {
            push({
                kind: 'contract_ref',
                value: ethers_1.ethers.getAddress(c),
                chainId: 138,
                field: 'searchableText',
            });
        }
    }
    return matches;
}
function currencyMatchesCwAsset(currency, registry) {
    if (!currency)
        return false;
    const c = currency.toUpperCase();
    for (const sym of registry.cwSymbols) {
        if (c === sym.toUpperCase())
            return true;
    }
    for (const sym of registry.symbols) {
        if (c === sym.toUpperCase())
            return true;
    }
    return false;
}
//# sourceMappingURL=resourceMatcher.js.map