"use strict";var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");var _defineProperty = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/define-property"));var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
(0, _defineProperty.default)(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const fs = require("fs");
const path = require("path");
const chromePathGetter = require("chrome-paths");
function lookupChromeWindows() {
  return new _promise.default((resolve, reject) => {
    let chromePath = '';
    const chromeSysRegHKey = '"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe" /v path';
    child_process_1.exec(`REG QUERY ${chromeSysRegHKey}`, {}, (err, stdout) => {
      if (!err) {
        chromePath = downgradLookup();
        if (!chromePath) {
          reject(err);
          return;
        } else
        {
          resolve(chromePath);
          return;
        }
      }
      if (typeof stdout === 'string') {
        chromePath = parseChromePathFromRegOutput(stdout);
      }
      if (!chromePath) {
        chromePath = chromePathGetter.chrome || chromePathGetter.chromeCanary;
      }
      resolve(chromePath);
    });
  });
}
exports.default = lookupChromeWindows;
function downgradLookup() {
  let chromePath = '';
  const suffix = '\\Google\\Chrome\\Application\\chrome.exe';
  const prefixes = [
  process.env.LOCALAPPDATA || '',
  process.env.PROGRAMFILES || '',
  process.env['PROGRAMFILES(X86)'] || ''];

  for (const prefix of prefixes) {
    try {
      chromePath = path.join(prefix, suffix);
      fs.accessSync(chromePath);
      break;
    }
    catch (err) {}
  }
  return chromePath;
}
function parseChromePathFromRegOutput(stdout) {
  const lines = stdout.split('\n');
  let testPath = '',chromePath = '';
  lines.every((line) => {
    const targLine = line.match(/path\s+reg_sz\s+(.+)/i);
    if (targLine) {
      testPath = targLine[1];
      return false;
    }
    return true;
  });
  try {
    testPath = path.resolve(testPath, 'chrome.exe');
    fs.accessSync(testPath);
    chromePath = testPath;
  }
  catch (err) {}
  return chromePath;
}