'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var React = require('react');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-core');
require('eventemitter3');
require('@dynamic-labs/utils');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../context/ViewContext/ViewContext.cjs');
require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../constants/colors.cjs');
require('../../constants/values.cjs');
require('../../../shared/consts/index.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-wallet/browser-wallet-client');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.cjs');
require('../../../events/dynamicEvents.cjs');
var useProjectSettings = require('../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.cjs');
var useDynamicWaas = require('../useDynamicWaas/useDynamicWaas.cjs');

const NO_WAAS_CHAIN_ERROR = 'No enabled WaaS chain found for business account operations';
const NO_CONNECTOR_ERROR = 'WaaS wallet connector not found';
const useBusinessAccounts = () => {
    const { getWaasWalletConnector } = useDynamicWaas.useDynamicWaas();
    const projectSettings = useProjectSettings.useProjectSettings();
    const enabledChainNames = React.useMemo(() => {
        var _a, _b;
        return ((_b = (_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _a === void 0 ? void 0 : _a.chainConfigurations) !== null && _b !== void 0 ? _b : [])
            .filter((chainConfig) => chainConfig.enabled)
            .map((chainConfig) => chainConfig.name);
    }, [projectSettings]);
    const defaultChainName = React.useMemo(() => {
        var _a, _b, _c, _d, _e;
        const enabledChains = (_b = (_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _a === void 0 ? void 0 : _a.chainConfigurations) === null || _b === void 0 ? void 0 : _b.filter((chainConfig) => chainConfig.enabled);
        return ((_d = (_c = enabledChains === null || enabledChains === void 0 ? void 0 : enabledChains.find((chainConfig) => chainConfig.primary)) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : (_e = enabledChains === null || enabledChains === void 0 ? void 0 : enabledChains[0]) === null || _e === void 0 ? void 0 : _e.name);
    }, [projectSettings]);
    // Business-account ops are chain-agnostic; borrow any enabled WaaS connector
    // to reach the shared orchestrator client.
    const getBusinessClient = React.useCallback((chainName) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const chain = chainName !== null && chainName !== void 0 ? chainName : defaultChainName;
        if (!chain) {
            throw new Error(NO_WAAS_CHAIN_ERROR);
        }
        const connector = getWaasWalletConnector(chain);
        if (!connector) {
            throw new Error(NO_CONNECTOR_ERROR);
        }
        return connector.getWaasWalletClient();
    }), [defaultChainName, getWaasWalletConnector]);
    const createBusinessAccount = React.useCallback((...args_1) => _tslib.__awaiter(void 0, [...args_1], void 0, function* (input = {}) {
        const client = yield getBusinessClient();
        return client.businessAccounts.createBusinessAccount(input);
    }), [getBusinessClient]);
    const listBusinessAccounts = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.businessAccounts.listBusinessAccounts();
    }), [getBusinessClient]);
    const getBusinessAccount = React.useCallback((businessAccountId) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.businessAccounts.getBusinessAccount(businessAccountId);
    }), [getBusinessClient]);
    const addMember = React.useCallback((input) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.businessAccounts.addMember(input);
    }), [getBusinessClient]);
    const addWalletToBusinessAccount = React.useCallback((input) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.businessAccounts.addWalletToBusinessAccount(input);
    }), [getBusinessClient]);
    // Adds a signer via the SSE reshare ceremony on the wallet's chain connector.
    // Mints the new signer's share set and seeds it to client-keyshare-service.
    const addSigner = React.useCallback((_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ accountAddress, walletId, chainName, businessAccountId, targetSignerIdentity, signerType, password, }) {
        const connector = getWaasWalletConnector(chainName);
        if (!connector) {
            throw new Error(NO_CONNECTOR_ERROR);
        }
        return connector.addSignerViaReshare({
            accountAddress,
            businessAccountId,
            password,
            signerType,
            targetSignerIdentity,
            walletId,
        });
    }), [getWaasWalletConnector]);
    const createWalletForBusinessAccount = React.useCallback((_b) => _tslib.__awaiter(void 0, [_b], void 0, function* ({ businessAccountId, chainName, thresholdSignatureScheme, password, }) {
        const chain = chainName !== null && chainName !== void 0 ? chainName : defaultChainName;
        if (!chain) {
            throw new Error(NO_WAAS_CHAIN_ERROR);
        }
        const connector = getWaasWalletConnector(chain);
        if (!connector) {
            throw new Error(NO_CONNECTOR_ERROR);
        }
        return connector.createWalletAccount({
            businessAccountId,
            password,
            thresholdSignatureScheme,
        });
    }), [defaultChainName, getWaasWalletConnector]);
    const listMyWalletAccounts = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.getWallets();
    }), [getBusinessClient]);
    return {
        addMember,
        addSigner,
        addWalletToBusinessAccount,
        createBusinessAccount,
        createWalletForBusinessAccount,
        defaultChainName,
        enabledChainNames,
        getBusinessAccount,
        listBusinessAccounts,
        listMyWalletAccounts,
    };
};

exports.useBusinessAccounts = useBusinessAccounts;
