/* global registerBackward */

registerBackward('1.7.14', function (object) {
    'use strict';

    function doTypographyBackward(prefixStyle, obj) {
        var passive = obj.typography[prefixStyle + 'LinkPassive'],
            hovered = obj.typography[prefixStyle + 'LinkHovered'],
            visited = obj.typography[prefixStyle + 'LinkVisited'],
            active = obj.typography[prefixStyle + 'LinkActive'];

        delete obj.typography[prefixStyle + 'LinkPassive'];
        delete obj.typography[prefixStyle + 'LinkHovered'];
        delete obj.typography[prefixStyle + 'LinkVisited'];
        delete obj.typography[prefixStyle + 'LinkActive'];

        if (passive) {
            obj.typography[prefixStyle + 'Link'] = passive;
        }
        if (hovered) {
            addStateTypography(obj, prefixStyle, 'hover');
            obj.hover.typography[prefixStyle + 'Link'] = hovered;
        }
        if (visited) {
            addStateTypography(obj, prefixStyle, 'visited');
            obj.visited.typography[prefixStyle + 'Link'] = visited;
        }
        if (active) {
            addStateTypography(obj, prefixStyle, 'active');
            obj.active.typography[prefixStyle + 'Link'] = active;
        }
    }

    function addStateTypography(obj, prefixStyle, state) {
        obj[state] = obj[state] || {};
        obj[state].typography = obj[state].typography || {};
        obj[state].typography[prefixStyle + 'Link'] = obj[state].typography[prefixStyle + 'Link'] || {};
    }

    function copyHeadings(obj, dest) {
        obj[dest] = obj[dest] || {};
        for (var i in obj.TypographyHeadings) {
            if (!obj[dest][i]) {
                obj[dest][i] = obj.TypographyHeadings[i];
            }
        }
    }

    function doControlBackward(control) {
        if (control.css && control.css.typography) {
            doTypographyBackward('TypographyText', control.css);
            doTypographyBackward('TypographyH1', control.css);
            doTypographyBackward('TypographyH2', control.css);
            doTypographyBackward('TypographyH3', control.css);
            doTypographyBackward('TypographyH4', control.css);
            doTypographyBackward('TypographyH5', control.css);
            doTypographyBackward('TypographyH6', control.css);
            doTypographyBackward('TypographyBulletList', control.css);
            doTypographyBackward('TypographyOrderedList', control.css);

            if (control.css.typography.TypographyHeadings) {
                copyHeadings(control.css.typography, 'TypographyH1');
                copyHeadings(control.css.typography, 'TypographyH2');
                copyHeadings(control.css.typography, 'TypographyH3');
                copyHeadings(control.css.typography, 'TypographyH4');
                copyHeadings(control.css.typography, 'TypographyH5');
                copyHeadings(control.css.typography, 'TypographyH6');
                delete control.css.typography.TypographyHeadings;
            }
        }
    }

    doControlBackward(object);

    return object;
});