'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { useMemo, useCallback } from 'react';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import 'eventemitter3';
import '@dynamic-labs/utils';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../constants/colors.js';
import '../../constants/values.js';
import '../../../shared/consts/index.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../store/state/nonce/nonce.js';
import '@dynamic-labs-wallet/browser-wallet-client';
import '@dynamic-labs/locale';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import '../../../events/dynamicEvents.js';
import { useProjectSettings } from '../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.js';
import { useDynamicWaas } from '../useDynamicWaas/useDynamicWaas.js';

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();
    const projectSettings = useProjectSettings();
    const enabledChainNames = 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 = 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 = useCallback((chainName) => __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 = useCallback((...args_1) => __awaiter(void 0, [...args_1], void 0, function* (input = {}) {
        const client = yield getBusinessClient();
        return client.businessAccounts.createBusinessAccount(input);
    }), [getBusinessClient]);
    const listBusinessAccounts = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.businessAccounts.listBusinessAccounts();
    }), [getBusinessClient]);
    const getBusinessAccount = useCallback((businessAccountId) => __awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.businessAccounts.getBusinessAccount(businessAccountId);
    }), [getBusinessClient]);
    const addMember = useCallback((input) => __awaiter(void 0, void 0, void 0, function* () {
        const client = yield getBusinessClient();
        return client.businessAccounts.addMember(input);
    }), [getBusinessClient]);
    const addWalletToBusinessAccount = useCallback((input) => __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 = useCallback((_a) => __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 = useCallback((_b) => __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 = useCallback(() => __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,
    };
};

export { useBusinessAccounts };
