<commit_before>(function() {
    "use strict";

    angular.module("blocktrail.wallet")
        .controller("BuyBTCBrokerCtrl", BuyBTCBrokerCtrl);

    // TODO Needs refactoring
    function BuyBTCBrokerCtrl($scope, $state, dialogService, glideraService, simplexService, activeWallet, settingsService, CONFIG,
                              $stateParams, $q, $timeout, $interval, $translate, $filter, trackingService, NotificationsService) {
        var walletData = activeWallet.getReadOnlyWalletData();

        $scope.broker = $stateParams.broker;
        $scope.brokerNotExistent = false;

        $scope.initializing = true;
        $scope.fetchingMainPrice = true;
        $scope.priceBTC = null;
        $scope.fetchingInputPrice = false;
        $scope.fiatFirst = false;
        $scope.buyInput = {
            currencyType: null,
            fiatCurrency: "USD",
            amount: null,
            btcValue: null,
            fiatValue: null,
            feeValue: null,
            feePercentage: null
        };
        $scope.currencies = [];
        $scope.altCurrency = {};

        $scope.termsAccepted = false;
        $scope.includingFee = true;

        $scope.errorMsg = null;
        var last_simplex_data = null;
        // Check if the user returned within seconds
        NotificationsService.checkAndPromptSimplex();

        var doneTypingInterval = 500;
        var typingTimer = null;

        var coinTicker = CONFIG.NETWORKS[walletData.networkType].TICKER;

        var fetchBrokerService = function() {
            switch ($scope.broker) {
                case "glidera":
                    $scope.includingFee = true;
                    trackingService.trackEvent(trackingService.EVENTS.BUYBTC.GLIDERA_OPEN);
                    $scope.buyInput.currencyType = "USD";
                    $scope.buyInput.fiatCurrency = "USD";
                    return glideraService;
                    break;
                case 'simplex':
                    $scope.includingFee = false;

                    trackingService.trackEvent(trackingService.EVENTS.BUYBTC.SIMPLEX_OPEN);
                    return simplexService;
                    break;
                default:
                    return null;
                    break;
            }
        };

        var handleErrors = function (errorMessage) {
            if (errorMessage.indexOf('value over limit') != -1) {
                $scope.errorMsg = $translate.instant("BUYBTC_HIGH_AMOUNT_ERROR", {
                    limit: 'US$' + errorMessage.replace(/[^0-9]/g, '')
                });
            } else if (errorMessage.indexOf('value under limit') != -1) {
                $scope.errorMsg = $translate.instant("BUYBTC_LOW_AMOUNT_ERROR", {
                    limit: 'US$' + errorMessage.replace(/[^0-9]/g, '')
                });
            } else {
                $scope.errorMsg = $translate.instant("BUYBTC_ERROR", {
                    network: CONFIG.NETWORKS[walletData.networkType].NETWORK_LONG
                });
            }
        };

        var updateBrokerCurrencies = function() {
            switch ($scope.broker) {
                case "glidera":
                    $scope.currencies = [{code: "USD", symbol: "USD"}];
                    return true;
                    break;
                case "simplex":
                    $scope.currencies = [
                        {code: "USD", symbol: "USD"},
                        {code: "EUR", symbol: "EUR"}
                    ];
                    return true;
                    break;
                default:
                    return false;
                    break;
            }
        };

        var updateMainPrice = function() {
            $scope.fetchingMainPrice = true;

            if (fetchBrokerService() == null) {
                $scope.brokerNotExistent = true;
                $scope.initializing = false;
                $scope.fetchingMainPrice = false;
                return null;
            }

            return fetchBrokerService().buyPrices(1, null, $scope.buyInput.fiatCurrency, false).then(function(result) {
                $scope.priceBTC = result.total;
                $scope.fetchingMainPrice = false;
            }).catch(function (e) {
                handleErrors(e.message);
            });
        };

        $scope.triggerUpdate = function() {
            clearTimeout(typingTimer);
            typingTimer = setTimeout(function() {
                $scope.updateInputPrice().catch(function() {
                    $scope.fetchingInputPrice = false;
                });
            }, doneTypingInterval);
        };

        $scope.updateInputPrice = function() {
            return $q.when().then(function() {
                $scope.fetchingInputPrice = true;
                $scope.errorMsg = null; // reset last error

                if ($scope.buyInput.currencyType === coinTicker) {
                    $scope.fiatFirst = false;
                    $scope.buyInput.btcValue = parseFloat($scope.buyInput.amount || 0) || 0;
                    $scope.buyInput.fiatValue = null;
                    $scope.buyInput.feeValue = null;
                    $scope.altCurrency = {};

                    if (!$scope.buyInput.amount || !$scope.buyInput.btcValue) {
                        return;
                    }

                    return fetchBrokerService().buyPrices($scope.buyInput.btcValue, null, $scope.buyInput.fiatCurrency, false)
                        .then(function(result) {
                            $timeout(function() {
                                $scope.buyInput.fiatValue = parseFloat(result.total);
                                if (isNaN($scope.buyInput.fiatValue)) {
                                    $scope.buyInput.fiatValue = null;
                                }
                                if (result.fees) $scope.buyInput.feeValue = parseFloat(result.fees);
                                if (result.fees) $scope.buyInput.feePercentage = ($scope.buyInput.feeValue / $scope.buyInput.fiatValue) * 100;

                                $scope.altCurrency = {
                                    code: $scope.buyInput.fiatCurrency,
                                    amount: $scope.buyInput.fiatValue
                                };

                                if ($scope.broker === 'simplex') {
                                    last_simplex_data = result;
                                }

                                $scope.fetchingInputPrice = false;

                                // simplex error handling
                                if (result["error"]) {
                                    $scope.errorMsg = result["error"];
                                }
                            });
                    });
                } else {
                    $scope.fiatFirst = true;
                    $scope.buyInput.fiatValue = parseFloat($scope.buyInput.amount || 0) || 0;
                    $scope.buyInput.btcValue = null;
                    $scope.buyInput.feeValue = null;
                    $scope.altCurrency = {};

                    if (!$scope.buyInput.amount || !$scope.buyInput.fiatValue) {
                        return;
                    }

                    return fetchBrokerService().buyPrices(null, $scope.buyInput.fiatValue, $scope.buyInput.fiatCurrency, $scope.fiatFirst)
                        .then(function(result) {
                            $timeout(function() {
                                $scope.buyInput.btcValue = parseFloat(result.qty);
                                if (isNaN($scope.buyInput.btcValue)) {
                                    $scope.buyInput.btcValue = null;
                                }
                                if (result.fees) $scope.buyInput.feeValue = parseFloat(result.fees);
                                if (result.fees) $scope.buyInput.feePercentage = ($scope.buyInput.feeValue / $scope.buyInput.fiatValue) * 100;

                                $scope.altCurrency = {
                                    code: coinTicker,
                                    amount: $scope.buyInput.btcValue
                                };

                                if ($scope.broker === 'simplex') {
                                    last_simplex_data = result;
                                }

                                $scope.fetchingInputPrice = false;

                                // simplex error handling
                                if (result["error"]) {
                                    $scope.errorMsg = result["error"];
                                }
                            });
                    });
                }// else
            }).catch(function (e) {
                handleErrors(e.message);
            });
        };

        $scope.updateCurrentType = function(currencyType) {
            updateBrokerCurrencies();
            $scope.currencies.unshift({code: coinTicker, "symbol": coinTicker});
            $scope.currencies = $scope.currencies.filter(function(currency) {
                return currency.code !== currencyType;
            });

            if (currencyType === coinTicker) {
                if ($scope.buyInput.fiatCurrency === $scope.buyInput.currencyType) {
                    $scope.buyInput.amount = $scope.buyInput.btcValue;
                } else {
                    $scope.buyInput.amount = null;
                }
            } else {
                if ($scope.buyInput.fiatCurrency === currencyType) {
                    $scope.buyInput.amount = $scope.buyInput.fiatValue;<commit_msg>don't show errors if they are json<commit_after>(function() {
    "use strict";

    angular.module("blocktrail.wallet")
        .controller("BuyBTCBrokerCtrl", BuyBTCBrokerCtrl);

    // TODO Needs refactoring
    function BuyBTCBrokerCtrl($scope, $state, dialogService, glideraService, simplexService, activeWallet, settingsService, CONFIG,
                              $stateParams, $q, $timeout, $interval, $translate, $filter, trackingService, NotificationsService) {
        var walletData = activeWallet.getReadOnlyWalletData();

        $scope.broker = $stateParams.broker;
        $scope.brokerNotExistent = false;

        $scope.initializing = true;
        $scope.fetchingMainPrice = true;
        $scope.priceBTC = null;
        $scope.fetchingInputPrice = false;
        $scope.fiatFirst = false;
        $scope.buyInput = {
            currencyType: null,
            fiatCurrency: "USD",
            amount: null,
            btcValue: null,
            fiatValue: null,
            feeValue: null,
            feePercentage: null
        };
        $scope.currencies = [];
        $scope.altCurrency = {};

        $scope.termsAccepted = false;
        $scope.includingFee = true;

        $scope.errorMsg = null;
        var last_simplex_data = null;
        // Check if the user returned within seconds
        NotificationsService.checkAndPromptSimplex();

        var doneTypingInterval = 500;
        var typingTimer = null;

        var coinTicker = CONFIG.NETWORKS[walletData.networkType].TICKER;

        var fetchBrokerService = function() {
            switch ($scope.broker) {
                case "glidera":
                    $scope.includingFee = true;
                    trackingService.trackEvent(trackingService.EVENTS.BUYBTC.GLIDERA_OPEN);
                    $scope.buyInput.currencyType = "USD";
                    $scope.buyInput.fiatCurrency = "USD";
                    return glideraService;
                    break;
                case 'simplex':
                    $scope.includingFee = false;

                    trackingService.trackEvent(trackingService.EVENTS.BUYBTC.SIMPLEX_OPEN);
                    return simplexService;
                    break;
                default:
                    return null;
                    break;
            }
        };

        var handleErrors = function (errorMessage) {
            if (errorMessage.indexOf('value over limit') != -1) {
                $scope.errorMsg = $translate.instant("BUYBTC_HIGH_AMOUNT_ERROR", {
                    limit: 'US$' + errorMessage.replace(/[^0-9]/g, '')
                });
            } else if (errorMessage.indexOf('value under limit') != -1) {
                $scope.errorMsg = $translate.instant("BUYBTC_LOW_AMOUNT_ERROR", {
                    limit: 'US$' + errorMessage.replace(/[^0-9]/g, '')
                });
            } else {
                $scope.errorMsg = $translate.instant("BUYBTC_ERROR", {
                    network: CONFIG.NETWORKS[walletData.networkType].NETWORK_LONG
                });
            }
        };

        var updateBrokerCurrencies = function() {
            switch ($scope.broker) {
                case "glidera":
                    $scope.currencies = [{code: "USD", symbol: "USD"}];
                    return true;
                    break;
                case "simplex":
                    $scope.currencies = [
                        {code: "USD", symbol: "USD"},
                        {code: "EUR", symbol: "EUR"}
                    ];
                    return true;
                    break;
                default:
                    return false;
                    break;
            }
        };

        var updateMainPrice = function() {
            $scope.fetchingMainPrice = true;

            if (fetchBrokerService() == null) {
                $scope.brokerNotExistent = true;
                $scope.initializing = false;
                $scope.fetchingMainPrice = false;
                return null;
            }

            return fetchBrokerService().buyPrices(1, null, $scope.buyInput.fiatCurrency, false).then(function(result) {
                $scope.priceBTC = result.total;
                $scope.fetchingMainPrice = false;
            }).catch(function (e) {
                handleErrors(e.message);
            });
        };

        $scope.triggerUpdate = function() {
            clearTimeout(typingTimer);
            typingTimer = setTimeout(function() {
                $scope.updateInputPrice().catch(function() {
                    $scope.fetchingInputPrice = false;
                });
            }, doneTypingInterval);
        };

        $scope.updateInputPrice = function() {
            return $q.when().then(function() {
                $scope.fetchingInputPrice = true;
                $scope.errorMsg = null; // reset last error

                if ($scope.buyInput.currencyType === coinTicker) {
                    $scope.fiatFirst = false;
                    $scope.buyInput.btcValue = parseFloat($scope.buyInput.amount || 0) || 0;
                    $scope.buyInput.fiatValue = null;
                    $scope.buyInput.feeValue = null;
                    $scope.altCurrency = {};

                    if (!$scope.buyInput.amount || !$scope.buyInput.btcValue) {
                        return;
                    }

                    return fetchBrokerService().buyPrices($scope.buyInput.btcValue, null, $scope.buyInput.fiatCurrency, false)
                        .then(function(result) {
                            $timeout(function() {
                                $scope.buyInput.fiatValue = parseFloat(result.total);
                                if (isNaN($scope.buyInput.fiatValue)) {
                                    $scope.buyInput.fiatValue = null;
                                }
                                if (result.fees) $scope.buyInput.feeValue = parseFloat(result.fees);
                                if (result.fees) $scope.buyInput.feePercentage = ($scope.buyInput.feeValue / $scope.buyInput.fiatValue) * 100;

                                $scope.altCurrency = {
                                    code: $scope.buyInput.fiatCurrency,
                                    amount: $scope.buyInput.fiatValue
                                };

                                if ($scope.broker === 'simplex') {
                                    last_simplex_data = result;
                                }

                                $scope.fetchingInputPrice = false;

                                // simplex error handling
                                if (result["error"]) {
                                    // if it is not JSON
                                    try { JSON.parse(data); }
                                    catch(e) {
                                        $scope.errorMsg = result["error"];
                                    }
                                }
                            });
                    });
                } else {
                    $scope.fiatFirst = true;
                    $scope.buyInput.fiatValue = parseFloat($scope.buyInput.amount || 0) || 0;
                    $scope.buyInput.btcValue = null;
                    $scope.buyInput.feeValue = null;
                    $scope.altCurrency = {};

                    if (!$scope.buyInput.amount || !$scope.buyInput.fiatValue) {
                        return;
                    }

                    return fetchBrokerService().buyPrices(null, $scope.buyInput.fiatValue, $scope.buyInput.fiatCurrency, $scope.fiatFirst)
                        .then(function(result) {
                            $timeout(function() {
                                $scope.buyInput.btcValue = parseFloat(result.qty);
                                if (isNaN($scope.buyInput.btcValue)) {
                                    $scope.buyInput.btcValue = null;
                                }
                                if (result.fees) $scope.buyInput.feeValue = parseFloat(result.fees);
                                if (result.fees) $scope.buyInput.feePercentage = ($scope.buyInput.feeValue / $scope.buyInput.fiatValue) * 100;

                                $scope.altCurrency = {
                                    code: coinTicker,
                                    amount: $scope.buyInput.btcValue
                                };

                                if ($scope.broker === 'simplex') {
                                    last_simplex_data = result;
                                }

                                $scope.fetchingInputPrice = false;

                                // simplex error handling
                                if (result["error"]) {
                                    // if it is not JSON
                                    try { JSON.parse(data); }
                                    catch(e) {
                                        $scope.errorMsg = result["error"];
                                    }
                                }
                            });
                    });
                }// else
            }).catch(function (e) {
                handleErrors(e.message);
            });
        };

        $scope.updateCurrentType = function(currencyType) {
            updateBrokerCurrencies();
            $scope.currencies.unshift({code: coinTicker, "symbol": coinTicker});
            $scope.currencies = $scope.currencies.filter(function(currency) {
                return currency.code !== currencyType;
            });

            if (currencyType === coinTicker) {
                if ($scope.buyInput.fiatCurrency === $scope.buyInput.currencyType) {
                    $scope.buyInput.amount = $scope.buyInput.btcValue;
                } else {
                    $scope.buyInput.amount = null;
                }
            } else {
                if ($scope.buyInput.fiatCurrency === currencyType) {
                    $scope.buyInput.amount = $scope.buyInput.fiatValue;
{'fraction_non_alphanumeric': 0.10255760952139782, 'fraction_numerical': 0.002025829323879463, 'mean_word_length': 1.1516835567178816, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4443007', 'n_tokens_mistral': 5143, 'n_tokens_neox': 4694, 'n_words': 775}