'use strict';

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

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

/**
 * 为 Vue 组件添加 emits 属性
 * @param {string} filePath 组件地址
 * @param {string} [fileContent] 组件内容
 * @returns {string} 新的组件内容
 *
 * @example
 * ```ts
 * addNameForComponent('xxx.vue');
 * ```
 */
function addEmitsForComponent(filePath) {
  var fileContent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var content = fileContent;
  if (!content) {
    content = fs_fs.readFileSync(filePath, false);
  }
  if (/emits:\s*\[/.test(content)) return;
  var emits = getEmitList(content);
  if (!emits.length) return;
  var newSource = content.replace(/(?=\s\sdata\(\)\s*\{)/, getEmitsStr(emits));
  fs_fs.writeFileSync(filePath, newSource);
  return newSource;
}
function getEmitList(source) {
  var reg = /emit\('([^',]+)'/g;
  var emits = regexp_regexp.getMatchListFromReg(source, reg);
  var array = Array.from(new Set(emits));
  array.sort();
  return array;
}
function getEmitsStr(emits) {
  var emitsStr = emits.map(function (item) {
    return "'".concat(item, "'");
  }).join(',\n    ');
  return ['  emits: [', "    ".concat(emitsStr, ","), '  ],\n'].join('\n');
}

exports.addEmitsForComponent = addEmitsForComponent;
