"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const db_1=require("@b/db"),query_1=require("@b/utils/query"),utils_1=require("@b/api/admin/finance/wallet/utils"),error_1=require("@b/utils/error"),cache_1=require("@b/utils/cache");exports.metadata={summary:"Lists all wallets for a given type",operationId:"listWalletsForType",tags:["Finance","Wallets"],parameters:[{index:0,name:"type",in:"path",description:"Wallet type",required:!0,schema:{type:"string",enum:["FIAT","SPOT","ECO","FUTURES"]}}],responses:{200:{description:"List of wallets",content:{"application/json":{schema:{type:"array",items:{type:"object",properties:utils_1.walletSchema}}}}},401:query_1.unauthorizedResponse,404:(0,query_1.notFoundMetadataResponse)("Wallets"),500:query_1.serverErrorResponse},requiresAuth:!0};exports.default=async e=>{const{params:t,user:r,ctx:a}=e;if(!(null==r?void 0:r.id))throw(0,error_1.createError)({statusCode:401,message:"Unauthorized"});const s=t.type;null==a||a.step(`Fetching ${s} wallets`);if("SPOT"===s){const e=cache_1.CacheManager.getInstance(),t=await e.getSetting("spotWallets");if(!(!0===t||"true"===t))return[]}const n={userId:r.id,type:s},l=await db_1.models.wallet.findAll({where:n,paranoid:!1}),i=l.filter(e=>"ECO"===e.type);if(i.length>0){const e=Array.from(new Set(i.map(e=>e.currency))),t=await db_1.models.ecosystemToken.findAll({where:{currency:e}}),r=new Map(t.map(e=>[e.currency,e.icon]));i.forEach(e=>{e.icon=r.get(e.currency)||null})}const o=l.map(e=>({...e.toJSON(),availableBalance:parseFloat(e.balance||0)-parseFloat(e.inOrder||0)}));null==a||a.success(`Retrieved ${o.length} ${s} wallets`);return o};