'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

(function () {
  var fs = require('fs');
  var chalk = require('chalk');

  var gulp = require('gulp');
  var through = require('through-gulp');
  var $ = require('gulp-load-plugins')();

  var path = require('./path.js');

  var concat = require('./compile/concat.js');
  var html = require('./compile/html.js');
  var webp = require('./compile/webp.js');

  var rType = /\.(\w+)$/i;

  module.exports = function (pathname, options, callback) {
    if ((typeof pathname === 'undefined' ? 'undefined' : _typeof(pathname)) === 'object' && pathname.root && pathname.input) {
      concat(pathname, options, function (err, data) {
        callback(err, data, pathname.output);
      });
    } else {

      var type = pathname.output.match(rType);

      if (type && type[0]) {

        switch (type[0].toLowerCase()) {

          case '.html':
            // jade会生成 .html 文件所以不需要特殊处理其他 .htm .shtml .xhtml .dhtml 这些文件
            html(pathname, options, function (err, data) {
              callback(err, data, pathname.output);
            });
            break;
          case '.webp':
            //
            webp(pathname, options, function (err, data) {
              callback(err, data, pathname.output);
            });
            break;
          default:

            var devDir = pathname.config && pathname.config.dev_dir ? pathname.config.dev_dir : '';
            var publishDir = pathname.config && pathname.config.publish_dir ? pathname.config.publish_dir : './';

            var _pathname = path.join(pathname.root, devDir || '', pathname.output);

            if (fs.existsSync(_pathname)) {
              console.log(chalk.yellow('src:') + ' ' + chalk.grey(_pathname));
              gulp.src(_pathname, {
                base: path.join(pathname.root, devDir || '')
              }).pipe($.if(options.publish && pathname.config, gulp.dest(publishDir, {
                cwd: pathname.root
              }))).pipe(through(function (file) {
                callback(null, file.contents);
                return file;
              }));
              return;
            }

            _pathname = path.join(pathname.root, pathname.output);
            if (fs.existsSync(_pathname)) {
              gulp.src(_pathname).pipe(through(function (file) {
                callback(null, file.contents, pathname.output);
                return file;
              }));
            } else {
              callback(new Error('文件或目录不存在:' + _pathname));
            }
            break;
        }
      } else {

        var _pathname2 = path.join(pathname.root, pathname.output);
        if (fs.existsSync(_pathname2)) {
          gulp.src(_pathname2).pipe(through(function (file) {
            callback(null, file.contents, pathname.output);
            return file;
          }));
        } else {
          callback(new Error('文件或目录不存在:' + _pathname2));
        }
      }
      callback();
    }
  };
})();