"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 util_1 = require("@polkadot/util");
const util_crypto_1 = require("@polkadot/util-crypto");
const U8aFixed_1 = __importDefault(require("../../codec/U8aFixed"));
/**
 * @name AccountId
 * @description
 * A wrapper around an AccountId/PublicKey representation. Since we are dealing with
 * underlying PublicKeys (32 bytes in length), we extend from U8aFixed which is
 * just a Uint8Array wrapper with a fixed length.
 */
class AccountId extends U8aFixed_1.default {
    constructor(registry, value = new Uint8Array()) {
        super(registry, AccountId.decodeAccountId(value), 256);
    }
    static encode(value) {
        return util_crypto_1.encodeAddress(value);
    }
    /** @internal */
    static decodeAccountId(value) {
        if (util_1.isU8a(value) || Array.isArray(value)) {
            return util_1.u8aToU8a(value);
        }
        else if (util_1.isHex(value)) {
            return util_1.hexToU8a(value.toString());
        }
        else if (util_1.isString(value)) {
            return util_crypto_1.decodeAddress(value.toString());
        }
        return value;
    }
    /**
     * @description Compares the value of the input to see if there is a match
     */
    eq(other) {
        return super.eq(AccountId.decodeAccountId(other));
    }
    /**
     * @description Converts the Object to JSON, typically used for RPC transfers
     */
    toJSON() {
        return this.toString();
    }
    /**
     * @description Returns the string representation of the value
     */
    toString() {
        return AccountId.encode(this);
    }
    /**
     * @description Returns the base runtime type name for this instance
     */
    toRawType() {
        return 'AccountId';
    }
}
exports.default = AccountId;
