'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var fs_fs = require('../fs/fs.js');
var nodejs_fs = require('../nodejs/fs.js');
var nodejs_path = require('../nodejs/path.js');
var fs_fsUtil = require('../fs/fs-util.js');
require('../nodejs/crypto.js');
require('../nodejs/os.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../time/time.js');

/**
 * 统计页面总数、分包数目等
 * @param dist
 * @returns result
 *
 * @example
 * ```ts
 * getPageTotal('./dist/dev/mp-weixin')
 * ```
 */
function getPageTotal(dist) {
  var _a;
  var appJson = nodejs_path.getPath().resolve(dist, 'app.json');
  var result = {
    pageTotal: 0,
    subPackageTotal: 0
  };
  if (!nodejs_fs.getFs().existsSync(appJson)) {
    return result;
  }
  var data = fs_fs.readFileSync(appJson, true);
  var _data$pages = data.pages,
    pages = _data$pages === void 0 ? [] : _data$pages;
  var subPackages = ((_a = data.subPackages) === null || _a === void 0 ? void 0 : _a.length) ? data.subPackages : data.subpackages || [];
  result.pageTotal = pages.length + subPackages.reduce(function (acc, item) {
    var _a;
    acc += ((_a = item.pages) === null || _a === void 0 ? void 0 : _a.length) || 0;
    return acc;
  }, 0);
  result.subPackageTotal = subPackages.length;
  return result;
}
/**
 * 统计组件数目、wxml大小、wxss大小、js大小等
 * @param dist
 * @returns result
 *
 * @example
 * ```ts
 * getComponentInfo('./dist/dev/mp-weixin')
 * ```
 */
function getComponentInfo(dist) {
  var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
    wxmlPostfix: '.wxml',
    wxssPostfix: '.wxss',
    vendorNames: ['common/vendor-1.js', 'common/vendor.js']
  };
  var result = {
    componentTotal: 0,
    wxsstTotal: 0,
    jsTotal: 0,
    wxmlSizeTotal: 0,
    wxssSizeTotal: 0,
    jsSizeTotal: 0,
    vendorJsSize: 0
  };
  if (!nodejs_fs.getFs().existsSync(dist)) {
    return result;
  }
  var isVendorJs = function isVendorJs(file) {
    var _a;
    return !!((_a = config.vendorNames) === null || _a === void 0 ? void 0 : _a.find(function (item) {
      return file.endsWith(item);
    }));
  };
  try {
    fs_fsUtil.traverseFolder(function (tPath) {
      var fileSize = nodejs_fs.getFs().statSync(tPath).size;
      if (tPath.endsWith(config.wxmlPostfix)) {
        result.componentTotal += 1;
        result.wxmlSizeTotal += fileSize;
      }
      if (tPath.endsWith(config.wxssPostfix)) {
        result.wxsstTotal += 1;
        result.wxssSizeTotal += fileSize;
      }
      if (tPath.endsWith('.js')) {
        result.jsTotal += 1;
        result.jsSizeTotal += fileSize;
      }
      if (isVendorJs(tPath)) {
        result.vendorJsSize += fileSize;
      }
    }, dist);
  } catch (err) {
    console.log('traverseFolder.error: ', err);
  }
  return result;
}

exports.getComponentInfo = getComponentInfo;
exports.getPageTotal = getPageTotal;
