(function () {
    'use strict';

    angular
        .module('app-base')
        .filter('nid', nid)
        .filter('uuid', uuid)
        .filter('drupalBody', drupalBody)
        .filter('drupalImage', drupalImage)
        .filter('drupalIframe', drupalIframe);

    /** @ngInject */
    function nid() {
        return function(node){
            if(!node.nid && !node._links){return;}
            if(node._links) {
                // drupal-8: hal_json
                var arr = node._links.self.href.split('/');
                return arr[arr.length - 1].split('?')[0];
            }
            if(typeof node.nid !== "string") {
                // drupal-8: json
                return node.nid[0].value;
            }else{
                // drupal-7: json
                return node.nid;
            }
        }
    }

    /** @ngInject */
    function uuid() {
        return function(entity){
            if(!entity.uuid){return;}
            if(typeof entity.uuid === "string") {
                return entity.uuid;
            }else{
                return entity.uuid[0].value;
            }
        }
    }

    /** @ngInject */
    function drupalBody(Drupal) {
        return function(input,styleName){
            if(!input){return;}
            var params = Drupal.model.siteParams;
            function replaceAll(src,str1,str2,ignore) {
                return String(src).replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
            }
            if(Drupal.version==='drupal-8'){
                // todo: nl to br: input = input.replace(new RegExp('\n','g'), '<br />')
                return replaceAll(input,'/'+params.file_directory_path, params.file_baseUrl);
            }
            if(Drupal.version==='drupal-7'){
                var file_root = params.baseUrl + params.file_directory_path +'/';
                return replaceAll(input,'public://', file_root);
            }
        }
    }

    /** @ngInject */
    function drupalImage(Drupal) {
        return function(input,styleName){
            if(!input){return;}
            var params = Drupal.model.siteParams;
            if(Drupal.version == 'drupal-8'){
                if(!styleName){
                    return input;
                }
                var path_to_files = Drupal.model.siteParams.file_baseUrl;
                var stylePath = 'styles/' + styleName + '/public/';
                var arr = input.split(path_to_files);
                var url = path_to_files + stylePath + arr[1];
                return url;
            }
            if(Drupal.version=='drupal-7'){
                var style = styleName || 'original';
                var media_root = params.baseUrl + params.file_directory_path +'/';
                if(style != 'original'){
                    media_root += 'styles/'+style+'/public/';
                }
                return input.replace('public://',media_root);
            }
        }
    }

    /** @ngInject */
    function drupalIframe() {
        return function(input){
            if(!input){return;}
            if(!input.match('http://')){ input = 'http://'+input }
            return input
        }
    }

})();
