Source: models/production/epg.js

define('application/models/production/epg', [
    'rofl/models/api/abstract',
    'rofl/lib/utils',
    'application/models/configuration',
    'application/models/epg/item',
    'application/managers/channel'
], function (
    Abstract,
    Utils,
    KPNConfig,
    EPGItem,
    ChannelManager
) {
    'use strict';

    var channelManager = ChannelManager.getInstance(),
        formatItems = function (unformatted, channel) {
        var items = [];

        Utils.each(unformatted, function (item) {
            items.push(new EPGItem(item, channel));
        });

        return items;
    };

    return Abstract.extend({

        /**
         * Resolves the endpoint.
         *
         * @param {Object} params - The parameters (optional).
         * @returns {string} - Endpoint.
         */
        resolveEndpoint: function (params) {
            var endpoint = '101/1.2.0/A/nld/smarttv/kpn/TRAY/EPG';

            if (!params || !params.startTime || !params.endTime) {
                throw 'EPG: Time is required for EPG calls.';
            }

            endpoint += Utils.formatTemplate('?from=0&to=10000&filter_startTime={{startTime}}&filter_endTime={{endTime}}', params);

            if (params && params.channels) {

                endpoint += '&filter_channelIds=' + params.channels.join();

                this._channels = params.channels;
            }

            return endpoint;
        },

        /**
         * Transforms the response.
         *
         * @param {Object} response - The response.
         * @returns {Object} Result.
         */
        transformFrom: function (response) {
            var data = Utils.getNested(response, 'resultObj', 'containers'),
                self = this,
                result = {};

            Utils.each(data, function (container) {
                var channelId = Utils.getNested(container, 'metadata', 'channelId'),
                    channel = channelManager.getChannelById(channelId);

                if (channel) {
                    self._channels.splice(self._channels.indexOf(channelId), 1);
                    result[channelId] = formatItems(container.containers, channel);
                }
            });

            Utils.each(this._channels, function (missingChannel) {
                result[missingChannel] = [];
            });

            return result;
        },

        /**
         * Validates the response.
         *
         * @param {Object} response - The response.
         * @returns {boolean} - True if valid.
         */
        validateResponse: function (response) {
            return Utils.getNested(response, 'resultCode') === KPNConfig.RESPONSE_CODES.OK;
        }
    });
});