"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseDocker = void 0;
const lib_1 = require("tnp-core/lib");
const utils_1 = require("../../utils");
const base_feature_for_project_1 = require("./base-feature-for-project");
//#endregion
class BaseDocker extends base_feature_for_project_1.BaseFeatureForProject {
    //#region update docker compose ports in .env
    /**
     * Updates ports in .env file based on available ports range.
     */
    async updateDockerComposePorts() {
        //#region @backendFunc
        const envPath = this.project.pathFor('.env');
        const envKeyAndComments = lib_1.UtilsDotFile.getCommentsKeysAsJsonObject(envPath) || {};
        const envKeyAndCommentsKey = Object.keys(envKeyAndComments);
        for (const key of envKeyAndCommentsKey) {
            const comment = envKeyAndComments[key];
            const tags = lib_1.UtilsJson.getAttributiesFromComment(comment);
            if (tags.map(c => c.name).includes(lib_1.CoreModels.tagForTaskName)) {
                const tag = tags.find(c => c.name === lib_1.CoreModels.tagForTaskName);
                const taskName = tag.value;
                const taskPort = await this.project.registerAndAssignPort(taskName);
                lib_1.UtilsDotFile.setValueToDotFile(envPath, key, taskPort);
                lib_1.UtilsDotFile.setCommentToKeyInDotFile(envPath, key, `${tag.name}="${tag.value}"`);
                console.log(`Updating .env "${key}"="${taskPort}" from available ports range.`);
            }
        }
        //#endregion
    }
    //#endregion
    //#region docker compose up / down
    getDockerComposeActionChildProcess(action, options) {
        //#region @backendFunc
        options = options || {};
        options.cwd = options?.cwd || this.project.location;
        return utils_1.UtilsDocker.getDockerComposeActionChildProcess(action, options);
        //#endregion
    }
    //#endregion
    //#region remove all images by COMPOSE_PROJECT_NAME from .env
    async removeAllImagesBy_Env_COMPOSE_PROJECT_NAME() {
        //#region @backendFunc
        await utils_1.UtilsDocker.cleanImagesAndContainersByDockerLabel(utils_1.UtilsDocker.DOCKER_LABEL_KEY, lib_1.UtilsDotFile.getValueFromDotFile(this.project.pathFor('.env'), 'COMPOSE_PROJECT_NAME').toString());
        //#endregion
    }
}
exports.BaseDocker = BaseDocker;
//# sourceMappingURL=base-docker.js.map