text: "use strict";
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  var __importDefault = (this && this.__importDefault) || function (mod) {
      return (mod && mod.__esModule) ? mod : { "default": mod };
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.DockerClientInstance = void 0;
  const fs_1 = __importDefault(require("fs"));
  const dockerode_1 = __importDefault(require("dockerode"));
  const docker_client_1 = require("./docker-client");
  const logger_1 = require("./logger");
  const uuid_1 = require("./uuid");
  class DockerClientInstance {
      static getInstance() {
          return __awaiter(this, void 0, void 0, function* () {
              if (!this.instance) {
                  this.instance = this.createInstance();
              }
              return this.instance;
          });
      }
      static createInstance() {
          return __awaiter(this, void 0, void 0, function* () {
              logger_1.log.debug("Creating new DockerClient");
              const dockerode = new dockerode_1.default();
              const host = yield this.getHost(dockerode);
              return new docker_client_1.DockerodeClient(host, dockerode, new uuid_1.RandomUuid().nextUuid());
          });
      }
      static getHost(dockerode) {
          return __awaiter(this, void 0, void 0, function* () {
              const modem = dockerode.modem;
              if (process.env.DOCKER_HOST) {
                  logger_1.log.info(`Detected DOCKER_HOST environment variable: ${process.env.DOCKER_HOST}`);
              }
              if (modem.host) {
                  const host = modem.host;
                  logger_1.log.info(`Using Docker host from modem: ${host}`);
                  return host;
              }
              else {
                  const socketPath = modem.socketPath;
                  if (!fs_1.default.existsSync("/.dockerenv")) {
                      const host = "localhost";
                      logger_1.log.info(`Using default Docker host: ${host}, socket path: ${socketPath}`);
                      return host;
                  }
                  else {
                      const network = yield dockerode.getNetwork("bridge").inspect();
                      if (!network.IPAM || !network.IPAM.Config) {
                          const host = "localhost";
                          logger_1.log.info(`Using Docker host from gateway without IPAM: ${host}, socket path: ${socketPath}`);
                          return host;
                      }
                      else {
                          const gateways = network.IPAM.Config.filter((config) => !!config.Gateway);
                          if (gateways.length > 0) {
                              const host = gateways[0].Gateway;
                              logger_1.log.info(`Using Docker host from gateway with IPAM: ${host}, socket path: ${socketPath}`);
                              return host;
                          }
                          else {
                              const host = "localhost";
                              logger_1.log.info(`Using Docker host from gateway with IPAM without gateway: ${host}, socket path: ${socketPath}`);
                              return host;
                          }
                      }
                  }
              }
          });
      }
  }
  exports.DockerClientInstance = DockerClientInstance;
  