"use strict";

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

var _logger = require("../logger");

var _requireLocalOrGlobal = require("./requireLocalOrGlobal");

const globalCertificate = (0, _requireLocalOrGlobal.requireGlobal)('@zohodesk-private/client_dev_cert');

function httpsOptions(options) {
  const {
    httpsCerts
  } = options.app.server;
  const certificate = (0, _requireLocalOrGlobal.requireLocalOrGlobal)(httpsCerts); // TODO: in future we may do some version based check. certificate.version

  return certificate.httpsOptions;
}

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

  if (typeof httpsCerts === 'string' && httpsCerts) {
    try {
      const certificate = httpsOptions(options);
      return certificate;
    } catch (error) {
      if (error.message.indexOf('Cannot find module') !== -1) {
        (0, _logger.errorLogger)(`You have given react-cli.app.server.httpsCerts as "${httpsCerts}". \n But it seems that file path have some problem.\n we are unable to locate "${httpsCerts}" in "${process.cwd()}"`);
      } else {
        (0, _logger.errorLogger)(error);
      }

      process.exit(0);
    }
  } else if (globalCertificate) {
    (0, _logger.messageLogger)('global `@zohodesk-private/client_dev_cert` package taken as https certificate');
    return globalCertificate.httpsOptions;
  } else {
    return null;
  }
}