'use strict';

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

var fs_fs = require('../fs/fs.js');
require('../nodejs/crypto.js');
require('../nodejs/fs.js');
require('../nodejs/os.js');

var DEFAULT_EXTRACT_REGEXP = /\$emit\('([\w]+)'/g;
function getEventList(filePath, extractRegexp) {
  var data = fs_fs.readFileSync(filePath);
  var eventList = [];
  var match = extractRegexp.exec(data);
  while (match) {
    eventList.push({
      name: match[1]
    });
    match = extractRegexp.exec(data);
  }
  return eventList;
}
function genTable(list) {
  var table = ['| 事件名 | 说明     | 参数 |', '| ------------------ | ---------------- | --------- |'];
  list.forEach(function (item) {
    var name = item.name;
    table.push("| ".concat(name, "          |   |-   |"));
  });
  return table.join('\n');
}
/**
 * 提取 Vue 组件的 event
 * @param {obj} params 参数
 * @param {string} params.filePath 源文件地址
 * @param {string} [params.targetFilePath] 输出文件地址
 * @param {Regexp} [params.extractRegexp] 提取正则
 * @example
 *
 * ```ts
 * extractEvent({
 *   filePath: 'xxx.vue',
 * })
 * ```
 */
function extractEvent(_ref) {
  var filePath = _ref.filePath,
    _ref$targetFilePath = _ref.targetFilePath,
    targetFilePath = _ref$targetFilePath === void 0 ? './log/extract-event.md' : _ref$targetFilePath,
    _ref$extractRegexp = _ref.extractRegexp,
    extractRegexp = _ref$extractRegexp === void 0 ? DEFAULT_EXTRACT_REGEXP : _ref$extractRegexp;
  var eventList = getEventList(filePath, extractRegexp);
  var table = genTable(eventList);
  fs_fs.writeFileSync(targetFilePath, table);
}

exports.extractEvent = extractEvent;
