"use strict";

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

var _address = _interopRequireDefault(require("address"));

var _url = _interopRequireDefault(require("url"));

var _chalk = _interopRequireDefault(require("chalk"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable */
function prepareUrls(protocol, host, port, pathname) {
  const formatUrl = hostname => _url.default.format({
    protocol,
    hostname,
    port,
    pathname: pathname || '/'
  });

  const prettyPrintUrl = hostname => _url.default.format({
    protocol,
    hostname,
    port: _chalk.default.bold(port),
    pathname: pathname || '/'
  });

  const isUnspecifiedHost = host === '0.0.0.0' || host === '::';
  let prettyHost, lanUrlForConfig, lanUrlForTerminal;

  if (isUnspecifiedHost) {
    prettyHost = 'localhost';

    try {
      // This can only return an IPv4 address
      lanUrlForConfig = _address.default.ip();

      if (lanUrlForConfig) {
        // Check if the address is a private ip
        // https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
        if (/^10[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(lanUrlForConfig)) {
          // Address is private, format it for later use
          lanUrlForTerminal = prettyPrintUrl(lanUrlForConfig);
        } else {
          // Address is not private, so we will discard it
          lanUrlForConfig = undefined;
        }
      }
    } catch (_e) {// ignored
    }
  } else {
    prettyHost = host;
  }

  const localUrlForTerminal = prettyPrintUrl(prettyHost);
  const localUrlForBrowser = formatUrl(prettyHost);
  return {
    lanUrlForConfig,
    lanUrlForTerminal,
    localUrlForTerminal,
    localUrlForBrowser
  };
}