import { exec } from 'child_process';
import { promisify } from 'util';
import { TerraformFramework } from './terraformFramework.mjs';
import path from 'path';
import { Logger } from '../logger.mjs';
import fs from 'fs/promises';
export const execAsync = promisify(exec);
/**
 * Support for Terraform framework
 */
export class OpenTofuFramework extends TerraformFramework {
    /**
     * Framework name
     */
    get name() {
        return 'opentofu';
    }
    /**
     * Can this class handle the current project
     * @returns
     */
    async canHandle() {
        // check for any file with .tf, .tf.json, .tofu, or .tofu.json extension
        const files = await fs.readdir(process.cwd());
        const r = files.some((f) => f.endsWith('.tf') ||
            f.endsWith('.tf.json') ||
            f.endsWith('.tofu') ||
            f.endsWith('.tofu.json'));
        if (!r) {
            Logger.verbose(`[${this.logName}] This is not a ${this.logName} project. There are no *.tf, *.tf.json, *.tofu, or *.tofu.json files in ${path.resolve('.')} folder.`);
            return false;
        }
        else {
            // check if Terraform or OpenTofu is installed
            try {
                await execAsync(this.checkInstalledCommand);
                return true;
            }
            catch {
                Logger.verbose(`[${this.logName}] This is not a ${this.logName} project. ${this.logName} is not installed.`);
                return false;
            }
        }
    }
    /**
     * Name of the framework in logs
     */
    get logName() {
        return 'OpenTofu';
    }
    /**
     * Get OpenTofu state CI command
     */
    get stateCommand() {
        return 'tofu show --json';
    }
    /**
     *
     * @returns Get command to check if OpenTodu is installed
     */
    get checkInstalledCommand() {
        return 'tofu --version';
    }
}
export const openTofuFramework = new OpenTofuFramework();
