"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RootWalletKeys = void 0;
const bip32_js_1 = require("../bip32.js");
const wasm_utxo_js_1 = require("../wasm/wasm_utxo.js");
/**
 * Convert WalletKeysArg to a triple of BIP32 instances
 */
function toBIP32Triple(keys) {
    if (keys instanceof RootWalletKeys) {
        return [keys.userKey(), keys.backupKey(), keys.bitgoKey()];
    }
    // Check if it's an IWalletKeys object
    if (typeof keys === "object" && "triple" in keys) {
        // Extract BIP32 keys from the triple
        return keys.triple.map((key) => bip32_js_1.BIP32.from(key));
    }
    // Otherwise it's a triple of strings (xpubs)
    return keys.map((xpub) => bip32_js_1.BIP32.fromWasm(wasm_utxo_js_1.WasmBIP32.from_xpub(xpub)));
}
/**
 * Extract derivation prefixes from WalletKeysArg, if present
 */
function extractDerivationPrefixes(keys) {
    if (typeof keys === "object" && "derivationPrefixes" in keys) {
        return keys.derivationPrefixes;
    }
    return null;
}
/**
 * RootWalletKeys represents a set of three extended public keys with their derivation prefixes
 */
class RootWalletKeys {
    _wasm;
    constructor(_wasm) {
        this._wasm = _wasm;
    }
    /**
     * Create a RootWalletKeys instance from a WasmRootWalletKeys instance (internal use)
     * @internal
     */
    static fromWasm(wasm) {
        return new RootWalletKeys(wasm);
    }
    /**
     * Create a RootWalletKeys from various input formats
     * @param keys - Can be a triple of xpub strings, an IWalletKeys object, or another RootWalletKeys instance
     * @returns A RootWalletKeys instance
     */
    static from(keys) {
        if (keys instanceof RootWalletKeys) {
            return keys;
        }
        const [user, backup, bitgo] = toBIP32Triple(keys);
        const derivationPrefixes = extractDerivationPrefixes(keys);
        const wasm = derivationPrefixes
            ? wasm_utxo_js_1.WasmRootWalletKeys.with_derivation_prefixes(user.wasm, backup.wasm, bitgo.wasm, derivationPrefixes[0], derivationPrefixes[1], derivationPrefixes[2])
            : new wasm_utxo_js_1.WasmRootWalletKeys(user.wasm, backup.wasm, bitgo.wasm);
        return new RootWalletKeys(wasm);
    }
    /**
     * Create a RootWalletKeys from three xpub strings
     * Uses default derivation prefix of m/0/0 for all three keys
     * @param xpubs - Triple of xpub strings
     * @returns A RootWalletKeys instance
     */
    static fromXpubs(xpubs) {
        const [user, backup, bitgo] = xpubs.map((xpub) => wasm_utxo_js_1.WasmBIP32.from_xpub(xpub));
        const wasm = new wasm_utxo_js_1.WasmRootWalletKeys(user, backup, bitgo);
        return new RootWalletKeys(wasm);
    }
    /**
     * Create a RootWalletKeys from three xpub strings with custom derivation prefixes
     * @param xpubs - Triple of xpub strings
     * @param derivationPrefixes - Triple of derivation path strings (e.g., ["0/0", "0/0", "0/0"])
     * @returns A RootWalletKeys instance
     */
    static withDerivationPrefixes(xpubs, derivationPrefixes) {
        const [user, backup, bitgo] = xpubs.map((xpub) => wasm_utxo_js_1.WasmBIP32.from_xpub(xpub));
        const wasm = wasm_utxo_js_1.WasmRootWalletKeys.with_derivation_prefixes(user, backup, bitgo, derivationPrefixes[0], derivationPrefixes[1], derivationPrefixes[2]);
        return new RootWalletKeys(wasm);
    }
    /**
     * Get the user key (first xpub)
     * @returns The user key as a BIP32 instance
     */
    userKey() {
        const wasm = this._wasm.user_key();
        return bip32_js_1.BIP32.fromWasm(wasm);
    }
    /**
     * Get the backup key (second xpub)
     * @returns The backup key as a BIP32 instance
     */
    backupKey() {
        const wasm = this._wasm.backup_key();
        return bip32_js_1.BIP32.fromWasm(wasm);
    }
    /**
     * Get the BitGo key (third xpub)
     * @returns The BitGo key as a BIP32 instance
     */
    bitgoKey() {
        const wasm = this._wasm.bitgo_key();
        return bip32_js_1.BIP32.fromWasm(wasm);
    }
    /**
     * Get the underlying WASM instance (internal use only)
     * @internal
     */
    get wasm() {
        return this._wasm;
    }
}
exports.RootWalletKeys = RootWalletKeys;
