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); }
};
/**
 * Copyright (C) 2024 Hedera Hashgraph, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the ""License"");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an ""AS IS"" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { Flags as flags } from '../../commands/flags.js';
import { ConfigManager } from '../config_manager.js';
import { K8 } from '../k8.js';
import { SoloLogger } from '../logging.js';
import { IntervalLease } from './interval_lease.js';
import { LeaseHolder } from './lease_holder.js';
import { LeaseAcquisitionError } from './lease_errors.js';
import { inject, injectable } from 'tsyringe-neo';
import { patchInject } from '../container_helper.js';
/**
 * Manages the acquisition and renewal of leases.
 */
let LeaseManager = class LeaseManager {
    _renewalService;
    _logger;
    k8;
    configManager;
    /**
     * Creates a new lease manager.
     *
     * @param _renewalService - the lease renewal service.
     * @param _logger - the logger.
     * @param k8 - the Kubernetes client.
     * @param configManager - the configuration manager.
     */
    constructor(_renewalService, _logger, k8, configManager) {
        this._renewalService = _renewalService;
        this._logger = _logger;
        this.k8 = k8;
        this.configManager = configManager;
        this._renewalService = patchInject(_renewalService, 'LeaseRenewalService', this.constructor.name);
        this._logger = patchInject(_logger, SoloLogger, this.constructor.name);
        this.k8 = patchInject(k8, K8, this.constructor.name);
        this.configManager = patchInject(configManager, ConfigManager, this.constructor.name);
    }
    /**
     * Creates a new lease. This lease is not acquired until the `acquire` method is called.
     *
     * @returns a new lease instance.
     */
    async create() {
        return new IntervalLease(this.k8, this._renewalService, LeaseHolder.default(), await this.currentNamespace());
    }
    /**
     * Retrieves the renewal service implementation.
     *
     * @returns the lease renewal service.
     */
    get renewalService() {
        return this._renewalService;
    }
    /**
     * Retrieves the logger instance.
     *
     * @returns the logger.
     */
    get logger() {
        return this._logger;
    }
    /**
     * Retrieves the user or configuration supplied namespace to use for lease acquisition.
     *
     * @returns the namespace to use for lease acquisition or null if no namespace is specified.
     * @throws LeaseAcquisitionError if the namespace does not exist and cannot be created.
     */
    async currentNamespace() {
        const deploymentNamespace = this.configManager.getFlag(flags.namespace);
        const clusterSetupNamespace = this.configManager.getFlag(flags.clusterSetupNamespace);
        if (!deploymentNamespace && !clusterSetupNamespace) {
            return null;
        }
        const namespace = deploymentNamespace ? deploymentNamespace : clusterSetupNamespace;
        if (!(await this.k8.hasNamespace(namespace))) {
            await this.k8.createNamespace(namespace);
            if (!(await this.k8.hasNamespace(namespace))) {
                throw new LeaseAcquisitionError(`failed to create the '${namespace}' namespace`);
            }
        }
        return namespace;
    }
};
LeaseManager = __decorate([
    injectable(),
    __param(0, inject('LeaseRenewalService')),
    __param(1, inject(SoloLogger)),
    __param(2, inject(K8)),
    __param(3, inject(ConfigManager)),
    __metadata("design:paramtypes", [Object, SoloLogger,
        K8,
        ConfigManager])
], LeaseManager);
export { LeaseManager };
//# sourceMappingURL=lease_manager.js.map