"use strict";
// Copyright 2017-2020 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const definitions_1 = __importDefault(require("../../interfaces/democracy/definitions"));
const util_1 = require("@polkadot/util");
const create_1 = require("../../codec/create");
const U8aFixed_1 = __importDefault(require("../../codec/U8aFixed"));
const Bool_1 = __importDefault(require("../Bool"));
exports.convictionNames = definitions_1.default.types.Conviction._enum;
// For votes, the topmost bit indicated aye/nay, the lower bits indicate the conviction
const AYE_BITS = 0b10000000;
const NAY_BITS = 0b00000000;
const CON_MASK = 0b01111111;
const DEF_CONV = 0b00000000; // the default conviction, None
/**
 * @name Vote
 * @description
 * A number of lock periods, plus a vote, one way or the other.
 */
class Vote extends U8aFixed_1.default {
    constructor(registry, value) {
        // decoded is just 1 byte
        // Aye: Most Significant Bit
        // Conviction: 0000 - 0101
        const decoded = Vote.decodeVote(registry, value);
        super(registry, decoded, 8);
        this._aye = (decoded[0] & AYE_BITS) === AYE_BITS;
        this._conviction = create_1.createType(this.registry, 'Conviction', decoded[0] & CON_MASK);
    }
    /** @internal */
    static decodeVote(registry, value) {
        if (util_1.isUndefined(value)) {
            return Vote.decodeVoteBool(false);
        }
        else if (value instanceof Boolean || util_1.isBoolean(value)) {
            return Vote.decodeVoteBool(new Bool_1.default(registry, value).isTrue);
        }
        else if (util_1.isNumber(value)) {
            return Vote.decodeVoteBool(value < 0);
        }
        else if (util_1.isU8a(value)) {
            return Vote.decodeVoteU8a(value);
        }
        const vote = new Bool_1.default(registry, value.aye).isTrue ? AYE_BITS : NAY_BITS;
        const conviction = create_1.createType(registry, 'Conviction', util_1.isUndefined(value.conviction) ? DEF_CONV : value.conviction);
        return new Uint8Array([vote | conviction.index]);
    }
    /** @internal */
    static decodeVoteBool(value) {
        return value
            ? new Uint8Array([AYE_BITS | DEF_CONV])
            : new Uint8Array([NAY_BITS]);
    }
    /** @internal */
    static decodeVoteU8a(value) {
        return value.length
            ? value.subarray(0, 1)
            : new Uint8Array([NAY_BITS]);
    }
    /**
     * @description returns a V2 conviction
     */
    get conviction() {
        return this._conviction;
    }
    /**
     * @description true if the wrapped value is a positive vote
     */
    get isAye() {
        return this._aye;
    }
    /**
     * @description true if the wrapped value is a negative vote
     */
    get isNay() {
        return !this.isAye;
    }
    /**
     * @description Returns the base runtime type name for this instance
     */
    toRawType() {
        return 'Vote';
    }
}
exports.default = Vote;
