import { ref } from '../Value';
export class AwsResource {
    type;
    properties;
    attributes;
    logicalName;
    condition;
    dependsOn;
    deletionPolicy;
    constructor(type, properties, attributes = undefined) {
        this.type = type;
        this.properties = properties;
        this.attributes = attributes;
    }
    withLogicalName(logicalName) {
        this.logicalName = logicalName;
        return this;
    }
    withCondition(condition) {
        this.condition = condition;
        return this;
    }
    withDependency(...dependsOn) {
        this.dependsOn = dependsOn;
        return this;
    }
    withDeletionPolicy(deletionPolicy) {
        this.deletionPolicy = deletionPolicy;
        return this;
    }
    static capitalize(props) {
        function caps(key) { return key.substring(0, 1).toUpperCase() + key.substring(1); }
        if (typeof props === 'object') {
            if (Array.isArray(props)) {
                return props.map(AwsResource.capitalize);
            }
            if (props?.['_nocaps'] !== undefined) {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { _nocaps, ...rest } = props;
                return rest;
            }
            if (props instanceof AwsResource) {
                return ref(props.logicalName);
            }
            return Object.keys(props).reduce((prev, key) => ({ ...prev, [caps(key)]: AwsResource.capitalize(props?.[key]) }), {});
        }
        return props;
    }
    template() {
        return {
            Type: this.type,
            ...(this.deletionPolicy ? { 'DeletionPolicy': this.deletionPolicy } : {}),
            ...(this.condition ? { 'Condition': this.condition } : {}),
            ...(this.dependsOn ? {
                DependsOn: this.dependsOn
                    .map(it => it instanceof AwsResource ? it.logicalName : it)
            } : {}),
            ...(Object.keys(this.properties).length === 0 ? {} : { Properties: AwsResource.capitalize(this.properties) })
        };
    }
    disableLowerCase() {
        this.properties = { ...this.properties, _nocaps: true };
        return this;
    }
}
