"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.volumeService = void 0;
const spawn_1 = require("../utils/spawn");
function runDocker(args) {
    const result = (0, spawn_1.spawnCommand)('docker', args, {
        encoding: 'utf8',
        timeout: 30000
    });
    if (result.error) {
        throw result.error;
    }
    if (result.status === null) {
        throw new Error('docker command not found. Is Docker installed?');
    }
    return result;
}
exports.volumeService = {
    createVolume(name) {
        const result = runDocker(['volume', 'create', name]);
        if (result.status !== 0) {
            throw new Error(`Failed to create volume '${name}': ${result.stderr?.trim() || 'unknown error'}`);
        }
    },
    exists(name) {
        try {
            const result = runDocker(['volume', 'inspect', name]);
            return result.status === 0 && Boolean(result.stdout?.trim());
        }
        catch {
            return false;
        }
    },
    removeVolume(name) {
        const result = runDocker(['volume', 'rm', name]);
        if (result.status !== 0) {
            throw new Error(`Failed to remove volume '${name}': ${result.stderr?.trim() || 'unknown error'}`);
        }
    },
    list() {
        const result = runDocker(['volume', 'ls', '--no-trunc', '--format', '{{json .}}']);
        if (result.status !== 0) {
            throw new Error(`Failed to list volumes: ${result.stderr?.trim() || 'unknown error'}`);
        }
        return (result.stdout ?? '')
            .trim()
            .split(/\r?\n/)
            .filter(Boolean)
            .map((line) => JSON.parse(line))
            .map((item) => ({
            Name: item.Name ?? '',
            Driver: item.Driver ?? '',
            Mountpoint: item.Mountpoint ?? ''
        }));
    }
};
//# sourceMappingURL=docker-volume.js.map