<commit_before>
import StellarSdk from 'stellar-sdk';
import directory from 'stellarterm-directory';
import horizon from '../../core/services/horizon.js';

function createAsset(json, prefix) {
	if (!prefix) {
		prefix = '';
	}

	if (json[`${prefix}asset_type`] === 'native') {
		return StellarSdk.Asset.native();
	} else {
		return new StellarSdk.Asset(
			json[`${prefix}asset_code`],
			json[`${prefix}asset_issuer`]
		);
	}
}

export default class SendController {

	/* @ngInject */
	constructor($location, Reviewer, Wallet) {
		this.$location = $location;
		this.Reviewer = Reviewer;
		this.Wallet = Wallet;
	}

	$onInit() {
		this.advanced = false;
		this.destinationAssets = [];
		this.send = {};
		this.forms = {};
		this.flags = {};

		this.assetCodeCollisions = null;
		this.hasPath			= false;
		this.isPathPending		= true;
		this.isPreFilled		= false;

		this.minimumAccountBalance = horizon.getMinimumAccountBalance(this.Wallet.current.network);
		this.state = 1;

		const query = this.$location.search();
		if (Object.keys(query).length !== 0) {
			this.send.destination = query.destination;
			this.send.amount		= query.amount;
			this.send.asset 		= query;
			this.destinationAssets.push(query);

			if (query.memo) {
				/* eslint-disable camelcase */
				this.send.memo_type = query.memo.type;
				this.send.memo      = query.memo.value;
				/* eslint-enable camelcase */
			}

			this.updateCollisions(this.destinationAssets.concat(this.Wallet.current.balances));

			this.isPreFilled = true;

			this.send.destInfo = {
				id: query.destination
			};

			this.getPaths();
		}
	}

	isEmail(address) {
		/* eslint-disable no-useless-escape */
		return /^[\w\.\+]+@([\w]+\.)+[\w]{2,}$/.test(address);
		/* eslint-enable no-useless-escape */
	}

	getAssetDescription(asset) {
		if (asset.asset_type !== 'native') {
			if (asset.asset_code in this.assetCodeCollisions) {
				return `${asset.asset_code}.${asset.asset_issuer}`;
			} else {
				return asset.asset_code;
			}
		} else {
			return 'XLM';
		}
	}

	getSourceAssetDescription(path) {
		/* eslint-disable camelcase */
		return this.getAssetDescription({
			asset_type:		path.source_asset_type,
			asset_code:		path.source_asset_code,
			asset_issuer:	path.source_asset_issuer
		});
		/* eslint-enable camelcase */
	}

	onAmount() {
		this.getPaths();
	}

	onAsset() {
		this.getPaths();
	}

	onDestInfo(destInfo) {

		if (this.isPreFilled) {
			return;
		}

		if (!destInfo) {
			this.requiresMemo = false;
			this.hasPath = false;
			this.isPathPending = true;
			delete this.send.amount;
			return;
		}

		/* eslint-disable camelcase */
		if (destInfo.memo_type) {
			this.send.memo_type	= destInfo.memo_type;
			this.send.memo		= destInfo.memo;
		} else {
			this.send.memo_type	= null;
			this.send.memo		= null;
		}
		/* eslint-enable camelcase */

		const directoryItem = directory.getDestination(destInfo.id);
		if (directoryItem) {
			if ('requiredMemoType' in directoryItem) {
				const table = {
					'MEMO_TEXT': 'text',
					'MEMO_ID':	'id'
				};

				/* eslint-disable camelcase */
				this.send.memo_type = table[directoryItem.requiredMemoType];
				this.requiresMemo = true;
				/* eslint-enable camelcase */
			}
		} else {
			this.requiresMemo = false;
		}

		const currentAccount = this.Wallet.current;

		currentAccount.horizon()
		.accounts()
		.accountId(destInfo.id)
		.call()

		//	destInfo.id is a registered account

		.then(res => {
			const assetSortFunction = function (foo, bar) {
				return foo.asset_code > bar.asset_code;
			};

			this.updateCollisions(res.balances.concat(this.Wallet.current.balances));

			//	append any issuing assets we hold in the wallet
			const issuing = currentAccount.getAssetsFromIssuer(destInfo.id);

			const assets = res.balances.concat(issuing);
			const native = assets.filter(item => item.asset_type === 'native');
			const creditAlphanum4 = assets.filter(item => item.asset_type === 'credit_alphanum4');
			const creditAlphanum12 = assets.filter(item => item.asset_type === 'credit_alphanum12');

			creditAlphanum4.sort(assetSortFunction);
			creditAlphanum12.sort(assetSortFunction);

			/* eslint-disable camelcase */
			native[0].asset_code = 'XLM';
			/* eslint-enable camelcase */
			this.destinationAssets = native.concat(creditAlphanum4, creditAlphanum12);
			this.send.asset = this.destinationAssets[0];
			this.flags.isUnregistered = false;
		})

		//	destInfo.id is not (currently) a registered account

		.catch(() => {

			/* eslint-disable camelcase */
			const assets = [{
				asset_type: 'native',
				asset_code: 'XLM'
			}];
			/* eslint-enable camelcase */

			const issuing = currentAccount.getAssetsFromIssuer(destInfo.id);
			this.destinationAssets = assets.concat(issuing);

			this.updateCollisions(this.Wallet.current.balances);

			this.send.asset = this.destinationAssets[0];
			this.flags.isUnregistered = true;
		});
	}

