(function (global, factory) {
    if (typeof define === "function" && define.amd) {
        define(['exports', 'axios', './types', '../assets/sounds/google_now_voice.mp3'], factory);
    } else if (typeof exports !== "undefined") {
        factory(exports, require('axios'), require('./types'), require('../assets/sounds/google_now_voice.mp3'));
    } else {
        var mod = {
            exports: {}
        };
        factory(mod.exports, global.axios, global.types, global.google_now_voice);
        global.index = mod.exports;
    }
})(this, function (exports, _axios, _types, _google_now_voice) {
    'use strict';

    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    exports.stopListening = exports.navigate = exports.listen = undefined;

    var _axios2 = _interopRequireDefault(_axios);

    var _google_now_voice2 = _interopRequireDefault(_google_now_voice);

    function _interopRequireDefault(obj) {
        return obj && obj.__esModule ? obj : {
            default: obj
        };
    }

    var API_ENDPOINT = 'http://raaapi.luisdev.eu/api';
    var SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    var recognition = new SpeechRecognition();
    recognition.lang = 'en-EN';
    recognition.interimResults = false;
    recognition.maxAlternatives = 1;

    var handleSuccessSpeechRequest = function handleSuccessSpeechRequest(dispatch, _ref) {
        var target = _ref.data.target;

        dispatch({
            type: _types.REQUESTED_TARGET,
            payload: target
        });
    };

    var handleErrorSpeechRequest = function handleErrorSpeechRequest(dispatch, _ref2) {
        var error = _ref2.response.data.error;

        switch (error) {
            case _types.SAY_TARGET:
                return dispatch({ type: _types.SAY_TARGET });
            case _types.REPEAT_COMMAND:
                return dispatch({ type: _types.REPEAT_COMMAND });
            default:
                console.log('request failed');
        }
        return 0;
    };

    var listen = exports.listen = function listen() {
        return function (dispatch) {
            dispatch({ type: _types.LISTENING });
            new Audio(_google_now_voice2.default).play();
            recognition.start();
            recognition.onresult = function (_ref3) {
                var results = _ref3.results;

                var last = results.length - 1;
                var text = results[last][0].transcript;
                _axios2.default.post(API_ENDPOINT + '/request', { text: text }).then(function (response) {
                    return handleSuccessSpeechRequest(dispatch, response);
                }).catch(function (response) {
                    return handleErrorSpeechRequest(dispatch, response);
                });
            };
            recognition.onspeechend = function () {
                recognition.stop();
                dispatch({ type: _types.STOP_LISTENING });
            };
        };
    };

    var navigate = exports.navigate = function navigate(nextTarget, callback) {
        return function (dispatch) {
            callback();
            dispatch({
                type: _types.NAVIGATION_DONE,
                payload: nextTarget
            });
        };
    };

    var stopListening = exports.stopListening = function stopListening() {
        recognition.stop();
        return { type: _types.STOP_LISTENING };
    };
});