// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var PodmanDependencyManager_1;
import * as constants from '../constants.js';
import * as version from '../../../version.js';
import { inject, injectable } from 'tsyringe-neo';
import { patchInject } from '../dependency-injection/container-helper.js';
import { InjectTokens } from '../dependency-injection/inject-tokens.js';
import { BaseDependencyManager } from './base-dependency-manager.js';
import { PackageDownloader } from '../package-downloader.js';
import util from 'node:util';
import { SoloError } from '../errors/solo-error.js';
import path from 'node:path';
import fs from 'node:fs';
import { Zippy } from '../zippy.js';
import { PodmanMode } from '../../types/index.js';
import { PathEx } from '../../business/utils/path-ex.js';
const PODMAN_RELEASES_LIST_URL = 'https://api.github.com/repos/containers/podman/releases';
let PodmanDependencyManager = PodmanDependencyManager_1 = class PodmanDependencyManager extends BaseDependencyManager {
    downloader;
    installationDirectory;
    podmanVersion;
    zippy;
    helpersDirectory;
    checksum;
    releaseBaseUrl;
    artifactFileName;
    artifactVersion;
    constructor(downloader, installationDirectory, osPlatform, osArch, podmanVersion, zippy, helpersDirectory) {
        // Patch injected values to handle undefined values
        installationDirectory = patchInject(installationDirectory, InjectTokens.PodmanInstallationDir, PodmanDependencyManager_1.name);
        osPlatform = patchInject(osPlatform, InjectTokens.OsPlatform, PodmanDependencyManager_1.name);
        osArch = patchInject(osArch, InjectTokens.OsArch, PodmanDependencyManager_1.name);
        podmanVersion = patchInject(podmanVersion, InjectTokens.PodmanVersion, PodmanDependencyManager_1.name);
        downloader = patchInject(downloader, InjectTokens.PackageDownloader, PodmanDependencyManager_1.name);
        zippy = patchInject(zippy, InjectTokens.Zippy, PodmanDependencyManager_1.name);
        helpersDirectory = patchInject(helpersDirectory, InjectTokens.PodmanDependenciesInstallationDir, PodmanDependencyManager_1.name);
        // Call the base constructor with the podman-specific parameters
        super(downloader, installationDirectory, osPlatform, osArch, podmanVersion || version.PODMAN_VERSION, constants.PODMAN, '');
        this.downloader = downloader;
        this.installationDirectory = installationDirectory;
        this.podmanVersion = podmanVersion;
        this.zippy = zippy;
        this.helpersDirectory = helpersDirectory;
    }
    /**
     * Get the Podman artifact name based on version, OS, and architecture
     */
    getArtifactName() {
        return util.format(this.artifactFileName, this.getRequiredVersion(), this.osPlatform, this.osArch);
    }
    get mode() {
        return this.osPlatform === constants.OS_LINUX ? PodmanMode.ROOTFUL : PodmanMode.VIRTUAL_MACHINE;
    }
    async getVersion(executablePath) {
        // The retry logic is to handle potential transient issues with the command execution
        // The command `podman --version` was sometimes observed to return an empty output in the CI environment
        const maxAttempts = 3;
        for (let attempt = 1; attempt <= maxAttempts; attempt++) {
            try {
                const output = await this.run(`${executablePath} --version`);
                if (output.length > 0) {
                    const match = output[0].trim().match(/(\d+\.\d+\.\d+)/);
                    return match[1];
                }
            }
            catch (error) {
                throw new SoloError('Failed to check podman version', error);
            }
        }
        throw new SoloError('Failed to check podman version');
    }
    /**
     * Fetches the latest release information from GitHub API
     * @returns Promise with the release base URL, asset name, digest, and version
     */
    async fetchLatestReleaseInfo() {
        try {
            // Make a GET request to GitHub API using fetch
            const response = await fetch(PODMAN_RELEASES_LIST_URL, {
                method: 'GET', // Changed from HEAD to GET to retrieve the body
                headers: {
                    'User-Agent': constants.SOLO_USER_AGENT_HEADER,
                    Accept: 'application/vnd.github.v3+json', // Explicitly request GitHub API v3 format
                },
            });
            if (!response.ok) {
                throw new SoloError(`GitHub API request failed with status ${response.status}`);
            }
            // Parse the JSON response
            const releases = await response.json();
            if (!releases || releases.length === 0) {
                throw new SoloError('No releases found');
            }
            // Get the latest release
            const latestRelease = releases[0];
            const version = latestRelease.tag_name.replace(/^v/, ''); // Remove 'v' prefix if present
            // Normalize platform/arch for asset matching
            const platform = this.osPlatform === constants.OS_WIN32 ? constants.OS_WINDOWS : this.osPlatform;
            const arch = this.getArch();
            // Construct asset pattern based on platform
            let assetPattern;
            if (platform === constants.OS_WINDOWS) {
                // Windows
                assetPattern = new RegExp(String.raw `podman-remote-release-windows_${arch}\.zip$`);
            }
            else if (platform === 'darwin') {
                // macOS
                assetPattern = new RegExp(String.raw `podman-remote-release-darwin_${arch}\.zip$`);
            }
            else {
                // Linux
                assetPattern = new RegExp(String.raw `podman-remote-static-linux_${arch}\.tar\.gz$`);
            }
            // Find the matching asset
            const matchingAsset = latestRelease.assets.find(asset => assetPattern.test(asset.browser_download_url));
            if (!matchingAsset) {
                throw new SoloError(`No matching asset found for ${platform}-${arch}`);
            }
            // Get the digest from the shasums file
            const checksum = matchingAsset.digest
                ? matchingAsset.digest.replace('sha256:', '')
                : '0000000000000000000000000000000000000000000000000000000000000000';
            // Construct the release base URL (removing the filename from the download URL)
            const downloadUrl = matchingAsset.browser_download_url.slice(0, Math.max(0, matchingAsset.browser_download_url.lastIndexOf('/')));
            return {
                downloadUrl,
                assetName: matchingAsset.name,
                checksum,
                version,
            };
        }
        catch (error) {
            if (error instanceof SoloError) {
                throw error;
            }
            throw new SoloError('Failed to parse GitHub API response', error);
        }
    }
    // Podman should only be installed if Docker is not already present on the client system
    async shouldInstall() {
        // Check if Podman is explicitly requested via environment variable
        if (process.env.FORCE_PODMAN === 'true') {
            return true;
        }
        // Determine if Docker is already installed
        try {
            await this.run(`${constants.DOCKER} --version`);
            return false;
        }
        catch {
            return true;
        }
    }
    async preInstall() {
        const latestReleaseInfo = await this.fetchLatestReleaseInfo();
        this.checksum = latestReleaseInfo.checksum;
        this.releaseBaseUrl = latestReleaseInfo.downloadUrl;
        this.artifactFileName = latestReleaseInfo.assetName;
        this.artifactVersion = latestReleaseInfo.version;
    }
    getDownloadURL() {
        return `${this.releaseBaseUrl}/${this.artifactFileName}`;
    }
    /**
     * Handle any post-download processing before copying to destination
     * Child classes can override this for custom extraction or processing
     */
    async processDownloadedPackage(packageFilePath, temporaryDirectory) {
        // Extract the archive based on file extension
        if (packageFilePath.endsWith('.zip')) {
            this.zippy.unzip(packageFilePath, temporaryDirectory);
        }
        else {
            this.zippy.untar(packageFilePath, temporaryDirectory);
        }
        let binDirectory;
        if (this.osPlatform === constants.OS_LINUX) {
            binDirectory = path.join(temporaryDirectory, 'bin');
            const arch = this.getArch();
            fs.renameSync(path.join(binDirectory, `podman-remote-static-linux_${arch}`), path.join(binDirectory, constants.PODMAN));
        }
        else {
            // Find the Podman executable inside the extracted directory
            binDirectory = path.join(temporaryDirectory, `${constants.PODMAN}-${this.artifactVersion}`, 'usr', 'bin');
        }
        return fs.readdirSync(binDirectory).map((file) => path.join(binDirectory, file));
    }
    getChecksumURL() {
        return this.checksum;
    }
    /**
     * Create a custom containers.conf file for Podman and set the CONTAINERS_CONF env variable
     * @private
     */
    async setupConfig() {
        // Create the containers.conf file from the template
        const configDirectory = path.join(constants.SOLO_HOME_DIR, 'config');
        if (!fs.existsSync(configDirectory)) {
            fs.mkdirSync(configDirectory, { recursive: true });
        }
        const templatesDirectory = PathEx.join(constants.SOLO_HOME_DIR, 'cache', 'templates');
        const templatePath = path.join(templatesDirectory, 'podman', 'containers.conf');
        const destinationPath = path.join(configDirectory, 'containers.conf');
        let configContent = fs.readFileSync(templatePath, 'utf8');
        configContent = configContent.replace('$HELPER_BINARIES_DIR', this.helpersDirectory.replaceAll('\\', '/'));
        fs.writeFileSync(destinationPath, configContent, 'utf-8');
        process.env.CONTAINERS_CONF = destinationPath;
    }
};
PodmanDependencyManager = PodmanDependencyManager_1 = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.PackageDownloader)),
    __param(1, inject(InjectTokens.PodmanInstallationDir)),
    __param(2, inject(InjectTokens.OsPlatform)),
    __param(3, inject(InjectTokens.OsArch)),
    __param(4, inject(InjectTokens.PodmanVersion)),
    __param(5, inject(InjectTokens.Zippy)),
    __param(6, inject(InjectTokens.PodmanDependenciesInstallationDir)),
    __metadata("design:paramtypes", [PackageDownloader, String, String, String, String, Zippy, String])
], PodmanDependencyManager);
export { PodmanDependencyManager };
//# sourceMappingURL=podman-dependency-manager.js.map