<commit_before>import _ from 'lodash';

interface IScope extends ng.IScope {
    validator: any;
    origin: any;
    images: any;
    placeholder: any;
    isDirty: any;
    selectImage: any;
    onBlur: any;
    onChange: any;
    metadata: any;
    save: any;
    close: any;
    $close: any;
}

MultiImageEditController.$inject = ['$scope', 'deployConfig', 'modal', 'gettextCatalog', 'data', 'authoring'];
export function MultiImageEditController($scope: IScope, deployConfig, modal, gettextCatalog, data, authoring) {
    let changes = {};

    $scope.validator = deployConfig.getSync('validator_media_metadata');

    $scope.origin = angular.copy(data);
    $scope.images = angular.copy(data);

    $scope.placeholder = {};

    $scope.isDirty = () => !_.isEmpty(changes);

    $scope.selectImage = (image) => {
        image.unselected = !image.unselected;
        image.unselected
            ? _.remove($scope.images, (res: any) => res._id === image._id)
            : $scope.images.push(image);

        return updateMetadata();
    };

    $scope.onBlur = () => {
        _.map($scope.origin, (item) => {
            if (_.find($scope.images, (image) => image._id === item._id)) {
                _.merge(item, _.pick($scope.metadata, _.keys(changes)));
            }
        });
    };

    $scope.onChange = (field) => {
        changes[field] = true;
        $scope.placeholder[field] = '';
    };

    $scope.save = (close) => {
        angular.forEach(data, (image) => {
            authoring.save(image, _.find($scope.origin, (item) => item._id === image._id))
                .then(() => {
                    changes = {};
                });
        });

        if (close) {
            $scope.$close();
        }
    };

    $scope.close = () => {
        if ($scope.isDirty()) {
            modal.confirm(
                gettextCatalog.getString('You have unsaved changes, do you want to continue?'),
                gettextCatalog.getString('Confirm'),
            )
                .then(() => {
                    $scope.$close();
                });
        } else {
            $scope.$close();
        }
    };

    updateMetadata();

    function updateMetadata() {
        $scope.metadata = {
            headline: compare('headline'),
            description_text: compare('description_text'),
            archive_description: compare('archive_description'),
            alt_text: compare('alt_text'),
            byline: compare('byline'),
            copyrightholder: compare('copyrightholder'),
            usageterms: compare('usageterms'),
            copyrightnotice: compare('copyrightnotice'),
        };
    }

    function compare(value) {
        if (!$scope.images.length) {
            $scope.placeholder = {};
            return '';
        }

        let uniqueValue = true;
        let initialValue = _.find($scope.origin, (origImage) => origImage._id === $scope.images[0]._id);

        angular.forEach($scope.images, (image) => {
            let compareImage = _.find($scope.origin, (origImage) => origImage._id === image._id);

            if (initialValue[value] !== compareImage[value]) {
                uniqueValue = false;
            }
        });

        if (uniqueValue) {
            $scope.placeholder[value] = '';
            return initialValue[value];
        }

        $scope.placeholder[value] = '(multiple values)';

        return '';
    }
}

/**
 * @ngdoc service
 * @module superdesk.apps.search
 * @name multiImageEdit
 *
 * @requires $modal
 *
 * @description Service for editing metadata of multiple images at same time
 */
MultiImageEditService.$inject = ['$modal'];
export function MultiImageEditService($modal) {
    this.edit = (data) => {
        $modal.open({
            templateUrl: 'scripts/apps/search/views/multi-image-edit.html',
            controller: MultiImageEditController,
            size: 'fullscreen modal--dark-ui',
            resolve: {
                data: () => data,
            },
        });
    };
}<commit_msg>rename for clarity<commit_after>import _ from 'lodash';

interface IScope extends ng.IScope {
    validator: any;
    origin: any;
    images: any;
    placeholder: any;
    isDirty: any;
    selectImage: any;
    onBlur: any;
    onChange: any;
    metadata: any;
    save: any;
    close: any;
    $close: any;
}

MultiImageEditController.$inject = ['$scope', 'deployConfig', 'modal', 'gettextCatalog', 'images', 'authoring'];
export function MultiImageEditController($scope: IScope, deployConfig, modal, gettextCatalog, images, authoring) {
    let changes = {};

    $scope.validator = deployConfig.getSync('validator_media_metadata');

    $scope.origin = angular.copy(images);
    $scope.images = angular.copy(images);

    $scope.placeholder = {};

    $scope.isDirty = () => !_.isEmpty(changes);

    $scope.selectImage = (image) => {
        image.unselected = !image.unselected;
        image.unselected
            ? _.remove($scope.images, (res: any) => res._id === image._id)
            : $scope.images.push(image);

        return updateMetadata();
    };

    $scope.onBlur = () => {
        _.map($scope.origin, (item) => {
            if (_.find($scope.images, (image) => image._id === item._id)) {
                _.merge(item, _.pick($scope.metadata, _.keys(changes)));
            }
        });
    };

    $scope.onChange = (field) => {
        changes[field] = true;
        $scope.placeholder[field] = '';
    };

    $scope.save = (close) => {
        angular.forEach(images, (image) => {
            authoring.save(image, _.find($scope.origin, (item) => item._id === image._id))
                .then(() => {
                    changes = {};
                });
        });

        if (close) {
            $scope.$close();
        }
    };

    $scope.close = () => {
        if ($scope.isDirty()) {
            modal.confirm(
                gettextCatalog.getString('You have unsaved changes, do you want to continue?'),
                gettextCatalog.getString('Confirm'),
            )
                .then(() => {
                    $scope.$close();
                });
        } else {
            $scope.$close();
        }
    };

    updateMetadata();

    function updateMetadata() {
        $scope.metadata = {
            headline: compare('headline'),
            description_text: compare('description_text'),
            archive_description: compare('archive_description'),
            alt_text: compare('alt_text'),
            byline: compare('byline'),
            copyrightholder: compare('copyrightholder'),
            usageterms: compare('usageterms'),
            copyrightnotice: compare('copyrightnotice'),
        };
    }

    function compare(value) {
        if (!$scope.images.length) {
            $scope.placeholder = {};
            return '';
        }

        let uniqueValue = true;
        let initialValue = _.find($scope.origin, (origImage) => origImage._id === $scope.images[0]._id);

        angular.forEach($scope.images, (image) => {
            let compareImage = _.find($scope.origin, (origImage) => origImage._id === image._id);

            if (initialValue[value] !== compareImage[value]) {
                uniqueValue = false;
            }
        });

        if (uniqueValue) {
            $scope.placeholder[value] = '';
            return initialValue[value];
        }

        $scope.placeholder[value] = '(multiple values)';

        return '';
    }
}

/**
 * @ngdoc service
 * @module superdesk.apps.search
 * @name multiImageEdit
 *
 * @requires $modal
 *
 * @description Service for editing metadata of multiple images at same time
 */
MultiImageEditService.$inject = ['$modal'];
export function MultiImageEditService($modal) {
    this.edit = (images) => {
        $modal.open({
            templateUrl: 'scripts/apps/search/views/multi-image-edit.html',
            controller: MultiImageEditController,
            size: 'fullscreen modal--dark-ui',
            resolve: {
                images: () => images,
            },
        });
    };
}
{'fraction_non_alphanumeric': 0.15230063251891354, 'fraction_numerical': 0.00024804663276696017, 'mean_word_length': 1.9833518312985572, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '4665250', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2224, 'n_words': 428}