/* global AJS:false */
AJS.test.require([
    'com.atlassian.bitbucket.server.bitbucket-web:bitbucket-web-fragments',
    'com.atlassian.bitbucket.server.bitbucket-it-javascript:test-fragment-location',
    'com.atlassian.bitbucket.server.bitbucket-it-javascript:test-fragment-location-panel'
], function() {
    'use strict';

    var webFragments = require('bitbucket/internal/impl/web-fragments');

    QUnit.module('web-fragments');

    function assertTestItem(assert, item) {
        assert.strictEqual(item.type, 'ITEM');
        assert.strictEqual(item.weight, 1);
        assert.strictEqual(item.location, 'test-fragments');
        assert.strictEqual(item.url, '#my-item');
        assert.strictEqual(item.key, 'test-web-item');
        assert.strictEqual(item.text, 'my-item');
        assert.strictEqual(item.params['my-param'], 'my-value');
        assert.strictEqual(item.id, 'my-id');
        assert.strictEqual(item.cssClass, 'item-style');
        assert.strictEqual(item.tooltip, 'tooltip');
        assert.strictEqual(item.iconUrl, null);
        assert.strictEqual(item.iconWidth, 0);
        assert.strictEqual(item.iconHeight, 0);
    }

    function assertTestSection(assert, section) {
        assert.strictEqual(section.type, 'SECTION');
        assert.strictEqual(section.weight, 3);
        assert.strictEqual(section.location, 'test-fragments');
        assert.strictEqual(section.key, 'test-web-section');
        assert.strictEqual(section.text, 'my-section');
        assert.strictEqual(section.params['my-param'], 'my-value');
    }

    function assertTestPanel(assert, panel) {
        assert.strictEqual(panel.type, 'PANEL');
        assert.strictEqual(panel.weight, 2);
        assert.strictEqual(panel.location, 'test-fragments');
        assert.strictEqual(panel.key, 'test-web-panel');
        assert.strictEqual(panel.html, 'my-html');
        assert.strictEqual(panel.params['my-param'], 'my-value');
    }

    QUnit.test('getWebItems', function(assert) {
        var fragments = webFragments.getWebItems('test-fragments', { data : 'thing' });
        assert.ok(fragments instanceof Array);
        assert.strictEqual(fragments.length, 1);
        assertTestItem(assert, fragments[0]);
    });

    QUnit.test('getWebSections', function(assert) {
        var fragments = webFragments.getWebSections('test-fragments', { data : 'thing' });
        assert.ok(fragments instanceof Array);
        assert.strictEqual(fragments.length, 1);
        assertTestSection(assert, fragments[0]);
    });

    QUnit.test('getWebPanels', function(assert) {
        var fragments = webFragments.getWebPanels('test-fragments', { data : 'thing' });
        assert.ok(fragments instanceof Array);
        assert.strictEqual(fragments.length, 1);
        assertTestPanel(assert, fragments[0]);
    });


    QUnit.test('getWebFragments', function(assert) {
        var testerByType = {
            ITEM : assertTestItem,
            SECTION : assertTestSection,
            PANEL : assertTestPanel
        };
        Object.keys(testerByType).forEach(function(type) {
            var fragments = webFragments.getWebFragments('test-fragments', [ type ], { data : 'thing' });
            assert.ok(fragments instanceof Array);
            assert.strictEqual(fragments.length, 1);
            testerByType[type](assert, fragments[0]);
        });

        var fragments = webFragments.getWebFragments('test-fragments', ['ITEM', 'SECTION', 'PANEL'], { data : 'thing' });
        assert.ok(fragments instanceof Array);
        assert.strictEqual(fragments.length, 3);
        assertTestItem(assert, fragments[0]);
        assertTestPanel(assert, fragments[1]);
        assertTestSection(assert, fragments[2]);
    });

});
