<commit_before>/*global getParameterByName, getSearchResults, getAutocompleteSuggestions, parseYoutubeVideoID, getYouTubeVideoDescription*/

// Pointer to Keen client
var client;

function anonymizeFileUrl() {
    var url = window.location.href;
    if (url.indexOf("file://") === 0) {
        url = "localhost";
    }
    return url;
}

function sendKeenEvent(_msg, _data) {
    var d = {
        page_url: anonymizeFileUrl(), //eslint-disable-line camelcase
        user_agent: "${keen.user_agent}", //eslint-disable-line camelcase
        ip_address: "${keen.ip}", //eslint-disable-line camelcase
        keen: {
            addons: [
                {
                    name: "keen:ip_to_geo",
                    input: {
                        ip: "ip_address"
                    },
                    output: "ip_geo_info"
                },
                {
                    name: "keen:ua_parser",
                    input: {
                        ua_string: "user_agent" //eslint-disable-line camelcase
                    },
                    output: "parsed_user_agent"
                },
                {
                    name: "keen:url_parser",
                    input: {
                        url: "page_url"
                    },
                    output: "parsed_page_url"
                }
            ]
        }
    };

    for (var _d in _data) {
        d[_d] = _data[_d];
    }
    client.addEvent(_msg, d);
}

/**
 * YouTube iframe API required setup
 */
// var player;
var plyrPlayer;
var youTubeDataApiKey = "<KEY>";
var currentVideoID;

// function onYouTubeIframeAPIReady() { //eslint-disable-line no-unused-vars
//     player = new YT.Player("player", {
//         height: "300",
//         width: "400",
//         // Parse the querystring and populate the video when loading the page
//         videoId: getCurrentVideoID(),
//         playerVars: {
//             "autoplay": 1,
//             "cc_load_policy": 0
//         },
//         events: {
//             "onReady": onPlayerReady,
//             "onStateChange": function(event) {
//                 // Look at playerState for debugging
//                 var playerState = event.data;

//                 switch (playerState) {
//                     case YT.PlayerState.PLAYING:
//                         ZenPlayer.showPauseButton();
//                         break;
//                     default:
//                         ZenPlayer.showPlayButton();
//                 }
//             },
//             "onError": function(event) {
//                 // TODO: how can we replicate this?
//                 var message = "Got an unknown error, check the JS console.";
//                 var verboseMessage = message;

//                 // Handle the different error codes
//                 switch (event.data) {
//                     case 2:
//                         verboseMessage = "The request contains an invalid parameter value. For example, this error occurs if you specify a video ID that does not have 11 characters, or if the video ID contains invalid characters, such as exclamation points or asterisks.";
//                         message = "looks like an invalid video ID";
//                         break;
//                     case 5:
//                         verboseMessage = "The requested content cannot be played in an HTML5 player or another error related to the HTML5 player has occurred.";
//                         message = "we can't play that video here, or something is wrong with YouTube's iframe API";
//                         break;
//                     case 100:
//                         verboseMessage = "The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.";
//                         message = "we can't find that video, it might be private or removed";
//                         break;
//                     case 101:
//                         verboseMessage = "The owner of the requested video does not allow it to be played in embedded players.";
//                         message = "the video owner won't allow us to play that video";
//                         break;
//                     case 150:
//                         verboseMessage = "This error is the same as 101. It's just a 101 error in disguise!";
//                         message = "the video owner won't allow us to play that video";
//                         break;
//                 }

//                 // Update the UI w/ error
//                 errorMessage.show(message);
//                 ga("send", "event", "YouTube iframe API error", verboseMessage);
//                 sendKeenEvent("YouTube iframe API error", {verbose: verboseMessage, message: message, code: event.data});

//                 // Log debug info
//                 console.log("Verbose debug error message: ", verboseMessage);
//             }
//         }
//     });
// }

// TODO: refactor away
// function onPlayerReady(event) {
//     var currentVideoID = getCurrentVideoID();

