define([
    'aui',
    'bitbucket/internal/util/analytics'
], function(
    AJS,
    analytics
) {

    'use strict';

    module('util-analytics', {
        setup: function(){
            sinon.spy(AJS, 'trigger');
        },
        teardown: function(){
            AJS.trigger.restore();
        }
    });

    test('Happy path', function () {
        var attributes = {
            isAuthor: true,
            textLength: 100
        };

        var combinedAttributes = {
            name: 'stash.client.test.event',
            data: {
                isAuthor: true,
                textLength: 100
            }
        };

        analytics.add('test.event', attributes);

        ok(AJS.trigger.calledOnce, 'AJS.trigger is called');
        deepEqual(AJS.trigger.args[0], ['analytics', combinedAttributes], 'The payload is combined correctly for AJS.trigger');
    });

    test('Event name is required', function(){
        analytics.add();
        ok(!AJS.trigger.called, 'AJS.trigger is not called if no event name supplied');
    });


    test('Event name prefixing', function(){
        analytics.add('test.event');
        equal(AJS.trigger.args[0][1].name, 'stash.client.test.event', 'Prefix is added correctly');

        analytics.add('stash.client.test.event');
        equal(AJS.trigger.args[1][1].name, 'stash.client.test.event', 'Prefix is not added if it already exists');
    });

    test('Data must be a plain object', function(){
        analytics.add('test.event');
        ok(AJS.trigger.calledOnce, 'Event is triggered if data is null');
        equal(AJS.trigger.args[0].data, null, 'No attribute data is present in the event if data is null');

        throws(analytics.add.bind(analytics, 'test.event', new Date()), Error, "Invalid data throws error");
        ok(AJS.trigger.calledOnce, 'Trigger not called on invalid data');
    });

    function verifyDemographics(obj, description) {
        ok(obj.d_browser, description + "browser");
        ok(obj.d_version, description + "version");
        ok(obj.d_platform, description + "platform");
        ok(obj.d_windowHeight, description + "window height");
        ok(obj.d_windowWidth, description + " window width");
        ok(obj.d_screenHeight, description + "screen height");
        ok(obj.d_screenWidth, description + "screen width");
    }

    test('demographics flag is respected', function(){
        var attributes = {
            isAuthor: true,
            textLength: 100
        };

        analytics.add('test.event', attributes, true);

        ok(AJS.trigger.calledOnce, 'AJS.trigger is called');
        verifyDemographics(AJS.trigger.args[0][1].data, '');
    });

    test('_mixinDemographics', function(){
        var attributes = {
            isAuthor: true,
            textLength: 100
        };

        verifyDemographics(analytics._mixinDemographics(), 'No object supplied: ');
        verifyDemographics(analytics._mixinDemographics({}), 'Empty object supplied: ');
        verifyDemographics(analytics._mixinDemographics(attributes), 'populated object supplied: ');
    });
});
