"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.httpsOptions = httpsOptions;
exports.httpsOptionsWithUserFriendlyError = httpsOptionsWithUserFriendlyError;

var _buildToolLoggers = require("../../buildToolLoggers");

var _constants = require("../constants");

// import fs from 'fs';
// import path from 'path';
// export const httpsOptions = {
//   key: fs.readFileSync(path.join(__dirname, './cert/Tsicsezwild-22-23.key')),
//   cert: fs.readFileSync(path.join(__dirname, './cert/Tsicsezwild-22-23.crt'))
// };
function httpsOptions(options) {
  const {
    httpsCerts
  } = options.server; // eslint-disable-next-line global-require, import/no-dynamic-require

  const certificate = require(require.resolve(httpsCerts, {
    paths: [process.cwd()]
  })); // TODO: in future we may do some version based check. certificate.version


  return certificate.httpsOptions;
}

function httpsOptionsWithUserFriendlyError(options) {
  const {
    httpsCerts
  } = options.server;

  if (typeof httpsCerts === 'string' && httpsCerts) {
    try {
      const certificate = httpsOptions(options);
      return certificate;
    } catch (error) {
      if (error.msg.indexOf('Error: Cannot find module') !== -1) {
        (0, _buildToolLoggers.errorLogger)(`You have given ${_constants.BASE_CONFIG_KEY}.server.httpsCerts as "${httpsCerts}.
        But it seems that file path have some problem";`);
      }

      (0, _buildToolLoggers.errorLogger)(error);
      process.exit(0);
    }
  }

  return null;
}