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from '@metamask/superstruct';\n\nimport type { AccountGroupMultichainAccountObject } from '../../group';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport type {\n  BackupAndSyncContext,\n  LegacyUserStorageSyncedAccount,\n  UserStorageSyncedWallet,\n  UserStorageSyncedWalletGroup,\n} from '../types';\nimport { UserStorageSyncedWalletGroupSchema } from '../types';\nimport { toErrorMessage } from '../utils/errors';\nimport {\n  assertValidUserStorageWallet,\n  assertValidUserStorageGroup,\n  assertValidLegacyUserStorageAccount,\n} from './validation';\n\n/**\n * Formats the wallet for user storage usage.\n * This function extracts the necessary metadata from the wallet\n * and formats it according to the user storage requirements.\n *\n * @param context - The backup and sync context.\n * @param wallet - The wallet object to format.\n * @returns The formatted wallet for user storage.\n */\nexport const formatWalletForUserStorageUsage = (\n  context: BackupAndSyncContext,\n  wallet: AccountWalletEntropyObject,\n): UserStorageSyncedWallet => {\n  // This can be null if the user has not manually set a name\n  const persistedWalletMetadata =\n    context.controller.state.accountWalletsMetadata[wallet.id];\n\n  return {\n    ...(persistedWalletMetadata ?? {}),\n    isLegacyAccountSyncingDisabled: true, // If we're here, it means legacy account syncing has been performed at least once, so we can disable it for this wallet.\n  };\n};\n\n/**\n * Formats the group for user storage usage.\n * This function extracts the necessary metadata from the group\n * and formats it according to the user storage requirements.\n *\n * @param context - The backup and sync context.\n * @param group - The group object to format.\n * @returns The formatted group for user storage.\n */\nexport const formatGroupForUserStorageUsage = (\n  context: BackupAndSyncContext,\n  group: AccountGroupMultichainAccountObject,\n): UserStorageSyncedWalletGroup => {\n  // This can be null if the user has not manually set a name, pinned or hidden the group\n  const persistedGroupMetadata =\n    context.controller.state.accountGroupsMetadata[group.id];\n  const { groupIndex } = group.metadata.entropy;\n\n  try {\n    // We mask and we try catch, since `mask` will throw if the persisted metadata has\n    // fields with wrong types.\n    return mask(\n      {\n        ...(persistedGroupMetadata ?? {}),\n        groupIndex,\n      },\n      UserStorageSyncedWalletGroupSchema,\n    );\n  } catch (error) {\n    backupAndSyncLogger(\n      `Error trying to format group for user storage usage: ${toErrorMessage(error)}`,\n    );\n\n    // If anything goes wrong with this group, we use blank metadata for it.\n    return { groupIndex };\n  }\n};\n\n/**\n * Parses the wallet from user storage response.\n * This function attempts to parse the wallet data from a string format\n * and returns it as a UserStorageSyncedWallet object.\n *\n * @param wallet - The wallet data in string format.\n * @returns The parsed UserStorageSyncedWallet object.\n * @throws If the wallet data is not in valid JSON format or fails validation.\n */\nexport const parseWalletFromUserStorageResponse = (\n  wallet: string,\n): UserStorageSyncedWallet => {\n  try {\n    const walletData = JSON.parse(wallet);\n    assertValidUserStorageWallet(walletData);\n    return walletData;\n  } catch (error: unknown) {\n    throw new Error(\n      `Error trying to parse wallet from user storage response: ${toErrorMessage(error)}`,\n    );\n  }\n};\n\n/**\n * Parses the group from user storage response.\n * This function attempts to parse the group data from a string format\n * and returns it as a UserStorageSyncedWalletGroup object.\n *\n * @param group - The group data in string format.\n * @returns The parsed UserStorageSyncedWalletGroup object.\n * @throws If the group data is not in valid JSON format or fails validation.\n */\nexport const parseGroupFromUserStorageResponse = (\n  group: string,\n): UserStorageSyncedWalletGroup => {\n  try {\n    const groupData = JSON.parse(group);\n    assertValidUserStorageGroup(groupData);\n    return groupData;\n  } catch (error: unknown) {\n    throw new Error(\n      `Error trying to parse group from user storage response: ${toErrorMessage(error)}`,\n    );\n  }\n};\n\n/**\n * Parses the legacy account from user storage response.\n * This function attempts to parse the account data from a string format\n * and returns it as a LegacyUserStorageSyncedAccount object.\n *\n * @param account - The account data in string format.\n * @returns The parsed LegacyUserStorageSyncedAccount object.\n * @throws If the account data is not in valid JSON format or fails validation.\n */\nexport const parseLegacyAccountFromUserStorageResponse = (\n  account: string,\n): LegacyUserStorageSyncedAccount => {\n  try {\n    const accountData = JSON.parse(account);\n    assertValidLegacyUserStorageAccount(accountData);\n    return accountData;\n  } catch (error: unknown) {\n    throw new Error(\n      `Error trying to parse legacy account from user storage response: ${toErrorMessage(error)}`,\n    );\n  }\n};\n"]}