<commit_before>(function() {

    'use strict';

    PackagesService.$inject = ['api', '$q'];
    function PackagesService(api, $q) {

        this.fetch = function(item) {
            if (item.linked_in_packages == null) {
                return $q.when(null);
            }
            var id = item.linked_in_packages[0]['package'];
            return api.find('packages', id).then(angular.bind(this, function(result) {
                return $q.when(result);
            }));
        };

        this.createPackageFromItem = function createPackageFromItem(item, idRef) {
            var new_package = {
                headline: item.headline || '',
                slugline: item.slugline || '',
                description: item.description || '',
                groups: [
                    {
                    role: 'grpRole:NEP',
                    refs: [{idRef: idRef}],
                    id: 'root'
                },
                {
                    refs: [{
                        headline: item.headline || '',
                        residRef: item._id,
                        location: 'archive',
                        slugline: item.slugline || '',
                        renditions: item.renditions
                    }],
                    id: idRef,
                    role: 'grpRole:Main'
                }]
            };

            return api.packages.save(new_package);
        };

        this.addItemsToPackage = function addToPackage(currentPackage, items, idRef) {
            var self = this;

            var patch = _.pick(currentPackage, 'groups');
            var rootGroup = _.find(patch.groups, function(group) { return group.id === 'root'; });
            _.forEach(items, function(item) {
                var newId = self.generateNewId(rootGroup.refs, idRef);
                rootGroup.refs.push({idRef: newId});
                patch.groups.push({
                    refs: [{
                        headline: item.headline || '',
                        residRef: item._id,
                        location: 'archive',
                        slugline: item.slugline || '',
                        renditions: item.renditions
                    }],
                    id: newId,
                    role: 'grpRole:' + newId
                });
            });
            return api.packages.save(currentPackage, patch);
        };

        this.generateNewId = function generateNewId(refs, idRef) {
            var filter = function(ref) { return (ref.idRef.toLowerCase().indexOf(idRef.toLowerCase())) === 0 ? 'found' : 'none'; };
            var counts = _.countBy(refs, filter);
            return counts.found ? (idRef + '-' + counts.found) : idRef;
        };
    }

    PackagesCtrl.$inject = ['$scope', 'packagesService', 'superdesk'];
    function PackagesCtrl($scope, packagesService, superdesk) {
        $scope.selected = {};
        $scope.selected.hide_menu = true;
        $scope.contenttab = true;
        $scope.$watch('item._id', reload);

        $scope.itemTypes = [
            {
                icon: 'text',
                label: 'Story'
            },
            {
                icon: 'text',
                label: 'Sidebar'
            },<commit_msg>feat(packages) - fix  renditions<commit_after>(function() {

    'use strict';

    PackagesService.$inject = ['api', '$q'];
    function PackagesService(api, $q) {

        this.fetch = function(item) {
            if (item.linked_in_packages == null) {
                return $q.when(null);
            }
            var id = item.linked_in_packages[0]['package'];
            return api.find('packages', id).then(angular.bind(this, function(result) {
                return $q.when(result);
            }));
        };

        this.getGroupFor = function (item, idRef) {
            return {
                refs: [{
                    headline: item.headline || '',
                    residRef: item._id,
                    location: 'archive',
                    slugline: item.slugline || '',
                    renditions: item.renditions || {}
                }],
                id: idRef,
                role: 'grpRole:' + idRef
            };
        };

        this.createPackageFromItem = function createPackageFromItem(item, idRef) {
            var new_package = {
                headline: item.headline || '',
                slugline: item.slugline || '',
                description: item.description || '',
                groups: [
                    {
                    role: 'grpRole:NEP',
                    refs: [{idRef: idRef}],
                    id: 'root'
                    },
                    this.getGroupFor(item, idRef)
                ]
            };

            return api.packages.save(new_package);
        };

        this.addItemsToPackage = function addToPackage(currentPackage, items, idRef) {
            var self = this;

            var patch = _.pick(currentPackage, 'groups');
            var rootGroup = _.find(patch.groups, function(group) { return group.id === 'root'; });
            _.forEach(items, function(item) {
                var newId = self.generateNewId(rootGroup.refs, idRef);
                rootGroup.refs.push({idRef: newId});
                patch.groups.push(self.getGroupFor(item, newId));
            });
            return api.packages.save(currentPackage, patch);
        };

        this.generateNewId = function generateNewId(refs, idRef) {
            var filter = function(ref) { return (ref.idRef.toLowerCase().indexOf(idRef.toLowerCase())) === 0 ? 'found' : 'none'; };
            var counts = _.countBy(refs, filter);
            return counts.found ? (idRef + '-' + counts.found) : idRef;
        };
    }

    PackagesCtrl.$inject = ['$scope', 'packagesService', 'superdesk'];
    function PackagesCtrl($scope, packagesService, superdesk) {
        $scope.selected = {};
        $scope.selected.hide_menu = true;
        $scope.contenttab = true;
        $scope.$watch('item._id', reload);

        $scope.itemTypes = [
            {
                icon: 'text',
                label: 'Story'
            },
            {
                icon: 'text',
                label: 'Sidebar'
            },
{'fraction_non_alphanumeric': 0.12888248479026576, 'fraction_numerical': 0.0006404098623118795, 'mean_word_length': 1.3502633559066968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5623405', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1595, 'n_words': 303}