/* eslint-disable perfectionist/sort-named-imports */
/* eslint-disable unicorn/no-array-callback-reference */
/* eslint-disable unicorn/no-array-for-each */
/* eslint-disable perfectionist/sort-named-exports */
/* eslint-disable perfectionist/sort-objects */
import Conf from 'conf';
import fs from 'fs';
import log from './log.js';
import paths from './paths.js';
const { campfireBuildWorkflowTemplatePath, campfireDeleteWorkflowPath, userDeleteWorkflowPath, userUpdateWorkflowPath, campfireUpdateWorkflowPath, githubFolderPath, workflowFolderPath, userCreateWorkflowPath, actionFolderPaths, campfireActionTemplatePaths, userActionPaths, } = paths;
const DEFAULT_NAME = 'campfire';
const config = new Conf({ projectName: DEFAULT_NAME });
const createFolder = path => {
    if (!fs.existsSync(path)) {
        fs.mkdirSync(path);
        process.stdout.write('campfire created folder at: ');
        log.info(path.replace(process.cwd(), '.'));
    }
};
// create necessary directory structure in project repo to copy workflow files into
const createWorkflowDir = () => {
    createFolder(githubFolderPath);
    createFolder(workflowFolderPath);
    actionFolderPaths.forEach(createFolder);
};
const populateBuildWorkflow = () => {
    let buildWorkflowFile = fs.readFileSync(userCreateWorkflowPath, 'utf8');
    buildWorkflowFile = buildWorkflowFile.replace('USER_EXECUTION_ROLE_ARN', config.get("EXECUTION_ROLE_ARN"));
    buildWorkflowFile = buildWorkflowFile.replace('USER_DOMAIN', config.get('USER_DOMAIN'));
    buildWorkflowFile = buildWorkflowFile.replace('USER_DEFAULT_SUBNET', config.get('DEFAULT_SUBNET_NAME'));
    buildWorkflowFile = buildWorkflowFile.replace('USER_SECURITY_GROUP', config.get('SERVICE_SECURITY_GROUP'));
    buildWorkflowFile = buildWorkflowFile.replace('USER_CLUSTER_NAME', config.get('CLUSTER_NAME'));
    buildWorkflowFile = buildWorkflowFile.replace('USER_AWS_REGION', config.get('AWS_REGION'));
    fs.writeFileSync(userCreateWorkflowPath, buildWorkflowFile);
};
const populateUpdateWorkflow = () => {
    let updateWorkflowFile = fs.readFileSync(userUpdateWorkflowPath, 'utf8');
    updateWorkflowFile = updateWorkflowFile.replace('USER_EXECUTION_ROLE_ARN', config.get("EXECUTION_ROLE_ARN"));
    updateWorkflowFile = updateWorkflowFile.replace('USER_DOMAIN', config.get('USER_DOMAIN'));
    updateWorkflowFile = updateWorkflowFile.replace('USER_DEFAULT_SUBNET', config.get('DEFAULT_SUBNET_NAME'));
    updateWorkflowFile = updateWorkflowFile.replace('USER_SECURITY_GROUP', config.get('SERVICE_SECURITY_GROUP'));
    updateWorkflowFile = updateWorkflowFile.replace('USER_AWS_REGION', config.get('AWS_REGION'));
    fs.writeFileSync(userUpdateWorkflowPath, updateWorkflowFile);
};
const populateTeardownWorkflow = () => {
    let deleteWorkflowFile = fs.readFileSync(userDeleteWorkflowPath, 'utf8');
    deleteWorkflowFile = deleteWorkflowFile.replace('USER_CLUSTER_NAME', config.get('CLUSTER_NAME'));
    deleteWorkflowFile = deleteWorkflowFile.replace('USER_S3_BUCKET', config.get('S3_BUCKET_NAME'));
    deleteWorkflowFile = deleteWorkflowFile.replace('USER_AWS_REGION', config.get('AWS_REGION'));
    fs.writeFileSync(userDeleteWorkflowPath, deleteWorkflowFile);
};
const populateWorkflows = () => {
    populateBuildWorkflow();
    populateTeardownWorkflow();
    populateUpdateWorkflow();
};
const copyWorkflowFilesToRepo = () => {
    // copy the template file into the project workflows directory
    fs.copyFileSync(campfireBuildWorkflowTemplatePath, userCreateWorkflowPath);
    fs.copyFileSync(campfireDeleteWorkflowPath, userDeleteWorkflowPath);
    fs.copyFileSync(campfireUpdateWorkflowPath, userUpdateWorkflowPath);
    campfireActionTemplatePaths.forEach((campfireFile, index) => {
        const userFile = userActionPaths[index];
        fs.copyFileSync(campfireFile, userFile);
    });
};
export { populateWorkflows, createWorkflowDir, copyWorkflowFilesToRepo, };
