import { PublicKey, PublicKeyInitData } from '@solarti/web3.js';

export { PublicKey } from '@solarti/web3.js';
export type PublicKeyString = string;
export type PublicKeyValues =
  | PublicKeyInitData
  | { publicKey: PublicKey }
  | { address: PublicKey };

export const toPublicKey = (value: PublicKeyValues): PublicKey => {
  // m17, vanilla,  compile error due to maybe compiler bug
  // if (typeof value === 'object' && 'publicKey' in value) {
  //   return value.publicKey;
  // } 

  // if (typeof value === 'object' && 'address' in value) {
  //   return value.address;
  // }

  if (typeof value === 'object') {
    if ('publicKey' in value) {
      return value.publicKey;
    }

    if ('address' in value) {
      return value.address;
    }
  } 

  return new PublicKey(value);
};