	onValidAddress(res) {
		this.send.destInfo = res;
		this.onDestInfo(res);<commit_msg>Refactor to use sortAssets<commit_after>
import StellarSdk from 'stellar-sdk';
import directory from 'stellarterm-directory';
import horizon from '../../core/services/horizon.js';
import {sortAssets} from '../../core/services/account';

function createAsset(json, prefix) {
	if (!prefix) {
		prefix = '';
	}

	if (json[`${prefix}asset_type`] === 'native') {
		return StellarSdk.Asset.native();
	} else {
		return new StellarSdk.Asset(
			json[`${prefix}asset_code`],
			json[`${prefix}asset_issuer`]
		);
	}
}

export default class SendController {

	/* @ngInject */
	constructor($location, Reviewer, Wallet) {
		this.$location = $location;
		this.Reviewer = Reviewer;
		this.Wallet = Wallet;
	}

	$onInit() {
		this.advanced = false;
		this.destinationAssets = [];
		this.send = {};
		this.forms = {};
		this.flags = {};

		this.assetCodeCollisions = null;
		this.hasPath			= false;
		this.isPathPending		= true;
		this.isPreFilled		= false;

		this.minimumAccountBalance = horizon.getMinimumAccountBalance(this.Wallet.current.network);
		this.state = 1;

		const query = this.$location.search();
		if (Object.keys(query).length !== 0) {
			this.send.destination = query.destination;
			this.send.amount		= query.amount;
			this.send.asset 		= query;
			this.destinationAssets.push(query);

			if (query.memo) {
				/* eslint-disable camelcase */
				this.send.memo_type = query.memo.type;
				this.send.memo      = query.memo.value;
				/* eslint-enable camelcase */
			}

			this.updateCollisions(this.destinationAssets.concat(this.Wallet.current.balances));

			this.isPreFilled = true;

			this.send.destInfo = {
				id: query.destination
			};

			this.getPaths();
		}
	}

	isEmail(address) {
		/* eslint-disable no-useless-escape */
		return /^[\w\.\+]+@([\w]+\.)+[\w]{2,}$/.test(address);
		/* eslint-enable no-useless-escape */
	}

	getAssetDescription(asset) {
		if (asset.asset_type !== 'native') {
			if (asset.asset_code in this.assetCodeCollisions) {
				return `${asset.asset_code}.${asset.asset_issuer}`;
			} else {
				return asset.asset_code;
			}
		} else {
			return 'XLM';
		}
	}

	getSourceAssetDescription(path) {
		/* eslint-disable camelcase */
		return this.getAssetDescription({
			asset_type:		path.source_asset_type,
			asset_code:		path.source_asset_code,
			asset_issuer:	path.source_asset_issuer
		});
		/* eslint-enable camelcase */
	}

	onAmount() {
		this.getPaths();
	}

	onAsset() {
		this.getPaths();
	}

	onDestInfo(destInfo) {

		if (this.isPreFilled) {
			return;
		}

		if (!destInfo) {
			this.requiresMemo = false;
			this.hasPath = false;
			this.isPathPending = true;
			delete this.send.amount;
			return;
		}

		/* eslint-disable camelcase */
		if (destInfo.memo_type) {
			this.send.memo_type	= destInfo.memo_type;
			this.send.memo		= destInfo.memo;
		} else {
			this.send.memo_type	= null;
			this.send.memo		= null;
		}
		/* eslint-enable camelcase */

		const directoryItem = directory.getDestination(destInfo.id);
		if (directoryItem) {
			if ('requiredMemoType' in directoryItem) {
				const table = {
					'MEMO_TEXT': 'text',
					'MEMO_ID':	'id'
				};

				/* eslint-disable camelcase */
				this.send.memo_type = table[directoryItem.requiredMemoType];
				this.requiresMemo = true;
				/* eslint-enable camelcase */
			}
		} else {
			this.requiresMemo = false;
		}

		const currentAccount = this.Wallet.current;

		currentAccount.horizon()
		.accounts()
		.accountId(destInfo.id)
		.call()

		//	destInfo.id is a registered account

		.then(res => {
			this.updateCollisions(res.balances.concat(this.Wallet.current.balances));

			//	append any issuing assets we hold in the wallet
			const issuing = currentAccount.getAssetsFromIssuer(destInfo.id);
			this.destinationAssets = sortAssets(res.balances.concat(issuing));
			this.send.asset = this.destinationAssets[0];
			this.flags.isUnregistered = false;
		})

		//	destInfo.id is not (currently) a registered account

		.catch(() => {

			/* eslint-disable camelcase */
			const assets = [{
				asset_type: 'native',
				asset_code: 'XLM'
			}];
			/* eslint-enable camelcase */

			const issuing = currentAccount.getAssetsFromIssuer(destInfo.id);
			this.destinationAssets = assets.concat(issuing);

			this.updateCollisions(this.Wallet.current.balances);

			this.send.asset = this.destinationAssets[0];
			this.flags.isUnregistered = true;
		});
	}

	onValidAddress(res) {
		this.send.destInfo = res;
		this.onDestInfo(res);
{'fraction_non_alphanumeric': 0.14652792524952218, 'fraction_numerical': 0.0024421320874920367, 'mean_word_length': 3.3707656612529004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2208431', 'n_tokens_mistral': 3849, 'n_tokens_neox': 3291, 'n_words': 547}