'use strict';

var Api = require('./Api-D9vOcUw4.js');
var Plaid = require('./Plaid-CPESyiiV.js');
require('./Pagination-Dediz133.js');
require('./Business-oBifyXkW.js');
require('./config-state-BIgGhhe9.js');
require('./dinero-BwNN_k7x.js');
var utils = require('./utils-BsnepLy6.js');

const api = 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 Plaid.SubAccount(dataItem))) || [];
            onSuccess({ subAccounts, pagingInfo });
        }
        else {
            const responseError = utils.getErrorMessage(response.error);
            const code = utils.getErrorCode((_b = response.error) === null || _b === void 0 ? void 0 : _b.code);
            return onError({
                error: responseError,
                code,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = utils.getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        return onError({
            error: error.message || error,
            code,
            severity: Plaid.ComponentErrorSeverity.ERROR,
        });
    }
};

exports.SubAccountService = SubAccountService;
exports.makeGetSubAccounts = makeGetSubAccounts;
