import { WasmBIP32 } from "./wasm/wasm_utxo.js";
/**
 * BIP32 wrapper class for extended key operations
 */
export class BIP32 {
    _wasm;
    constructor(_wasm) {
        this._wasm = _wasm;
    }
    /**
     * Create a BIP32 instance from a WasmBIP32 instance (internal use)
     * @internal
     */
    static fromWasm(wasm) {
        return new BIP32(wasm);
    }
    /**
     * Convert BIP32Arg to BIP32 instance
     * @param key - The BIP32 key in various formats
     * @returns BIP32 instance
     */
    static from(key) {
        // Short-circuit if already a BIP32 instance
        if (key instanceof BIP32) {
            return key;
        }
        // If it's a WasmBIP32 instance, wrap it
        if (key instanceof WasmBIP32) {
            return new BIP32(key);
        }
        // If it's a string, parse from base58
        if (typeof key === "string") {
            const wasm = WasmBIP32.from_base58(key);
            return new BIP32(wasm);
        }
        // If it's an object (BIP32Interface), use from_bip32_interface
        if (typeof key === "object" && key !== null) {
            const wasm = WasmBIP32.from_bip32_interface(key);
            return new BIP32(wasm);
        }
        throw new Error("Invalid BIP32Arg type");
    }
    /**
     * Create a BIP32 key from a base58 string (xpub/xprv/tpub/tprv)
     * @param base58Str - The base58-encoded extended key string
     * @returns A BIP32 instance
     */
    static fromBase58(base58Str) {
        const wasm = WasmBIP32.from_base58(base58Str);
        return new BIP32(wasm);
    }
    /**
     * Create a BIP32 master key from a seed
     * @param seed - The seed bytes
     * @param network - Optional network string
     * @returns A BIP32 instance
     */
    static fromSeed(seed, network) {
        const wasm = WasmBIP32.from_seed(seed, network);
        return new BIP32(wasm);
    }
    /**
     * Get the chain code as a Uint8Array
     */
    get chainCode() {
        return this._wasm.chain_code;
    }
    /**
     * Get the depth
     */
    get depth() {
        return this._wasm.depth;
    }
    /**
     * Get the child index
     */
    get index() {
        return this._wasm.index;
    }
    /**
     * Get the parent fingerprint
     */
    get parentFingerprint() {
        return this._wasm.parent_fingerprint;
    }
    /**
     * Get the private key as a Uint8Array (if available)
     */
    get privateKey() {
        return this._wasm.private_key;
    }
    /**
     * Get the public key as a Uint8Array
     */
    get publicKey() {
        return this._wasm.public_key;
    }
    /**
     * Get the identifier as a Uint8Array
     */
    get identifier() {
        return this._wasm.identifier;
    }
    /**
     * Get the fingerprint as a Uint8Array
     */
    get fingerprint() {
        return this._wasm.fingerprint;
    }
    /**
     * Check if this is a neutered (public) key
     * @returns True if the key is public-only (neutered)
     */
    isNeutered() {
        return this._wasm.is_neutered();
    }
    /**
     * Get the neutered (public) version of this key
     * @returns A new BIP32 instance containing only the public key
     */
    neutered() {
        const wasm = this._wasm.neutered();
        return new BIP32(wasm);
    }
    /**
     * Serialize to base58 string
     * @returns The base58-encoded extended key string
     */
    toBase58() {
        return this._wasm.to_base58();
    }
    /**
     * Get the WIF encoding of the private key
     * @returns The WIF-encoded private key
     */
    toWIF() {
        return this._wasm.to_wif();
    }
    /**
     * Derive a normal (non-hardened) child key
     * @param index - The child index
     * @returns A new BIP32 instance for the derived key
     */
    derive(index) {
        const wasm = this._wasm.derive(index);
        return new BIP32(wasm);
    }
    /**
     * Derive a hardened child key (only works for private keys)
     * @param index - The child index
     * @returns A new BIP32 instance for the derived key
     */
    deriveHardened(index) {
        const wasm = this._wasm.derive_hardened(index);
        return new BIP32(wasm);
    }
    /**
     * Derive a key using a derivation path (e.g., "0/1/2" or "m/0/1/2")
     * @param path - The derivation path string
     * @returns A new BIP32 instance for the derived key
     */
    derivePath(path) {
        const wasm = this._wasm.derive_path(path);
        return new BIP32(wasm);
    }
    /**
     * Get the underlying WASM instance (internal use only)
     * @internal
     */
    get wasm() {
        return this._wasm;
    }
}
