/* global registerBackward, DataProviderFacade, __app */

registerBackward('3.12.0', 'Theme', function (control) {
    'use strict';

    var applyOrder = function (entity) {
        var parent = typeof entity.parent === 'undefined' ? '' : entity.parent;
        maxOrder[parent] = maxOrder[parent] || (parent ? 128 : 0);
        entity.order = maxOrder[parent];
        maxOrder[parent] += 128;
    };

    if (DataProviderFacade.getCmsInfo().cmsName === 'Html') {
        var maxOrder = {};
        var pages = __app.project.model.data.page;
        Object.keys(pages).map(function (k) { return pages[k]; }).forEach(applyOrder);

        maxOrder = {};
        var posts = __app.project.model.data.post;
        Object.keys(posts).map(function (k) { return posts[k]; }).forEach(applyOrder);
    }

    return control;
});