define([], function () {
    'use strict';

    function supportsHtmlMediaAutoplay() {

        return new Promise(function (resolve, reject) {

            var timeout;
            var elem = document.createElement('video');
            var elemStyle = elem.style;
            //skip the test if video itself, or the autoplay
            //element on it isn't supported
            if (!('autoplay' in elem)) {
                reject();
                return;
            }
            elemStyle.position = 'absolute';
            elemStyle.height = 0;
            elemStyle.width = 0;

            elem.setAttribute('autoplay', 'autoplay');
            elem.style.display = 'none';
            document.body.appendChild(elem);

            var testAutoplay = function (arg) {
                clearTimeout(timeout);
                elem.removeEventListener('playing', testAutoplay);
                elem.removeEventListener('play', testAutoplay);
                var supported = (arg && arg.type === 'playing') || (arg && arg.type === 'play') || elem.currentTime !== 0;
                elem.parentNode.removeChild(elem);

                if (supported) {
                    resolve();
                } else {
                    reject();
                }
            };

            // play needed for firefox
            elem.addEventListener('play', testAutoplay);
            elem.addEventListener('playing', testAutoplay);

            try {
                elem.src = 'data:video/mp4;base64,AAAAHGZ0eXBtcDQyAAAAAG1wNDJpc29tYXZjMQAAAz5tb292AAAAbG12aGQAAAAAzaNacc2jWnEAAV+QAAFfkAABAAABAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAGGlvZHMAAAAAEICAgAcAT////3//AAACQ3RyYWsAAABcdGtoZAAAAAHNo1pxzaNacQAAAAEAAAAAAAFfkAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAEAAAAAAEAAAABAAAAAAAd9tZGlhAAAAIG1kaGQAAAAAzaNacc2jWnEAAV+QAAFfkFXEAAAAAAAhaGRscgAAAAAAAAAAdmlkZQAAAAAAAAAAAAAAAAAAAAGWbWluZgAAABR2bWhkAAAAAQAAAAAAAAAAAAAAJGRpbmYAAAAcZHJlZgAAAAAAAAABAAAADHVybCAAAAABAAABVnN0YmwAAACpc3RzZAAAAAAAA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wIGJsdXJheV9jb21wYXQ9MCBjb25zdHJhaW5lZF9pbnRyYT0wIGJmcmFtZXM9MCB3ZWlnaHRwPTAga2V5aW50PTUwIGtleWludF9taW49NSBzY2VuZWN1dD00MCBpbnRyYV9yZWZyZXNoPTAgcmM9Y3FwIG1idHJlZT0wIHFwPTAAgAAAAD5liISscR8A+E4ACAACFoAAITAAAgsAAPgYCoKgoC+L4vi+KAvi+L4YfAEAACMzgABF9AAEUGUgABDJiXnf4AAAAARBmiKUAAAABEGaQpQAAAAEQZpilAAAAARBmoKU';
                var promise = elem.play();
                if (promise && promise.catch) {
                    promise.catch(reject);
                }

                timeout = setTimeout(testAutoplay, 500);
            }

            catch (e) {
                reject();
                return;
            }
        });
    }

    return {
        supportsHtmlMediaAutoplay: supportsHtmlMediaAutoplay
    };
});