"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Variant_1 = require("@wireio/sdk-core/chain/Variant");
const Name_1 = require("@wireio/sdk-core/chain/Name");
const Integer_1 = require("@wireio/sdk-core/chain/Integer");
const serializer_1 = require("@wireio/sdk-core/serializer");
const EXTENDED_VARIANT_ARM_COUNT = 129;
const EXTENDED_VARIANT_ARM_INDEX = 128;
const EXTENDED_VARIANT_PAYLOAD = 7;
const EXTENDED_VARIANT_HEX = "800107";
let MyVariant = class MyVariant extends Variant_1.Variant {
};
MyVariant = __decorate([
    Variant_1.Variant.type("my_variant", [Name_1.Name, Integer_1.UInt64])
], MyVariant);
const createExtendedVariantAbi = () => {
    const aliases = Array.from({ length: EXTENDED_VARIANT_ARM_COUNT }, (_, index) => ({
        new_type_name: `t${index}`,
        type: "uint8"
    }));
    return {
        version: "sysio::abi/1.1",
        types: aliases,
        variants: [
            {
                name: "vtype",
                types: aliases.map(alias => alias.new_type_name)
            }
        ],
        structs: [],
        actions: [],
        tables: [],
        ricardian_clauses: [],
        action_results: [],
        enums: []
    };
};
describe("Variant", () => {
    describe("from", () => {
        it("creates a variant with a Name value", () => {
            const v = MyVariant.from(["name", "alice"]);
            expect(v).toBeInstanceOf(MyVariant);
            expect(v.variantIdx).toBe(0);
            expect(v.value).toBeInstanceOf(Name_1.Name);
            expect(String(v.value)).toBe("alice");
        });
        it("creates a variant with a UInt64 value", () => {
            const v = MyVariant.from(["uint64", 42]);
            expect(v).toBeInstanceOf(MyVariant);
            expect(v.variantIdx).toBe(1);
        });
    });
    describe("equals", () => {
        it("returns true for equal variants", () => {
            const a = MyVariant.from(["name", "alice"]);
            const b = MyVariant.from(["name", "alice"]);
            expect(a.equals(b)).toBe(true);
        });
        it("returns false for different variant types", () => {
            const a = MyVariant.from(["name", "alice"]);
            const b = MyVariant.from(["uint64", 42]);
            expect(a.equals(b)).toBe(false);
        });
    });
    describe("variantName", () => {
        it("returns the name of the active variant type", () => {
            const v = MyVariant.from(["name", "alice"]);
            expect(v.variantName).toBe("name");
        });
    });
    describe("decorator", () => {
        it("registers ABI name via Variant.type", () => {
            expect(MyVariant.abiName).toBe("my_variant");
        });
        it("registers variant types", () => {
            expect(MyVariant.abiVariant).toBeDefined();
            expect(MyVariant.abiVariant.length).toBe(2);
        });
    });
    describe("ABI serialization", () => {
        it("decodes variant indexes as varuint32", () => {
            const abi = createExtendedVariantAbi();
            const encoded = serializer_1.Serializer.encode({
                object: [`t${EXTENDED_VARIANT_ARM_INDEX}`, EXTENDED_VARIANT_PAYLOAD],
                type: "vtype",
                abi
            });
            const decoder = new serializer_1.ABIDecoder(encoded.array);
            const decoded = serializer_1.Serializer.decode({
                data: decoder,
                type: "vtype",
                abi
            });
            expect(Array.isArray(decoded)).toBe(true);
            const [decodedName, decodedPayload] = decoded;
            expect(encoded.hexString).toBe(EXTENDED_VARIANT_HEX);
            expect(decodedName).toBe(`t${EXTENDED_VARIANT_ARM_INDEX}`);
            expect(decodedPayload).toBeInstanceOf(Integer_1.UInt8);
            expect(Number(decodedPayload)).toBe(EXTENDED_VARIANT_PAYLOAD);
            expect(decoder.getPosition()).toBe(encoded.length);
        });
    });
});
//# sourceMappingURL=Variant.test.js.map