import fs from 'fs';
import { TemplateCreator } from './template-creator';
export function keepCase(item) {
    if (typeof item === 'object') {
        if (Array.isArray(item)) {
            return item.map(keepCase);
        }
        if (!Object.prototype.hasOwnProperty.call(item, 'Ref'))
            return Object.keys(item).reduce((prev, next) => ({ ...prev, [next]: keepCase(item[next]) }), { _nocaps: true });
    }
    return item;
}
export class TemplateBuilder {
    aws;
    outputFileName;
    parameterFileName;
    parameters = {};
    outputDir = '';
    conditions = {};
    _templateTransform = template => JSON.stringify(template, null, 2);
    transforms = [];
    constructor(aws, outputFileName, parameterFileName = "parameters.json") {
        this.aws = aws;
        this.outputFileName = outputFileName;
        this.parameterFileName = parameterFileName;
    }
    withCondition(name, condition) {
        this.conditions[name] = condition;
        return this;
    }
    withTransform(transform) {
        this.transforms.push(transform);
        return this;
    }
    params(params) {
        this.parameters = { ...this.parameters, ...params };
        return this;
    }
    envParams(params) {
        this.parameters = { ...this.parameters, ...Object.keys(params).reduce((prev, key) => ({ ...prev, [key]: { type: 'Future', environmentName: params[key] } }), {}) };
        return this;
    }
    outputTo(directory) {
        this.outputDir = directory;
        return this;
    }
    transformTemplate(_templateTransform = template => JSON.stringify(template, null, 2)) {
        this._templateTransform = _templateTransform;
        return this;
    }
    pathTo(file) {
        if (this.outputDir) {
            fs.mkdirSync(this.outputDir, { recursive: true });
        }
        return (this.outputDir ? this.outputDir + '/' : '') + file;
    }
    build(builder) {
        return TemplateCreator.createWithParams(this.aws, (this.parameters ?? {}), this.conditions, builder, this.pathTo(this.outputFileName), this.pathTo(this.parameterFileName), this._templateTransform, this.transforms.length > 0 ? this.transforms : undefined);
    }
    static create(aws, outputFileName = 'template.json', parametersFileName = 'parameters.json') {
        return new TemplateBuilder(aws, outputFileName, parametersFileName);
    }
}
