'use strict';

angular.module('filters', [])

.filter( 'dateformat' , [ '$filter', 'util', 'app', function ( $filter, util, app ) {
        
    return function ( value, pattern, auto ) {
        
        var val;
        
        if( value === null) { 
            val = '--'; 
        }
        else {
            
            // var _date = $filter( 'date' )( new Date( value ), 'dd/MM hh:mm a' );
            // val = '<span class="date-info">' + _date.toUpperCase() + '</span>';
            // val =  _date.toUpperCase();
            if ( auto ) {
                
                return util.formatDateBasedOnDiff( value );
            }
            else {
                
                var date = util.getMoment( value );
                val = date.format( pattern || app.CALENDAR_FORMAT );
            }
        }
        return  val;//$sce.trustAsHtml(val);
    };
    
}])

// Escape html
.filter( 'trust_html', function ( $sce ) {

    return function ( value ) {
        
        return $sce.trustAsHtml(value);
    };
})

// Convert HEX text to string then escape it
.filter( 'escape_text', [ "$sce", "util", function ( $sce, util ) {
    
    // Escape using for HTML tag
    // Not escape using for input tag
    return function ( value, isEscape ) {
        
        if ( !angular.isString( value ))
            return value;
        
        // Convert them
        value = util.convertHexToString( value );
        
        // Escape 
        ( isEscape ) && ( value = util.escape( value ));
        
        return $sce.trustAsHtml(value);
    };
}])

// Fileter file size
.filter( 'size', function ( AppUtil ) {

    return function ( value, file ) {

        if ( typeof ( value ) === 'undefined' ) 
            return '--';

        if ( file ) {
            if  ( file.folder )
                return '--';
        }

        return AppUtil.convertFileSize( value);
    };
});


 


