import { $TSFixMe, User } from '../../type';

export const getSourceAppPid = (user: User) => user?.sourceAppPid;

export const getCurrentAppPid = (user: User) => user?.sourceAppPid || window?.blocklet?.appPid;

export function getPermanentDid(user: User) {
  return user?.did;
}

export function getConnectedAccounts(user: User) {
  return user?.connectedAccounts || user?.extraConfigs?.connectedAccounts || [];
}

export function getSourceProvider(user: User) {
  return user?.sourceProvider || user?.extraConfigs?.sourceProvider || 'wallet';
}

export function getSourceProviders(user: User) {
  const connectedAccounts = getConnectedAccounts(user);
  return connectedAccounts.map((item: $TSFixMe) => item.provider);
}

export function getConnectedDids(user: User) {
  const connectedAccounts = getConnectedAccounts(user);
  const didList = connectedAccounts.map((item: $TSFixMe) => item.did);
  return didList;
}

export function getWallet(user: User) {
  const connectedAccounts = getConnectedAccounts(user);
  const walletAccount = connectedAccounts.find((item: $TSFixMe) => item.provider === 'wallet');
  return walletAccount;
}

export function getWalletDid(user: User) {
  const walletAccount = getWallet(user);
  return walletAccount?.did;
}
