// SPDX-License-Identifier: Apache-2.0
import { lt, SemVer } from 'semver';
import { GetClustersRequest } from '../request/get/get-clusters-request.js';
import { KindCluster } from '../model/kind-cluster.js';
import { KindExecutionBuilder } from '../execution/kind-execution-builder.js';
import { VersionRequest } from '../request/version-request.js';
import { KindVersion } from '../model/kind-version.js';
import { ClusterCreateRequest } from '../request/cluster/cluster-create-request.js';
import { ClusterCreateOptionsBuilder } from '../model/create-cluster/create-cluster-options-builder.js';
import { ClusterCreateResponse } from '../model/create-cluster/cluster-create-response.js';
import { ClusterDeleteResponse } from '../model/delete-cluster/cluster-delete-response.js';
import { ClusterDeleteOptionsBuilder } from '../model/delete-cluster/cluster-delete-options-builder.js';
import { ClusterDeleteRequest } from '../request/cluster/cluster-delete-request.js';
import { BuildNodeImagesResponse } from '../model/build-node-images/build-node-images-response.js';
import { BuildNodeImagesRequest } from '../request/build/build-node-images-request.js';
import { ExportLogsRequest } from '../request/export/export-logs-request.js';
import { ExportLogsResponse } from '../model/export-logs/export-logs-response.js';
import { ExportLogsOptionsBuilder } from '../model/export-logs/export-logs-options-builder.js';
import { ExportKubeConfigOptionsBuilder } from '../model/export-kubeconfig/export-kubeconfig-options-builder.js';
import { ExportKubeConfigRequest } from '../request/export/export-kubeconfig-request.js';
import { ExportKubeConfigResponse } from '../model/export-kubeconfig/export-kubeconfig-response.js';
import { GetNodesResponse } from '../model/get-nodes/get-nodes-response.js';
import { GetNodesOptionsBuilder } from '../model/get-nodes/get-nodes-options-builder.js';
import { GetNodesRequest } from '../request/get/get-nodes-request.js';
import { GetKubeConfigOptionsBuilder } from '../model/get-kubeconfig/get-kubeconfig-options-builder.js';
import { GetKubeConfigRequest } from '../request/get/get-kubeconfig-request.js';
import { GetKubeConfigResponse } from '../model/get-kubeconfig/get-kubeconfig-response.js';
import { LoadDockerImageRequest } from '../request/load/docker-image-request.js';
import { LoadDockerImageResponse } from '../model/load-docker-image/load-docker-image-response.js';
import { LoadDockerImageOptionsBuilder } from '../model/load-docker-image/load-docker-image-options-builder.js';
import { LoadImageArchiveOptionsBuilder } from '../model/load-image-archive/load-image-archive-options-builder.js';
import { LoadImageArchiveResponse } from '../model/load-image-archive/load-image-archive-response.js';
import { LoadImageArchiveRequest } from '../request/load/image-archive-request.js';
import { KIND_VERSION } from '../../../../version.js';
import { KindVersionRequirementException } from '../errors/kind-version-requirement-exception.js';
export class DefaultKindClient {
    executable;
    static minimumVersion = new SemVer(KIND_VERSION);
    constructor(executable) {
        this.executable = executable;
        if (!executable || !executable.trim()) {
            throw new Error('executable must not be blank');
        }
        this.executable = executable;
    }
    async checkVersion() {
        const version = await this.version();
        if (lt(version, DefaultKindClient.minimumVersion)) {
            throw new KindVersionRequirementException(`The Kind CLI version ${version} is lower than the minimum required version ${DefaultKindClient.minimumVersion}.`);
        }
    }
    async version() {
        const request = new VersionRequest();
        const builder = new KindExecutionBuilder();
        builder.executable(this.executable);
        request.apply(builder);
        const execution = builder.build();
        if (execution instanceof Promise) {
            throw new TypeError('Unexpected async execution');
        }
        const versionClass = KindVersion;
        const result = await execution.responseAs(versionClass);
        if (!(result instanceof KindVersion)) {
            throw new TypeError('Unexpected response type');
        }
        return result.getVersion();
    }
    async createCluster(clusterName, options) {
        const builder = ClusterCreateOptionsBuilder.from(options);
        builder.name(clusterName);
        return this.executeAsync(new ClusterCreateRequest(builder.build()), ClusterCreateResponse);
    }
    async deleteCluster(clusterName, options) {
        const builder = ClusterDeleteOptionsBuilder.from(options);
        builder.name(clusterName);
        return this.executeAsync(new ClusterDeleteRequest(builder.build()), ClusterDeleteResponse);
    }
    async buildNodeImage(options) {
        return this.executeAsync(new BuildNodeImagesRequest(options), BuildNodeImagesResponse);
    }
    async exportLogs(clusterName) {
        const options = ExportLogsOptionsBuilder.builder().name(clusterName).build();
        return this.executeAsync(new ExportLogsRequest(options), ExportLogsResponse);
    }
    async exportKubeConfig(clusterName) {
        const options = ExportKubeConfigOptionsBuilder.builder().name(clusterName).build();
        return this.executeAsync(new ExportKubeConfigRequest(options), ExportKubeConfigResponse);
    }
    async getClusters() {
        return this.executeAsList(new GetClustersRequest(), KindCluster);
    }
    async getNodes(contextName, options) {
        const builder = GetNodesOptionsBuilder.from(options).name(contextName);
        return this.executeAsync(new GetNodesRequest(builder.build()), GetNodesResponse);
    }
    async getKubeConfig(contextName, options) {
        const builder = GetKubeConfigOptionsBuilder.from(options).name(contextName);
        return this.executeAsync(new GetKubeConfigRequest(builder.build()), GetKubeConfigResponse);
    }
    async loadDockerImage(imageName, options) {
        const builder = LoadDockerImageOptionsBuilder.from(options).imageName(imageName);
        return this.executeAsync(new LoadDockerImageRequest(builder.build()), LoadDockerImageResponse);
    }
    async loadImageArchive(imageName, options) {
        const builder = LoadImageArchiveOptionsBuilder.from(options).name(imageName);
        return this.executeAsync(new LoadImageArchiveRequest(builder.build()), LoadImageArchiveResponse);
    }
    /**
     * Executes the given request and returns the response as the given class.
     * The request is executed using the default namespace.
     *
     * @param request - The request to execute
     * @param responseClass - The class of the response
     * @returns The response
     */
    async executeAsync(request, responseClass) {
        return this.executeInternal(undefined, request, responseClass, async (b) => {
            const response = await b.responseAs(responseClass);
            return response;
        });
    }
    /**
     * Executes the given request and returns the response as a list of the given class.
     * The request is executed using the default namespace.
     *
     * @param request - The request to execute
     * @param responseClass - The class of the response
     * @returns A list of response objects
     */
    async executeAsList(request, responseClass) {
        return this.executeInternal(undefined, request, responseClass, async (b) => {
            const response = await b.responseAsList(responseClass);
            return response;
        });
    }
    async executeInternal(namespace, request, responseClass, responseFunction) {
        if (namespace && !namespace.trim()) {
            throw new Error('namespace must not be blank');
        }
        const builder = new KindExecutionBuilder();
        builder.executable(this.executable);
        request.apply(builder);
        const execution = builder.build();
        return responseFunction(execution, responseClass);
    }
}
//# sourceMappingURL=default-kind-client.js.map