// TODO: Move to joy-utils?
import { AccountId } from '@polkadot/types/interfaces';

// Keyring / identicon / address
// -----------------------------------

import { createOptionItem } from '@polkadot/ui-keyring/options/item';
import { findNameByAddress } from '@polkadot/joy-utils/functions/misc';

// Hash
// -----------------------------------

import { decodeAddress } from '@polkadot/keyring';
import { stringToU8a } from '@polkadot/util';
import { blake2AsHex } from '@polkadot/util-crypto';

export type HashedVote = {
  applicantId: string;
  salt: string;
  hash: string;
};

const createAddressOption = (address: string) => {
  const name = findNameByAddress(address);

  return createOptionItem(address, name);
};

export const accountIdsToOptions = (applicants: Array<AccountId>) => {
  if (applicants && applicants.length) {
    return applicants.map((a) => {
      const addr = a.toString();

      return createAddressOption(addr);
    });
  }

  return [];
};

/** hash(accountId + salt) */
export const hashVote = (accountId?: string | null, salt?: string): string | null => {
  if (!accountId || !salt) {
    // console.log('Cannot hash a vote: either accountId or salt is undefined', { accountId, salt });
    return null;
  }

  const accountU8a = decodeAddress(accountId);
  const saltU8a = stringToU8a(salt);
  const voteU8a = new Uint8Array(accountU8a.length + saltU8a.length);

  voteU8a.set(accountU8a);
  voteU8a.set(saltU8a, accountU8a.length);

  const hash = blake2AsHex(voteU8a, 256);

  // console.log('Vote hash:', hash, 'for', { accountId, salt });
  return hash;
};
