/* global hexo */
'use strict';
var CleanCSS = require('clean-css');

function OptimizeCSS(str, data) {
    var hexo = this,
        options = hexo.config.css_minifier;
    // Return if disabled.
    if (false === options.enable) return;

    var path = data.path;
    var exclude = options.exclude;
    if (exclude && !Array.isArray(exclude)) exclude = [exclude];

    if (path && exclude && exclude.length) {
        for (var i = 0, len = exclude.length; i < len; i++) {
            if (minimatch(path, exclude[i])) return str;
        }
    }

    var log = hexo.log || console.log;
    // var result = MoreCSS.compress(str, options.radical);
    // var saved = ((str.length - result.length) / str.length * 100).toFixed(2);
    // log.log('update Optimize CSS: %s [ %s saved]', path, saved + '%');
    // return result;

    return new Promise(function (resolve, reject) {
        new CleanCSS(options).minify(str, function (err, result) {
            if (err) return reject(err);
            var saved = ((str.length - result.styles.length) / str.length * 100).toFixed(2);
            resolve(result.styles);
            log.log('update Optimize CSS: %s [ %s saved]', path, saved + '%');
        });
    });
}

module.exports = OptimizeCSS;
@@ -61,16 +61,54 @@
 n-css');
+%0Avar minimatch = require('minimatch');
 %0A%0Afuncti
@@ -135,18 +135,16 @@
 data) %7B%0A
-  
   var he
@@ -154,20 +154,16 @@
 = this,%0A
-    
     opti
@@ -196,18 +196,16 @@
 fier;%0A  
- 
- 
 // Retur
@@ -223,26 +223,14 @@
 ed.%0A
-  
   if (
-false === 
 opti
@@ -239,16 +239,26 @@
 s.enable
+ === false
 ) return
@@ -260,18 +260,16 @@
 eturn;%0A%0A
-  
   var pa
@@ -284,18 +284,16 @@
 a.path;%0A
-  
   var ex
@@ -317,18 +317,16 @@
 xclude;%0A
-  
   if (ex
@@ -383,18 +383,16 @@
 de%5D;%0A%0A  
- 
- 
 if (path
@@ -428,20 +428,16 @@
 ) %7B%0A    
-   
- 
 for (var
@@ -478,30 +478,24 @@
 len; i++) %7B%0A
-      
       if (mi
@@ -541,23 +541,15 @@
     
-   
- 
 %7D%0A
-  
   %7D%0A%0A
-  
   va
@@ -579,311 +579,158 @@
 sole
-.log
 ;%0A  
- 
- // var result = MoreCSS.compress(str, options.radical);%0A    // var saved = ((str.length - result.length) / str.length * 100).toFixed(2);%0A    // log.log('update Optimize CSS: %25s %5B %25s saved%5D', path, saved + '%25');%0A    // return result;%0A%0A    return new Promise(function (resolve, reject) %7B%0A       
+var minifierOptions = Object.assign(%7B returnPromise: true %7D, options);%0A  delete minifierOptions.enable;%0A  delete minifierOptions.exclude;%0A  return
  new
@@ -739,24 +739,37 @@
 leanCSS(
-o
+minifierO
 ptions)
+%0A    
 .minify(
@@ -775,82 +775,32 @@
 (str
-, function (err, 
+)%0A    .then(
 result
-)
+ =%3E
  %7B%0A
-            if (err) return reject(err);%0A      
     
@@ -892,22 +892,88 @@
     
-      resolve(
+log.log('update Optimize CSS: %25s %5B %25s saved%5D', path, saved + '%25');%0A      return 
 resu
@@ -985,100 +985,116 @@
 yles
-)
 ;%0A    
-        log.log('update Optimize CSS: %25s %5B %25s saved%5D', path, saved + '%25');%0A        %7D)
+%7D)%0A    .catch(err=%3E%7B%0A      log.log('Cannot minifier CSS: %25s, because %25s', path, err);%0A      return str
 ;%0A  

use the latest clean css