Source: models/production/search.js

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

    var channelManager = ChannelManager.getInstance();

    return Abstract.extend({

        /**
         * Resolves the endpoint.
         *
         * @param {Object} params - Search parameters.
         * @param {string} params.query - Text to be searched for in the backend.
         * @returns {string} Resolved endpoint.
         */
        resolveEndpoint: function (params) {
            var sanitizedQuery = encodeURIComponent(params.query),
                channelMap = channelManager.getChannelMap(),
                endpoint;

            if (params.formatByChannel) {
                this._formatByChannel = params.formatByChannel || false;
            }

            if (params.filterProgramType) {
                endpoint = '101/1.2.0/A/nld/smarttv/kpn/TRAY/SEARCH/PROGRAM?'
                    + 'filter_programType=' + params.genre.join(',')
                    + '&filter_startTime=' + params.startTime
                    + '&filter_endTime=' + params.endTime
                    + '&filter_channelIds=' + channelMap
                    + '&sortOrder=asc&from=0&to=9999&maxResults=10000'
                    + '&filter_isCatchUp=' + true
                    + '&filter_excludedGenres=erotiek';
            } else if (params.query.length === 0) {
                endpoint = '101/1.2.0/A/nld/smarttv/kpn/TRAY/SEARCH/PROGRAM?'
                    + 'filter_genre=' + params.genre.join(',')
                    + '&filter_startTime=' + params.startTime
                    + '&filter_endTime=' + params.endTime
                    + '&filter_channelIds=' + channelMap
                    + '&sortOrder=asc&from=0&to=9999&maxResults=10000'
                    + '&filter_isCatchUp=' + true
                    + '&filter_excludedGenres=erotiek';
            } else {
                endpoint = '101/1.2.0/A/nld/smarttv/kpn/TRAY/SEARCH/PROGRAM?filter_contentType=TVOY&'
                    + 'query=' + sanitizedQuery
                    + '&filter_startTime=' + params.startTime
                    + '&filter_endTime=' + params.endTime
                    + '&filter_channelIds=' + channelMap
                    + '&filter_isTvPremiere=true'
                    + '&filter_isCatchUp=' + true
                    + '&filter_excludedGenres=erotiek';
            }

            return endpoint;
        },

        /**
         * Transforms API data into the abstract model data.
         *
         * @param  {Object} response - The data object gotten from the API.
         * @returns {Object} The filled model instance.
         */
        transformFrom: function (response) {
            var results = Utils.getNested(response, 'resultObj', 'containers') || [],
                channels = {};

            if (this._formatByChannel) {

                Utils.each(results, function (entry) {

                    if (!channels[entry.channel.channelId]) {
                        channels[entry.channel.channelId] = [];
                    }

                    channels[entry.channel.channelId].push(new Broadcast(entry));
                });

                return channels;
            }

            return Utils.map(results, function (entry) {
                return new Broadcast(entry);
            });
        },

        /**
         * Validates the read response.
         *
         * @param {Object} response - The read response.
         * @returns {boolean} - True if valid.
         */
        validateReadResponse: function (response) {
            return response.resultCode === Configuration.RESPONSE_CODES.OK;
        }
    });
});