"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var docker_exports = {};
__export(docker_exports, {
  Docker: () => Docker
});
module.exports = __toCommonJS(docker_exports);
var import_promises = require("node:fs/promises");
var import_dockerode = __toESM(require("dockerode"), 1);
var import_composition = require("./composition.js");
var import_container = require("./container.js");
var import_errors = require("./errors.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class Docker {
  #intf = new import_dockerode.default();
  get intf() {
    return this.#intf;
  }
  /**
   * Build a composition of multiple containers.
   */
  compose(name, config) {
    return new import_composition.Composition(this, name, config);
  }
  /**
   * Ensure a container doesn't exist regardless of current state.
   */
  async erase(name) {
    const info = await this.containerStatus(name);
    if (info) {
      const ct = (0, import_container.Container)(this, this.#intf.getContainer(info.id));
      try {
        await ct.remove(true);
      } catch (e) {
        import_errors.DockerError.accept(e, 404, 409);
      }
      return ct;
    }
  }
  /**
   * Obtain a running {@link Container}.  Connects to existing container or creates a new container.
   */
  async open(options) {
    const info = await this.containerStatus(options.name);
    if (info) {
      const ct2 = (0, import_container.Container)(this, this.#intf.getContainer(info.id));
      if (!info.isRunning) {
        await ct2.start();
      }
      return ct2;
    }
    const ct = await this.create(options);
    await ct.start();
    return ct;
  }
  async containerStatus(name) {
    try {
      const inspect = await import_errors.DockerError.adapt(this.#intf.getContainer(name).inspect());
      return {
        name,
        id: inspect.Id,
        isRunning: inspect.State.Status === "running"
      };
    } catch (e) {
      import_errors.DockerError.accept(e, 404);
    }
  }
  async create(config) {
    return (0, import_container.Container)(this, config);
  }
  async start(config) {
    const ct = await this.create(config);
    await import_errors.DockerError.adapt(ct.start());
    return ct;
  }
  async pull(nameAndTag, platform) {
    const imageConfig = {
      fromImage: nameAndTag
    };
    if (platform !== void 0) {
      imageConfig.platform = platform;
    }
    const progress = await import_errors.DockerError.adapt(this.#intf.createImage(imageConfig));
    await new Promise((resolve, reject) => {
      this.#intf.modem.followProgress(progress, (error) => {
        if (error) {
          reject(import_errors.DockerError.translate(error));
        }
        resolve();
      });
    });
  }
  async buildImage(name, path) {
    const files = await (0, import_promises.readdir)(path);
    const stream = await import_errors.DockerError.adapt(
      this.#intf.buildImage(
        {
          context: path,
          src: files
        },
        {
          t: name
        }
      )
    );
    await new Promise((resolve, reject) => {
      this.#intf.modem.followProgress(stream, (error, result) => {
        if (error) {
          reject(import_errors.DockerError.translate(error));
        }
        const finalMessage = result[result.length - 1];
        const errorMessage = finalMessage?.error ?? finalMessage?.errorDetail?.message;
        if (errorMessage) {
          reject(new Error(errorMessage));
        }
        resolve();
      });
    });
  }
}
//# sourceMappingURL=docker.js.map
