import path from "path";
import fs from "fs";
import { Stack } from "./Stack.js";
import { isSSTConstruct, isStackConstruct, } from "./Construct.js";
import { bindParameters, bindType } from "./util/functionBinding.js";
import { stack } from "./FunctionalStack.js";
import { createRequire } from "module";
import { Auth } from "./Auth.js";
import { useDeferredTasks } from "./deferred_task.js";
import { AppContext } from "./context.js";
import { useProject } from "../project.js";
import { Logger } from "../logger.js";
import { App as CDKApp, Tags, CfnResource, RemovalPolicy, CustomResourceProvider, CustomResourceProviderRuntime, CustomResource, Aspects, } from "aws-cdk-lib/core";
import { CfnFunction } from "aws-cdk-lib/aws-lambda";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { ArnPrincipal, PolicyStatement } from "aws-cdk-lib/aws-iam";
import { CfnLogGroup } from "aws-cdk-lib/aws-logs";
const require = createRequire(import.meta.url);
function exitWithMessage(message) {
    console.error(message);
    process.exit(1);
}
/**
 * The App construct extends cdk.App and is used internally by SST.
 */
export class App extends CDKApp {
    /**
     * Whether or not the app is running locally under `sst dev`
     */
    local = false;
    /**
     * Whether the app is running locally under start, deploy or remove
     */
    mode;
    /**
     * The name of your app, comes from the `name` in your `sst.config.ts`
     */
    name;
    /**
     * The stage the app is being deployed to. If this is not specified as the --stage option, it'll default to the stage configured during the initial run of the SST CLI.
     */
    stage;
    /**
     * The region the app is being deployed to. If this is not specified as the --region option in the SST CLI, it'll default to the region in your sst.config.ts.
     */
    region;
    /**
     * The AWS account the app is being deployed to. This comes from the IAM credentials being used to run the SST CLI.
     */
    account;
    /** @internal */
    debugScriptVersion;
    /** @internal */
    debugIncreaseTimeout;
    /** @internal */
    appPath;
    /** @internal */
    isActiveStack;
    /** @internal */
    defaultFunctionProps;
    _defaultRemovalPolicy;
    /** @internal */
    get defaultRemovalPolicy() {
        return this._defaultRemovalPolicy;
    }
    /**
     * @internal
     */
    constructor(deployProps, props = {}) {
        super(props);
        AppContext.provide(this);
        this.appPath = process.cwd();
        this.mode = deployProps.mode;
        this.local = this.mode === "dev";
        this.stage = deployProps.stage || "dev";
        this.name = deployProps.name || "my-app";
        this.region =
            deployProps.region || process.env.CDK_DEFAULT_REGION || "us-east-1";
        this.account =
            deployProps.account || process.env.CDK_DEFAULT_ACCOUNT || "my-account";
        this.isActiveStack = deployProps.isActiveStack;
        this.defaultFunctionProps = [];
        if (this.mode === "dev") {
            this.debugScriptVersion = deployProps.debugScriptVersion;
            this.debugIncreaseTimeout = deployProps.debugIncreaseTimeout;
        }
    }
    /**
     * Use this method to prefix resource names in your stacks to make sure they don't thrash when deployed to different stages in the same AWS account. This method will prefix a given resource name with the stage and app name. Using the format `${stage}-${name}-${logicalName}`.
     * @example
     * ```js
     * console.log(app.logicalPrefixedName("myTopic"));
     *
     * // dev-my-app-myTopic
     * ```
     */
    logicalPrefixedName(logicalName) {
        const namePrefix = this.name === "" ? "" : `${this.name}-`;
        return `${this.stage}-${namePrefix}${logicalName}`;
    }
    /**
     * The default removal policy that'll be applied to all the resources in the app. This can be useful to set ephemeral (dev or feature branch) environments to remove all the resources on deletion.
     * :::danger
     * Make sure to not set the default removal policy to `DESTROY` for production environments.
     * :::
     * @example
     * ```js
     * app.setDefaultRemovalPolicy(app.mode === "dev" ? "destroy" : "retain")
     * ```
     */
    setDefaultRemovalPolicy(policy) {
        this._defaultRemovalPolicy = policy;
    }
    /**
     * The default function props to be applied to all the Lambda functions in the app. These default values will be overridden if a Function sets its own props.
     * This needs to be called before a stack with any functions have been added to the app.
     *
     * @example
     * ```js
     * app.setDefaultFunctionProps({
     *   runtime: "nodejs12.x",
     *   timeout: 30
     * })
     * ```
     */
    setDefaultFunctionProps(props) {
        this.defaultFunctionProps.push(props);
    }
    /**
     * Adds additional default Permissions to be applied to all Lambda functions in the app.
     *
     * @example
     * ```js
     * app.addDefaultFunctionPermissions(["s3"])
     * ```
     */
    addDefaultFunctionPermissions(permissions) {
        this.defaultFunctionProps.push({
            permissions,
        });
    }
    /**
     * Adds additional default environment variables to be applied to all Lambda functions in the app.
     *
     * @example
     * ```js
     * app.addDefaultFunctionEnv({
     *   "MY_ENV_VAR": "my-value"
     * })
     * ```
     */
    addDefaultFunctionEnv(environment) {
        this.defaultFunctionProps.push({
            environment,
        });
    }
    /**
     * Binds additional default resources to be applied to all Lambda functions in the app.
     *
     * @example
     * ```js
     * app.addDefaultFunctionBinding([STRIPE_KEY, bucket]);
     * ```
     */
    addDefaultFunctionBinding(bind) {
        this.defaultFunctionProps.push({ bind });
    }
    /**
     * Adds additional default layers to be applied to all Lambda functions in the stack.
     */
    addDefaultFunctionLayers(layers) {
        this.defaultFunctionProps.push({
            layers,
        });
    }
    isFinished = false;
    async finish() {
        if (this.isFinished)
            return;
        this.isFinished = true;
        Auth.injectConfig();
        this.buildConstructsMetadata();
        this.ensureUniqueConstructIds();
        this.codegenTypes();
        // Run deferred tasks
        // - after codegen b/c some frontend frameworks (ie. Next.js apps) runs
        //   type checking in the build step
        // - before remove govcloud unsupported resource properties b/c deferred
        //   tasks may add govcloud unsupported resource properties
        await useDeferredTasks().run();
        this.createBindingSsmParameters();
        this.removeGovCloudUnsupportedResourceProperties();
        const { config } = useProject();
        for (const child of this.node.children) {
            if (isStackConstruct(child)) {
                // Tag stacks
                Tags.of(child).add("sst:app", this.name);
                Tags.of(child).add("sst:stage", this.stage);
                // Set removal policy
                if (this._defaultRemovalPolicy)
                    this.applyRemovalPolicy(child, this._defaultRemovalPolicy);
                // Stack names need to be parameterized with the stage name
                if (config.advanced?.disableParameterizedStackNameCheck !== true &&
                    !child.stackName.startsWith(`${this.stage}-`) &&
                    !child.stackName.endsWith(`-${this.stage}`) &&
                    child.stackName.indexOf(`-${this.stage}-`) === -1) {
                    throw new Error(`Stack "${child.stackName}" is not parameterized with the stage name. The stack name needs to either start with "$stage-", end in "-$stage", or contain the stage name "-$stage-".`);
                }
            }
        }
    }
    isRunningSSTTest() {
        // Check the env var set inside test/setup-tests.js
        return process.env.SST_RESOURCES_TESTS === "enabled";
    }
    getInputFilesFromEsbuildMetafile(file) {
        let metaJson;
        try {
            metaJson = JSON.parse(fs.readFileSync(file).toString());
        }
        catch (e) {
            exitWithMessage("There was a problem reading the esbuild metafile.");
        }
        return Object.keys(metaJson.inputs).map((input) => path.resolve(input));
    }
    createBindingSsmParameters() {
        class CreateSsmParameters {
            visit(c) {
                if (!isSSTConstruct(c)) {
                    return;
                }
                if ("_doNotAllowOthersToBind" in c && c._doNotAllowOthersToBind) {
                    return;
                }
                bindParameters(c);
            }
        }
        Aspects.of(this).add(new CreateSsmParameters());
    }
    buildConstructsMetadata() {
        const constructs = this.buildConstructsMetadata_collectConstructs(this);
        const byStack = {};
        const local = [];
        for (const c of constructs) {
            const stack = Stack.of(c);
            const list = byStack[stack.node.id] || [];
            const metadata = c.getConstructMetadata();
            const item = {
                id: c.node.id,
                addr: c.node.addr,
                stack: Stack.of(c).stackName,
                ...metadata,
            };
            local.push(item);
            list.push({
                ...item,
                local: undefined,
            });
            byStack[stack.node.id] = list;
        }
        // Register constructs
        for (const child of this.node.children) {
            if (child instanceof Stack) {
                const stackName = child.node.id;
                child.addOutputs({
                    SSTMetadata: JSON.stringify({
                        app: this.name,
                        stage: this.stage,
                        version: useProject().version,
                        metadata: byStack[stackName] || [],
                    }),
                });
            }
        }
    }
    buildConstructsMetadata_collectConstructs(construct) {
        return [
            isSSTConstruct(construct) ? construct : undefined,
            ...construct.node.children.flatMap((c) => this.buildConstructsMetadata_collectConstructs(c)),
        ].filter((c) => Boolean(c));
    }
    applyRemovalPolicy(current, policy) {
        if (current instanceof CfnResource) {
            current.applyRemovalPolicy(RemovalPolicy[policy.toUpperCase()]);
        }
        // Had to copy this in to enable deleting objects in bucket
        // https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-s3/lib/bucket.ts#L1910
        if (current instanceof Bucket &&
            !current.node.tryFindChild("AutoDeleteObjectsCustomResource")) {
            const AUTO_DELETE_OBJECTS_RESOURCE_TYPE = "Custom::S3AutoDeleteObjects";
            const provider = CustomResourceProvider.getOrCreateProvider(current, AUTO_DELETE_OBJECTS_RESOURCE_TYPE, {
                codeDirectory: path.join(require.resolve("aws-cdk-lib/aws-s3"), "../lib/auto-delete-objects-handler"),
                runtime: CustomResourceProviderRuntime.NODEJS_16_X,
                description: `Lambda function for auto-deleting objects in ${current.bucketName} S3 bucket.`,
            });
            // Use a bucket policy to allow the custom resource to delete
            // objects in the bucket
            current.addToResourcePolicy(new PolicyStatement({
                actions: [
                    // list objects
                    "s3:GetBucket*",
                    "s3:List*",
                    // and then delete them
                    "s3:DeleteObject*",
                ],
                resources: [current.bucketArn, current.arnForObjects("*")],
                principals: [new ArnPrincipal(provider.roleArn)],
            }));
            const customResource = new CustomResource(current, "AutoDeleteObjectsCustomResource", {
                resourceType: AUTO_DELETE_OBJECTS_RESOURCE_TYPE,
                serviceToken: provider.serviceToken,
                properties: {
                    BucketName: current.bucketName,
                },
            });
            // Ensure bucket policy is deleted AFTER the custom resource otherwise
            // we don't have permissions to list and delete in the bucket.
            // (add a `if` to make TS happy)
            if (current.policy) {
                customResource.node.addDependency(current.policy);
            }
        }
        current.node.children.forEach((resource) => this.applyRemovalPolicy(resource, policy));
    }
    removeGovCloudUnsupportedResourceProperties() {
        if (!this.region.startsWith("us-gov-")) {
            return;
        }
        class RemoveGovCloudUnsupportedResourceProperties {
            visit(node) {
                if (node instanceof CfnFunction) {
                    node.addPropertyDeletionOverride("EphemeralStorage");
                }
                else if (node instanceof CfnLogGroup) {
                    node.addPropertyDeletionOverride("Tags");
                }
            }
        }
        Aspects.of(this).add(new RemoveGovCloudUnsupportedResourceProperties());
    }
    ensureUniqueConstructIds() {
        // "ids" has the shape of:
        // {
        //   Table: {
        //     "id_with_hyphen": "id-with-hyphen",
        //     "id_with_underscore": "id_with_underscore",
        //   }
        // }
        const ids = {};
        class EnsureUniqueConstructIds {
            visit(c) {
                if (!isSSTConstruct(c)) {
                    return;
                }
                if ("_doNotAllowOthersToBind" in c && c._doNotAllowOthersToBind) {
                    return;
                }
                const className = c.constructor.name;
                const id = c.id;
                const normId = id.replace(/-/g, "_");
                const existingIds = ids[className] || {};
                if (!id.match(/^[a-zA-Z]([a-zA-Z0-9-_])*$/)) {
                    throw new Error([
                        `Invalid id "${id}" for ${className} construct.`,
                        ``,
                        `Starting v1.16, construct ids can only contain alphabetic characters, hyphens ("-"), and underscores ("_"), and must start with an alphabetic character. If you are migrating from version 1.15 or earlier, please see the upgrade guide — https://docs.serverless-stack.com/upgrade-guide#upgrade-to-v116`,
                    ].join("\n"));
                }
                else if (["Parameter", "Secret"].includes(className)) {
                    const existingConfigId = ids.Secret?.[normId] || ids.Parameter?.[normId];
                    if (existingConfigId === id) {
                        throw new Error(`ERROR: Config with id "${id}" already exists.`);
                    }
                    else if (existingConfigId) {
                        throw new Error(`ERROR: You cannot have the same Config id with an underscore and hyphen: "${existingConfigId}" and "${id}".`);
                    }
                }
                else if (existingIds[normId]) {
                    throw new Error([
                        existingIds[normId] === id
                            ? `${className} with id "${id}" already exists.`
                            : `You cannot have the same ${className} id with an underscore and hyphen: "${existingIds[normId]}" and "${id}".`,
                        ``,
                        `Starting v1.16, constructs must have unique ids for a given construct type. If you are migrating from version 1.15 or earlier, set the "cdk.id" in the construct with the existing id, and pick a unique id for the construct. Please see the upgrade guide — https://docs.serverless-stack.com/upgrade-guide#upgrade-to-v116`,
                        ``,
                        `    For example, if you have two Bucket constructs with the same id:`,
                        `      new Bucket(this, "bucket");`,
                        `      new Bucket(this, "bucket");`,
                        ``,
                        `    Change it to:`,
                        `      new Bucket(this, "usersBucket", {`,
                        `        cdk: {`,
                        `          id: "bucket"`,
                        `        }`,
                        `      });`,
                        `      new Bucket(this, "adminBucket", {`,
                        `        cdk: {`,
                        `          id: "bucket"`,
                        `        }`,
                        `      });`,
                    ].join("\n"));
                }
                existingIds[normId] = id;
                ids[className] = existingIds;
            }
        }
        Aspects.of(this).add(new EnsureUniqueConstructIds());
    }
    codegenTypes() {
        const project = useProject();
        const typesPath = path.resolve(project.paths.out, "types");
        Logger.debug(`Generating types in ${typesPath}`);
        fs.rmSync(typesPath, {
            recursive: true,
            force: true,
        });
        fs.mkdirSync(typesPath, {
            recursive: true,
        });
        fs.appendFileSync(`${typesPath}/index.ts`, [
            `import "sst/node/config";`,
            `declare module "sst/node/config" {`,
            `  export interface ConfigTypes {`,
            `    APP: string;`,
            `    STAGE: string;`,
            `  }`,
            `}`,
        ].join("\n"));
        this.foreachConstruct((c) => {
            if (!isSSTConstruct(c)) {
                return;
            }
            if ("_doNotAllowOthersToBind" in c && c._doNotAllowOthersToBind) {
                return;
            }
            const binding = bindType(c);
            if (!binding) {
                return;
            }
            const className = c.constructor.name;
            const id = c.id;
            // Case 1: variable does not have properties, ie. Secrets and Parameters
            fs.appendFileSync(`${typesPath}/index.ts`, (binding.variables[0] === "."
                ? [
                    `import "sst/node/${binding.clientPackage}";`,
                    `declare module "sst/node/${binding.clientPackage}" {`,
                    `  export interface ${className}Resources {`,
                    `    "${id}": string;`,
                    `  }`,
                    `}`,
                ]
                : [
                    `import "sst/node/${binding.clientPackage}";`,
                    `declare module "sst/node/${binding.clientPackage}" {`,
                    `  export interface ${className}Resources {`,
                    `    "${id}": {`,
                    ...binding.variables.map((p) => `      ${p}: string;`),
                    `    }`,
                    `  }`,
                    `}`,
                ]).join("\n"));
        });
    }
    foreachConstruct(fn) {
        const loop = (parent) => {
            for (const child of parent.node.children) {
                fn(child);
                loop(child);
            }
        };
        for (const child of this.node.children) {
            if (child instanceof Stack) {
                loop(child);
            }
        }
    }
    // Functional Stack
    // This is a magical global to avoid having to pass app everywhere.
    // We only every have one instance of app
    stack(fn, props) {
        return stack(this, fn, props);
    }
}