//     updateTweetMessage();

//     // If the video isn't going to play, then return.
//     if (event && event.target && event.target.getPlayerState() === YT.PlayerState.UNSTARTED) {
//         errorMessage.show("Invalid YouTube videoID or URL.");
//         return;
//     }

//     // Setup player
//     if (currentVideoID) {
//         if (plyrPlayer) {
//             return;
//         }
//         // setupPlyr();
//         alert("death");
//     }
// }

var errorMessage = {
    init: function() {
        // nothing for now
    },
    show: function(message) {
        $("#zen-error").text("ERROR: " + message);
        $("#zen-error").show();

        // Pause if we got an error
        ZenPlayer.pause();

        // When the error message is shown, also hide the player
        ZenPlayer.hide();

        // Send the error to Google Analytics
        ga("send", "event", "error", message);
    },
    hide: function() {
        $("#zen-error").text("").hide();
    }
};

function isFileProtocol() {
    return window.location.protocol === "file:";
}

var ZenPlayer = {
    init: function(videoID) {
        // Inject svg with control icons
        $("#plyr-svg").load("../bower_components/plyr/dist/sprite.svg");

        plyrPlayer = document.querySelector(".plyr");

        plyr.setup(plyrPlayer, {
            autoplay: true,
            controls:["play", "current-time", "duration", "mute", "volume"]
        });

        // Load video into Plyr player
        if (plyrPlayer.plyr) {
            var that = this;
            plyrPlayer.addEventListener("error", function(event) {
                console.log("error");
                console.log(event);
                console.log(event.target.plyr);
                console.log(event.target.plyr.embed.api);
            });

            plyrPlayer.addEventListener("ready", function() {
                // Noop if we have nothing to play<commit_msg>Add TODO: for error handlings<commit_after>/*global getParameterByName, getSearchResults, getAutocompleteSuggestions, parseYoutubeVideoID, getYouTubeVideoDescription*/

// Pointer to Keen client
var client;

function anonymizeFileUrl() {
    var url = window.location.href;
    if (url.indexOf("file://") === 0) {
        url = "localhost";
    }
    return url;
}

function sendKeenEvent(_msg, _data) {
    var d = {
        page_url: anonymizeFileUrl(), //eslint-disable-line camelcase
        user_agent: "${keen.user_agent}", //eslint-disable-line camelcase
        ip_address: "${keen.ip}", //eslint-disable-line camelcase
        keen: {
            addons: [
                {
                    name: "keen:ip_to_geo",
                    input: {
                        ip: "ip_address"
                    },
                    output: "ip_geo_info"
                },
                {
                    name: "keen:ua_parser",
                    input: {
                        ua_string: "user_agent" //eslint-disable-line camelcase
                    },
                    output: "parsed_user_agent"
                },
                {
                    name: "keen:url_parser",
                    input: {
                        url: "page_url"
                    },
                    output: "parsed_page_url"
                }
            ]
        }
    };

    for (var _d in _data) {
        d[_d] = _data[_d];
    }
    client.addEvent(_msg, d);
}

/**
 * YouTube iframe API required setup
 */
// var player;
var plyrPlayer;
var youTubeDataApiKey = "<KEY>";
var currentVideoID;

// function onYouTubeIframeAPIReady() { //eslint-disable-line no-unused-vars
//     player = new YT.Player("player", {
//         height: "300",
//         width: "400",
//         // Parse the querystring and populate the video when loading the page
//         videoId: getCurrentVideoID(),
//         playerVars: {
//             "autoplay": 1,
//             "cc_load_policy": 0
//         },
//         events: {
//             "onReady": onPlayerReady,
//             "onStateChange": function(event) {
//                 // Look at playerState for debugging
//                 var playerState = event.data;

//                 switch (playerState) {
//                     case YT.PlayerState.PLAYING:
//                         ZenPlayer.showPauseButton();
//                         break;
//                     default:
//                         ZenPlayer.showPlayButton();
//                 }
//             },
//             "onError": function(event) {
//                 // TODO: how can we replicate this?
//                 var message = "Got an unknown error, check the JS console.";
//                 var verboseMessage = message;

//                 // Handle the different error codes
//                 switch (event.data) {
//                     case 2:
//                         verboseMessage = "The request contains an invalid parameter value. For example, this error occurs if you specify a video ID that does not have 11 characters, or if the video ID contains invalid characters, such as exclamation points or asterisks.";
//                         message = "looks like an invalid video ID";
//                         break;
//                     case 5:
//                         verboseMessage = "The requested content cannot be played in an HTML5 player or another error related to the HTML5 player has occurred.";
//                         message = "we can't play that video here, or something is wrong with YouTube's iframe API";
//                         break;
//                     case 100:
//                         verboseMessage = "The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.";
//                         message = "we can't find that video, it might be private or removed";
//                         break;
//                     case 101:
//                         verboseMessage = "The owner of the requested video does not allow it to be played in embedded players.";
//                         message = "the video owner won't allow us to play that video";
//                         break;
//                     case 150:
//                         verboseMessage = "This error is the same as 101. It's just a 101 error in disguise!";
//                         message = "the video owner won't allow us to play that video";
//                         break;
//                 }

//                 // Update the UI w/ error
//                 errorMessage.show(message);
//                 ga("send", "event", "YouTube iframe API error", verboseMessage);
//                 sendKeenEvent("YouTube iframe API error", {verbose: verboseMessage, message: message, code: event.data});

//                 // Log debug info
//                 console.log("Verbose debug error message: ", verboseMessage);
//             }
//         }
//     });
// }

// TODO: refactor away
// function onPlayerReady(event) {
//     var currentVideoID = getCurrentVideoID();

//     updateTweetMessage();

//     // If the video isn't going to play, then return.
//     if (event && event.target && event.target.getPlayerState() === YT.PlayerState.UNSTARTED) {
//         errorMessage.show("Invalid YouTube videoID or URL.");
//         return;
//     }

//     // Setup player
//     if (currentVideoID) {
//         if (plyrPlayer) {
//             return;
//         }
//         // setupPlyr();
//         alert("death");
//     }
// }

var errorMessage = {
    init: function() {
        // nothing for now
    },
    show: function(message) {
        $("#zen-error").text("ERROR: " + message);
        $("#zen-error").show();

        // Pause if we got an error
        ZenPlayer.pause();

        // When the error message is shown, also hide the player
        ZenPlayer.hide();

        // Send the error to Google Analytics
        ga("send", "event", "error", message);
    },
    hide: function() {
        $("#zen-error").text("").hide();
    }
};

function isFileProtocol() {
    return window.location.protocol === "file:";
}

var ZenPlayer = {
    init: function(videoID) {
        // Inject svg with control icons
        $("#plyr-svg").load("../bower_components/plyr/dist/sprite.svg");

        plyrPlayer = document.querySelector(".plyr");

        plyr.setup(plyrPlayer, {
            autoplay: true,
            controls:["play", "current-time", "duration", "mute", "volume"]
        });

        // Load video into Plyr player
        if (plyrPlayer.plyr) {
            var that = this;
            plyrPlayer.addEventListener("error", function(event) {
                // TODO: how do we see the real error message?
                console.log("error");
                console.log(event);
                console.log(event.target.plyr);
                console.log(event.target.plyr.embed.api);
            });

            plyrPlayer.addEventListener("ready", function() {
                // Noop if we have nothing to play
{'fraction_non_alphanumeric': 0.11950131047673018, 'fraction_numerical': 0.0042501948005950274, 'mean_word_length': 1.3787700084245997, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 5, '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': '458875', 'n_tokens_mistral': 3750, 'n_tokens_neox': 3514, 'n_words': 1048}