import { CoreModels, UtilsJson, UtilsDotFile, } from 'tnp-core/lib';
import { UtilsDocker } 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.getCommentsKeysAsJsonObject(envPath) || {};
        const envKeyAndCommentsKey = Object.keys(envKeyAndComments);
        for (const key of envKeyAndCommentsKey) {
            const comment = envKeyAndComments[key];
            const tags = UtilsJson.getAttributiesFromComment(comment);
            if (tags.map(c => c.name).includes(CoreModels.tagForTaskName)) {
                const tag = tags.find(c => c.name === CoreModels.tagForTaskName);
                const taskName = tag.value;
                const taskPort = await this.project.registerAndAssignPort(taskName);
                UtilsDotFile.setValueToDotFile(envPath, key, taskPort);
                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 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 UtilsDocker.cleanImagesAndContainersByDockerLabel(UtilsDocker.DOCKER_LABEL_KEY, UtilsDotFile.getValueFromDotFile(this.project.pathFor('.env'), 'COMPOSE_PROJECT_NAME').toString());
        //#endregion
    }
}
//# sourceMappingURL=base-docker.js.map