import { CoreModels__NS__tagForTaskName, UtilsDotFile__NS__getCommentsKeysAsJsonObject, UtilsDotFile__NS__getValueFromDotFile, UtilsDotFile__NS__setCommentToKeyInDotFile, UtilsDotFile__NS__setValueToDotFile, UtilsJson__NS__getAttributiesFromComment } from 'tnp-core/lib-prod';
import { UtilsDocker__NS__cleanImagesAndContainersByDockerLabel, UtilsDocker__NS__DOCKER_LABEL_KEY, UtilsDocker__NS__getDockerComposeActionChildProcess } from '../../utils';
import { BaseFeatureForProject } from './base-feature-for-project';
//#endregion
export class BaseDocker extends 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 = UtilsDotFile__NS__getCommentsKeysAsJsonObject(envPath) || {};
        const envKeyAndCommentsKey = Object.keys(envKeyAndComments);
        for (const key of envKeyAndCommentsKey) {
            const comment = envKeyAndComments[key];
            const tags = UtilsJson__NS__getAttributiesFromComment(comment);
            if (tags.map(c => c.name).includes(CoreModels__NS__tagForTaskName)) {
                const tag = tags.find(c => c.name === CoreModels__NS__tagForTaskName);
                const taskName = tag.value;
                const taskPort = await this.project.registerAndAssignPort(taskName);
                UtilsDotFile__NS__setValueToDotFile(envPath, key, taskPort);
                UtilsDotFile__NS__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 UtilsDocker__NS__getDockerComposeActionChildProcess(action, options);
        //#endregion
    }
    //#endregion
    //#region remove all images by COMPOSE_PROJECT_NAME from .env
    async removeAllImagesBy_Env_COMPOSE_PROJECT_NAME() {
        //#region @backendFunc
        await UtilsDocker__NS__cleanImagesAndContainersByDockerLabel(UtilsDocker__NS__DOCKER_LABEL_KEY, UtilsDotFile__NS__getValueFromDotFile(this.project.pathFor('.env'), 'COMPOSE_PROJECT_NAME').toString());
        //#endregion
    }
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/classes/base-docker.js.map