// 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 KubectlDependencyManager_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 KUBECTL_RELEASE_BASE_URL = 'https://dl.k8s.io/release';
const KUBECTL_ARTIFACT_TEMPLATE = '%s/bin/%s/%s/kubectl';
const KUBECTL_WINDOWS_ARTIFACT_TEMPLATE = '%s/bin/%s/%s/kubectl.exe';
let KubectlDependencyManager = KubectlDependencyManager_1 = class KubectlDependencyManager extends BaseDependencyManager {
    downloader;
    installationDirectory;
    kubectlVersion;
    constructor(downloader, installationDirectory, osPlatform, osArch, kubectlVersion) {
        // Patch injected values to handle undefined values
        installationDirectory = patchInject(installationDirectory, InjectTokens.KubectlInstallationDir, KubectlDependencyManager_1.name);
        osPlatform = patchInject(osPlatform, InjectTokens.OsPlatform, KubectlDependencyManager_1.name);
        osArch = patchInject(osArch, InjectTokens.OsArch, KubectlDependencyManager_1.name);
        kubectlVersion = patchInject(kubectlVersion, InjectTokens.KubectlVersion, KubectlDependencyManager_1.name);
        downloader = patchInject(downloader, InjectTokens.PackageDownloader, KubectlDependencyManager_1.name);
        // Call the base constructor with the Kubectl-specific parameters
        super(downloader, installationDirectory, osPlatform, osArch, kubectlVersion || version.KUBECTL_VERSION, constants.KUBECTL, KUBECTL_RELEASE_BASE_URL);
        this.downloader = downloader;
        this.installationDirectory = installationDirectory;
        this.kubectlVersion = kubectlVersion;
    }
    /**
     * Get the Kubectl artifact name based on version, OS, and architecture
     */
    getArtifactName() {
        return util.format(this.osPlatform === constants.OS_WINDOWS ? KUBECTL_WINDOWS_ARTIFACT_TEMPLATE : KUBECTL_ARTIFACT_TEMPLATE, this.getRequiredVersion(), this.osPlatform, this.osArch);
    }
    async getVersion(executablePath) {
        try {
            const output = await this.run(`${executablePath} version --client`);
            if (output.length > 0) {
                for (const line of output) {
                    if (line.trim().startsWith('Client Version')) {
                        const match = line.trim().match(/(\d+\.\d+\.\d+)/);
                        if (match) {
                            return match[1];
                        }
                    }
                }
            }
        }
        catch (error) {
            throw new SoloError('Failed to check kubectl version', error);
        }
        throw new SoloError('Failed to check kubectl 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}.sha256`;
    }
};
KubectlDependencyManager = KubectlDependencyManager_1 = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.PackageDownloader)),
    __param(1, inject(InjectTokens.KubectlInstallationDir)),
    __param(2, inject(InjectTokens.OsPlatform)),
    __param(3, inject(InjectTokens.OsArch)),
    __param(4, inject(InjectTokens.KubectlVersion)),
    __metadata("design:paramtypes", [PackageDownloader, String, String, String, String])
], KubectlDependencyManager);
export { KubectlDependencyManager };
//# sourceMappingURL=kubectl-dependency-manager.js.map