"use strict";
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
const KeyProps_1 = require("./KeyProps");
const sdk_1 = require("@hiero-ledger/sdk");
const RuntimeError_1 = require("../../../core/error/RuntimeError");
class PublicKey {
    static NULL = new PublicKey({
        key: "null",
        type: KeyProps_1.KeyType.NULL,
    });
    key;
    type;
    constructor(params) {
        let key, type;
        if (typeof params === "string") {
            key = this.formatKey(params);
            type = this.getTypeFromLength(key);
        }
        else {
            if (!params.key) {
                throw new RuntimeError_1.RuntimeError("Invalid public key");
            }
            key = this.formatKey(params.key);
            type = params.type ?? sdk_1.PublicKey.fromString(key)._key._type;
        }
        PublicKey.validate(key);
        this.key = key;
        this.type = type;
    }
    getTypeFromLength(params) {
        switch (params.length) {
            case 66 | 68:
                return KeyProps_1.KeyType.ECDSA;
            default:
                return KeyProps_1.KeyType.ED25519;
        }
    }
    formatKey(key) {
        if (key.length > 0 && key.startsWith("0x")) {
            return key.substring(2);
        }
        return key;
    }
    static fromHederaKey(key) {
        return new PublicKey({
            key: key.toStringRaw(),
            type: key._key._type,
        });
    }
    static isNull(val) {
        if (!val)
            return false;
        return val.key === PublicKey.NULL.key && val.type === PublicKey.NULL.type;
    }
    toHederaKey() {
        return sdk_1.PublicKey.fromString(this.key);
    }
    toString() {
        return JSON.stringify({
            key: this.key,
            _type: this.type,
        });
    }
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    static validate(val) {
        const err = [];
        return err;
    }
}
exports.default = PublicKey;
