Source: models/halo/editorial.js

define('application/models/halo/editorial', [
    'rofl/models/api/abstract',
    'rofl/lib/utils',
    'application/components/uibuilder',
    'application/models/halo/editorialblock'
], function (
    Abstract,
    Utils,
    UIBuilder,
    EditorialBlock
) {
    'use strict';

    var endpoint = '/api/generalcontent/instance/search';

    return Abstract.extend({

        /**
         * Resolves the endpoint.
         *
         * @param {Object} params - The endpoint params.
         * @returns {string} - The endpoint.
         */
        resolveEndpoint: function (params) {
            return Utils.formatTemplate(endpoint, params);
        },

        /**
         * Transform data from API to Application for a Create method.
         *
         * @param {*} data - The data to be transformed.
         * @returns {Object} Transformed data.
         */
        transformFromCreate: function (data) {
            var values = Utils.getNested(data, 'items', '0', 'values'),
                editorialBlocks = [];

            // Transform editorial blocks.
            Utils.each(values.editorial_blocks, function (editorialBlock) {
                editorialBlocks.push(new EditorialBlock(editorialBlock));
            });

            this._item = {
                editorialBlocks: editorialBlocks
            };

            return this;
        },

        /**
         * Returns the type of widget this model to be used with.
         *
         * @returns {string} - The type of widget.
         */
        getWidgetType: function () {
            return UIBuilder.WIDGET_TYPES.EDITORIAL_BLOCK;
        },

        /**
         * Returns the module item.
         *
         * @returns {Object} - The module content.
         */
        getItem: function () {
            return this._item;
        },

        /**
         * Returns the item.
         *
         * @param {Object} options - Options to retrieve editorial blocks.
         * @param {Object} options.hasContent - True returns blocks with content, false returns blocks with or without content.
         * @returns {Object} - The actual hero item.
         */
        getEditorialBlocks: function (options) {
            var hasContent = options && options.hasContent,
                itemEditorialBlocks = this._item.editorialBlocks,
                editorialBlocks;

            if (hasContent) {

                // Filter out items with no content.
                editorialBlocks = Utils.filter(itemEditorialBlocks, function (block) {
                    return !!block.getWatchAll().getItems().length;
                });
            } else {
                editorialBlocks = itemEditorialBlocks;
            }

            return editorialBlocks;
        }
    });
});