import { A as Api } from './Api-BulOmiMS.js';
import { S as SubAccount, C as ComponentErrorSeverity } from './Plaid-DxSkT8pY.js';
import './Pagination-CLHwhyvu.js';
import './Business-BcNb9bVO.js';
import './config-state-kKn89dp1.js';
import './dinero-HXpYMWUU.js';
import { g as getErrorCode, a as getErrorMessage } from './utils-CVYvs2hZ.js';

const api = Api();
class SubAccountService {
    async fetchSubAccounts(accountId, authToken, params) {
        const endpoint = 'sub_accounts';
        const headers = { Account: accountId };
        return api.get({ authToken, endpoint, params, headers });
    }
}

const makeGetSubAccounts = ({ primaryAccountId, authToken, service }) => async ({ params, onSuccess, onError }) => {
    var _a, _b;
    try {
        const response = await service.fetchSubAccounts(primaryAccountId, authToken, params);
        if (!response.error) {
            const pagingInfo = Object.assign({}, response.page_info);
            const subAccounts = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.map((dataItem) => new SubAccount(dataItem))) || [];
            onSuccess({ subAccounts, pagingInfo });
        }
        else {
            const responseError = getErrorMessage(response.error);
            const code = getErrorCode((_b = response.error) === null || _b === void 0 ? void 0 : _b.code);
            return onError({
                error: responseError,
                code,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        return onError({
            error: error.message || error,
            code,
            severity: ComponentErrorSeverity.ERROR,
        });
    }
};

export { SubAccountService as S, makeGetSubAccounts as m };
