import { getFs } from '../nodejs/fs.mjs';
import { getOs } from '../nodejs/os.mjs';
import { getPath } from '../nodejs/path.mjs';

var kMinutes = 601000;
var kDataFile = 't-comm-install.data';
function getNpmTipTemplate(_ref) {
  var prefix = _ref.prefix,
    link = _ref.link,
    postfix = _ref.postfix,
    feedbackTitle = _ref.feedbackTitle,
    feedbackList = _ref.feedbackList;
  return "".concat(colorFactory(96)).concat(prefix, " (").concat(colorFactory(94), " ").concat(link, " ").concat(colorFactory(96), ") ").concat(postfix).concat(colorFactory(), "\n\n") + "".concat(colorFactory(96)).concat(feedbackTitle).concat(colorFactory(), "\n") + "".concat(feedbackList.map(function (feedback) {
    return "".concat(colorFactory(96), ">").concat(colorFactory(94), " ").concat(feedback, " ").concat(colorFactory());
  }).join('\n'), "\n");
}
function getNpmTips(_ref2) {
  var packageName = _ref2.packageName,
    packageLink = _ref2.packageLink,
    packagePostfix = _ref2.packagePostfix,
    packagePostfixEn = _ref2.packagePostfixEn,
    feedbackList = _ref2.feedbackList;
  var content = isUtf8Encoding() ? getNpmTipTemplate({
    prefix: "\u611F\u8C22\u60A8\u4F7F\u7528".concat(packageName),
    link: packageLink,
    postfix: "".concat(packagePostfix, "\uFF01"),
    feedbackTitle: '如有任何疑问或建议，可通过下述渠道向我们反馈：',
    feedbackList: feedbackList
  }) : getNpmTipTemplate({
    prefix: "Thank you for using ".concat(packageName),
    link: packageLink,
    postfix: "".concat(packagePostfixEn, "!"),
    feedbackTitle: 'If you have any issue or advice, you can give us feedback:',
    feedbackList: feedbackList
  });
  return content;
}
function not(it) {
  return !it || it === '0' || it === 'false';
}
function colorFactory(color) {
  if (!process.stdout.hasColors()) {
    return '';
  }
  return "\x1B[".concat(color !== null && color !== void 0 ? color : 0, "m");
}
function isUtf8Encoding() {
  var _a;
  var _process = process,
    _process$env = _process.env,
    LANG = _process$env.LANG,
    LC_CTYPE = _process$env.LC_CTYPE;
  var language = ((_a = LANG !== null && LANG !== void 0 ? LANG : LC_CTYPE) !== null && _a !== void 0 ? _a : '').toLowerCase();
  if (language.includes('utf-8') || language.includes('utf8')) {
    return true;
  }
  if (process.platform === 'win32') {
    return true;
  }
  return false;
}
function isShowInfo(kContent) {
  // eslint-disable-next-line @typescript-eslint/naming-convention
  var _process2 = process,
    _process2$env = _process2.env,
    ADBLOCK = _process2$env.ADBLOCK,
    CI = _process2$env.CI,
    DISABLE_OPENCOLLECTIVE = _process2$env.DISABLE_OPENCOLLECTIVE,
    OPEN_SOURCE_CONTRIBUTOR = _process2$env.OPEN_SOURCE_CONTRIBUTOR,
    npm_config_loglevel = _process2$env.npm_config_loglevel;
  var isSilent = typeof npm_config_loglevel === 'string' && ['silent', 'error', 'warn'].includes(npm_config_loglevel);
  if (not(ADBLOCK) && not(CI) && not(DISABLE_OPENCOLLECTIVE) && not(OPEN_SOURCE_CONTRIBUTOR) && !isSilent) {
    var file = getPath().join(getOs().tmpdir(), kDataFile);
    var contents = [];
    try {
      var delta = Date.now() - getFs().statSync(file).mtime.getTime();
      if (delta >= 0 && delta < 3 * kMinutes) {
        contents = JSON.parse(getFs().readFileSync(file, 'utf8'));
        // if (contents.includes(kContent)) {
        //   return false;
        // }
      }
    } catch (_a) {
      contents = [];
    }
    try {
      contents.push(kContent);
      getFs().writeFileSync(file, JSON.stringify(contents), 'utf8');
    } catch (/* (empty function) */_b) {/* (empty function) */}
    return true;
  }
  return false;
}
function npmInstallTip(_ref3) {
  var packageName = _ref3.packageName,
    packageLink = _ref3.packageLink,
    packagePostfix = _ref3.packagePostfix,
    packagePostfixEn = _ref3.packagePostfixEn,
    feedbackList = _ref3.feedbackList;
  var content = getNpmTips({
    packageName: packageName,
    packageLink: packageLink,
    packagePostfix: packagePostfix,
    packagePostfixEn: packagePostfixEn,
    feedbackList: feedbackList
  });
  if (isShowInfo(content)) {
    console.log(content);
  }
}

export { npmInstallTip };
