// SPDX-License-Identifier: Apache-2.0
import { KeyType } from "./KeyProps";
import { PublicKey as HPublicKey } from "@hiero-ledger/sdk";
import { RuntimeError } from "../../../core/error/RuntimeError";
export default class PublicKey {
    static NULL = new PublicKey({
        key: "null",
        type: 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("Invalid public key");
            }
            key = this.formatKey(params.key);
            type = params.type ?? HPublicKey.fromString(key)._key._type;
        }
        PublicKey.validate(key);
        this.key = key;
        this.type = type;
    }
    getTypeFromLength(params) {
        switch (params.length) {
            case 66 | 68:
                return KeyType.ECDSA;
            default:
                return 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 HPublicKey.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;
    }
}
