(function () {
    global.dashModule
        .service('packagesUtilsService', ['$http', '$', '$translate', 'utilsService', packagesUtilsService]);

    function packagesUtilsService($http, $, $translate, utilsService) {
        let data = {};
        let packageTemplate = {};

        return {
            getTitle,
            getProductCategories,
            setCountryAndPackageSelected,
            getCountryAndPackageSelected,
            setPackageTemplateSelected,
            getPackageTemplateSelected,
            getPackagesPerCountry,
            getPackageTemplates
        };

        function getTitle(category, type, isTemplate) {
            const additionalTranslateName = isTemplate ? 'TEMPLATE_' : '';
            return type === 'products' ?
                $translate.instant('packages.headers.' + additionalTranslateName + category.toUpperCase() + '_PRODUCTS_BOX') :
                $translate.instant('packages.headers.' + additionalTranslateName + category.toUpperCase() + '_PACKAGES_BOX');
        }

        function getProductCategories() {
            return $http({
                method: 'GET',
                url: 'packages/api/getProductCategories'
            });
        }

        function setCountryAndPackageSelected(countryName, idCountry, packageSelected = {}) {
            data = {
                countrySelected: {
                    id: idCountry,
                    name: countryName
                },
                packageSelected
            };
            utilsService.executeRegisteredFunction('getProductsByCategories', data);
        }

        function getCountryAndPackageSelected() {
            return data;
        }

        function getPackagesPerCountry(idCountry) {
            return $http({
                method: 'POST',
                url: 'packages/api/getPackages',
                data: $.param({
                    idCountry
                })
            });
        }

        function setPackageTemplateSelected(packageSelected) {
            packageTemplate = packageSelected;
            utilsService.executeRegisteredFunction('getVirtualProductsByCategories', packageTemplate);
        }

        function getPackageTemplateSelected() {
            return packageTemplate;
        }

        function getPackageTemplates() {
            return $http({
                method: 'POST',
                url: 'packages/api/getTemplatePackagesForEdit'
            });
        }
    }
})();
