"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.kvStoreInterpreter = void 0;
const index_js_1 = require("../script/index.js");
const Utils = __importStar(require("../primitives/utils.js"));
const types_js_1 = require("./types.js");
/**
 * KVStore interpreter used by Historian.
 *
 * Validates the KVStore PushDrop tokens: [protocolID, key, value, controller, signature] (old format)
 * or [protocolID, key, value, controller, tags, signature] (new format).
 * Filters outputs by the provided key in the interpreter context.
 * Produces the plaintext value for matching outputs; returns undefined otherwise.
 *
 * @param transaction - The transaction to inspect.
 * @param outputIndex - The index of the output within transaction.outputs.
 * @param ctx - { key: string, protocolID: WalletProtocol } — per-call context specifying which key to match.
 *
 * @returns string | undefined — the decoded KV value if the output is a valid KVStore token for the
 *   given key; otherwise undefined.
 */
const kvStoreInterpreter = async (transaction, outputIndex, ctx) => {
    try {
        const output = transaction.outputs[outputIndex];
        if (output?.lockingScript == null)
            return undefined;
        if (ctx?.key == null)
            return undefined;
        // Decode the KVStore token
        const decoded = index_js_1.PushDrop.decode(output.lockingScript);
        // Support backwards compatibility: old format without tags, new format with tags
        const expectedFieldCount = Object.keys(types_js_1.kvProtocol).length;
        const hasTagsField = decoded.fields.length === expectedFieldCount;
        const isOldFormat = decoded.fields.length === expectedFieldCount - 1;
        if (!isOldFormat && !hasTagsField)
            return undefined;
        // Only return values for the given key and protocolID
        const key = Utils.toUTF8(decoded.fields[types_js_1.kvProtocol.key]);
        const protocolID = Utils.toUTF8(decoded.fields[types_js_1.kvProtocol.protocolID]);
        if (key !== ctx.key || protocolID !== JSON.stringify(ctx.protocolID))
            return undefined;
        try {
            return Utils.toUTF8(decoded.fields[types_js_1.kvProtocol.value]);
        }
        catch {
            return undefined;
        }
    }
    catch {
        // Skip non-KVStore outputs or malformed tokens
        return undefined;
    }
};
exports.kvStoreInterpreter = kvStoreInterpreter;
//# sourceMappingURL=kvStoreInterpreter.js.map