import fs from "fs";
import * as iam from "aws-cdk-lib/aws-iam";
import { Secret } from "./Secret.js";
import { Parameter } from "./Parameter.js";
import { FunctionConfig } from "@serverless-stack/core";
export function assertNameNotInUse(name) {
    if (Secret.hasName(name) || Parameter.hasName(name)) {
        throw new Error(`Config ${name} already exists`);
    }
}
export function codegenTypes(typesPath) {
    fs.appendFileSync(`${typesPath}/index.d.ts`, `export * from "./config";`);
    fs.writeFileSync(`${typesPath}/config.d.ts`, `
    import "@serverless-stack/node/config";
    declare module "@serverless-stack/node/config" {
      export interface ConfigType {
        ${[
        "APP",
        "STAGE",
        ...Parameter.getAllNames(),
        ...Secret.getAllNames()
    ].map((p) => `${p}: string;`).join("\n")}
      }
    }
  `);
}
export function configToEnvironmentVariables(config) {
    const env = {};
    (config || []).forEach((c) => {
        if (c instanceof Secret) {
            env[`${FunctionConfig.SECRET_ENV_PREFIX}${c.name}`] = "1";
        }
        else if (c instanceof Parameter) {
            env[`${FunctionConfig.PARAM_ENV_PREFIX}${c.name}`] = c.value;
        }
    });
    return env;
}
export function configToPolicyStatement(app, config) {
    const iamResources = [];
    (config || [])
        .filter((c) => c instanceof Secret)
        .forEach((c) => iamResources.push(`arn:aws:ssm:${app.region}:${app.account}:parameter${FunctionConfig.buildSsmNameForSecret(app.name, app.stage, c.name)}`, `arn:aws:ssm:${app.region}:${app.account}:parameter${FunctionConfig.buildSsmNameForSecretFallback(app.name, c.name)}`));
    if (iamResources.length === 0) {
        return;
    }
    return new iam.PolicyStatement({
        actions: ["ssm:GetParameters"],
        effect: iam.Effect.ALLOW,
        resources: iamResources,
    });
}
export { Secret, Parameter };
