{
  "version": 3,
  "sources": ["../../src/providers/strategies/LedgerProviderStrategy/helpers/authenticateLedgerAccount/helpers/updateAccountsList/updateAccountsList.ts"],
  "sourcesContent": ["import BigNumber from 'bignumber.js';\n\nimport { getEconomics } from 'apiCalls/economics/getEconomics';\nimport { Address, AddressComputer } from 'lib/sdkCore';\nimport { formatAmount } from 'lib/sdkDappUtils';\nimport { getNetworkConfig } from 'methods/network/getNetworkConfig';\nimport { ProviderErrorsEnum } from 'types/provider.types';\nimport { fetchAccount } from 'utils/account/fetchAccount';\nimport { getUsdValue } from 'utils/operations/getUsdValue';\n\nimport {\n  IUpdateAccountsList,\n  UpdateAccountObjectType\n} from './updateAccountsList.types';\nimport { ILedgerAccount } from '../../../../types/ledger.types';\n\n/**\n * Updates the list of accounts and fetches their balances.\n *\n * This function performs the following steps:\n * 1. Checks if the manager and provider are initialized; if not, throws an error.\n * 2. Retrieves the starting index for pagination and the current list of accounts.\n * 3. Checks if there is already data for the current page to avoid unnecessary fetching.\n * 4. If no data is present, it fetches the accounts from the wallet provider.\n * 5. Updates the state manager with the new account data and their balances.\n * 6. Handles errors by reverting to existing accounts and logging the error.\n */\nexport const updateAccountsList = async ({\n  manager,\n  provider\n}: IUpdateAccountsList) => {\n  const addressComputer = new AddressComputer();\n  if (!manager || !provider) {\n    throw new Error(ProviderErrorsEnum.notInitialized);\n  }\n\n  const { network } = getNetworkConfig();\n  const startIndex = manager.getAccountScreenData()?.startIndex || 0;\n  const allAccounts = manager.getAllAccounts();\n  const economics = await getEconomics({ baseURL: network.apiAddress });\n\n  const filterByStartIndexRange = (account: ILedgerAccount) =>\n    account.index >= startIndex &&\n    account.index < startIndex + manager.addressesPerPage;\n\n  const allAccountsObject = allAccounts.reduce(\n    (accountsObject: UpdateAccountObjectType, account) =>\n      Object.assign(accountsObject, { [account.index]: account }),\n    {}\n  );\n\n  const currentAccounts = allAccounts.filter(filterByStartIndexRange);\n  const isStartIndexInAccounts = Boolean(allAccountsObject[startIndex]);\n\n  manager.updateAccountScreen({\n    isLoading: !isStartIndexInAccounts\n  });\n\n  if (currentAccounts.length > 0) {\n    manager.updateAccountScreen({ accounts: currentAccounts });\n    return;\n  }\n\n  try {\n    const page = Math.floor(startIndex / manager.addressesPerPage);\n\n    const accountsArray = await provider.getAccounts(\n      page,\n      manager.addressesPerPage\n    );\n\n    const accountsWithBalance = accountsArray.map(\n      (address, accountIindex): ILedgerAccount => {\n        const shard = addressComputer.getShardOfAddress(\n          Address.newFromBech32(address)\n        );\n\n        return {\n          address,\n          balance: '...',\n          index: accountIindex + startIndex,\n          shard\n        };\n      }\n    );\n\n    const newAllAccounts = [...allAccounts, ...accountsWithBalance];\n    const balancePromises = accountsArray.map((address) =>\n      fetchAccount({ address, baseURL: network.apiAddress })\n    );\n\n    const balances = await Promise.all(balancePromises);\n    const newAllAccountsObject = newAllAccounts.reduce(\n      (accountsObject: UpdateAccountObjectType, account) =>\n        Object.assign(accountsObject, { [account.address]: account }),\n      {}\n    );\n\n    balances.forEach((account) => {\n      if (!account || !economics || !economics.price) {\n        return;\n      }\n\n      const balanceBigNumber = new BigNumber(account.balance);\n      const formattedBalance = balanceBigNumber\n        .dividedBy(BigNumber(10).pow(18))\n        .toFormat(4)\n        .toString();\n\n      if (balanceBigNumber.isNaN()) {\n        return;\n      }\n\n      const usdValue = getUsdValue({\n        amount: formatAmount({ input: account.balance }),\n        usd: economics?.price\n      });\n\n      newAllAccountsObject[account.address].balance = formattedBalance;\n      newAllAccountsObject[account.address].usdValue = usdValue;\n    });\n\n    const newAllAccountsArray = Object.values(newAllAccountsObject).sort(\n      (alpha, beta) => alpha.index - beta.index\n    );\n\n    const accountsScreenArray = newAllAccountsArray.filter(\n      filterByStartIndexRange\n    );\n\n    manager.updateAllAccounts(newAllAccountsArray);\n    manager.updateAccountScreen({\n      accounts: accountsScreenArray,\n      isLoading: false\n    });\n  } catch (error) {\n    console.error('Failed to fetch accounts:', error);\n  }\n};\n"],
  "mappings": "6PAAA,OAAOA,MAAe,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B,EAAW,MAAM,QAAQ,IAAIF,CAAe,EAC5CG,EAAuBJ,EAAe,OAC1C,CAACV,EAAyCF,IACxC,OAAO,OAAOE,EAAgB,CAAE,CAACF,EAAQ,OAAO,EAAGA,CAAQ,CAAC,EAC9D,CAAC,CACH,EAEAe,EAAS,QAASf,GAAY,CAC5B,GAAI,CAACA,GAAW,CAACH,GAAa,CAACA,EAAU,MACvC,OAGF,IAAMoB,EAAmB,IAAIC,EAAUlB,EAAQ,OAAO,EAChDmB,EAAmBF,EACtB,UAAUC,EAAU,EAAE,EAAE,IAAI,EAAE,CAAC,EAC/B,SAAS,CAAC,EACV,SAAS,EAEZ,GAAID,EAAiB,MAAM,EACzB,OAGF,IAAMG,EAAWC,EAAY,CAC3B,OAAQC,EAAa,CAAE,MAAOtB,EAAQ,OAAQ,CAAC,EAC/C,IAAKH,GAAW,KAClB,CAAC,EAEDmB,EAAqBhB,EAAQ,OAAO,EAAE,QAAUmB,EAChDH,EAAqBhB,EAAQ,OAAO,EAAE,SAAWoB,CACnD,CAAC,EAED,IAAMG,EAAsB,OAAO,OAAOP,CAAoB,EAAE,KAC9D,CAACQ,EAAOC,IAASD,EAAM,MAAQC,EAAK,KACtC,EAEMC,EAAsBH,EAAoB,OAC9CxB,CACF,EAEAV,EAAQ,kBAAkBkC,CAAmB,EAC7ClC,EAAQ,oBAAoB,CAC1B,SAAUqC,EACV,UAAW,EACb,CAAC,CACH,OAASC,EAAO,CACd,QAAQ,MAAM,4BAA6BA,CAAK,CAClD,CACF",
  "names": ["BigNumber", "updateAccountsList", "manager", "provider", "addressComputer", "AddressComputer", "network", "getNetworkConfig", "startIndex", "allAccounts", "economics", "getEconomics", "filterByStartIndexRange", "account", "allAccountsObject", "accountsObject", "currentAccounts", "isStartIndexInAccounts", "page", "accountsArray", "accountsWithBalance", "address", "accountIindex", "shard", "Address", "newAllAccounts", "balancePromises", "fetchAccount", "balances", "newAllAccountsObject", "balanceBigNumber", "BigNumber", "formattedBalance", "usdValue", "getUsdValue", "formatAmount", "newAllAccountsArray", "alpha", "beta", "accountsScreenArray", "error"]
}
