// 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 KindDependencyManager_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';
const KIND_RELEASE_BASE_URL = 'https://kind.sigs.k8s.io/dl';
const KIND_ARTIFACT_TEMPLATE = '%s/kind-%s-%s';
let KindDependencyManager = KindDependencyManager_1 = class KindDependencyManager extends BaseDependencyManager {
    downloader;
    installationDirectory;
    kindVersion;
    constructor(downloader, installationDirectory, osPlatform, osArch, kindVersion) {
        // Patch injected values to handle undefined values
        installationDirectory = patchInject(installationDirectory, InjectTokens.KindInstallationDir, KindDependencyManager_1.name);
        osPlatform = patchInject(osPlatform, InjectTokens.OsPlatform, KindDependencyManager_1.name);
        osArch = patchInject(osArch, InjectTokens.OsArch, KindDependencyManager_1.name);
        kindVersion = patchInject(kindVersion, InjectTokens.KindVersion, KindDependencyManager_1.name);
        downloader = patchInject(downloader, InjectTokens.PackageDownloader, KindDependencyManager_1.name);
        // Call the base constructor with the Kind-specific parameters
        super(downloader, installationDirectory, osPlatform, osArch, kindVersion || version.KIND_VERSION, constants.KIND, KIND_RELEASE_BASE_URL);
        this.downloader = downloader;
        this.installationDirectory = installationDirectory;
        this.kindVersion = kindVersion;
    }
    /**
     * Get the Kind artifact name based on version, OS, and architecture
     */
    getArtifactName() {
        return util.format(KIND_ARTIFACT_TEMPLATE, this.getRequiredVersion(), this.osPlatform, this.osArch);
    }
    async getVersion(executablePath) {
        // The retry logic is to handle potential transient issues with the command execution
        // The command `kind --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 kind version', error);
            }
        }
        throw new SoloError('Failed to check kind version');
    }
    getDownloadURL() {
        return `${this.downloadBaseUrl}/${this.artifactName}`;
    }
    /**
     * Handle any post-download processing before copying to destination
     * Child classes can override this for custom extraction or processing
     */
    async processDownloadedPackage(packageFilePath, temporaryDirectory) {
        // Default implementation - just return the downloaded file path
        // Child classes can override for extraction or other processing
        return packageFilePath;
    }
    getChecksumURL() {
        return `${this.downloadURL}.sha256sum`;
    }
};
KindDependencyManager = KindDependencyManager_1 = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.PackageDownloader)),
    __param(1, inject(InjectTokens.KindInstallationDir)),
    __param(2, inject(InjectTokens.OsPlatform)),
    __param(3, inject(InjectTokens.OsArch)),
    __param(4, inject(InjectTokens.KindVersion)),
    __metadata("design:paramtypes", [PackageDownloader, String, String, String, String])
], KindDependencyManager);
export { KindDependencyManager };
//# sourceMappingURL=kind-dependency-manager.js.map