const require_chunk = require('./chunk-CUT6urMc.cjs');
const __aws_sdk_client_ssm = require_chunk.__toESM(require("@aws-sdk/client-ssm"));

//#region src/deploy/SSMStateProvider.ts
/**
* AWS SSM Parameter Store state provider.
*
* Stores state as encrypted SecureString parameters.
* Parameter path: /gkm/{workspaceName}/{stage}/state
*/
var SSMStateProvider = class SSMStateProvider {
	constructor(workspaceName, client) {
		this.workspaceName = workspaceName;
		this.client = client;
	}
	/**
	* Create an SSMStateProvider with a new SSMClient.
	*/
	static create(options) {
		const clientConfig = {
			region: options.region,
			endpoint: options.endpoint
		};
		if (options.profile) {
			const { fromIni } = require("@aws-sdk/credential-providers");
			clientConfig.credentials = fromIni({ profile: options.profile });
		} else if (options.credentials) clientConfig.credentials = options.credentials;
		const client = new __aws_sdk_client_ssm.SSMClient(clientConfig);
		return new SSMStateProvider(options.workspaceName, client);
	}
	/**
	* Get the SSM parameter name for a stage.
	*/
	getParameterName(stage) {
		return `/gkm/${this.workspaceName}/${stage}/state`;
	}
	async read(stage) {
		const parameterName = this.getParameterName(stage);
		try {
			const response = await this.client.send(new __aws_sdk_client_ssm.GetParameterCommand({
				Name: parameterName,
				WithDecryption: true
			}));
			if (!response.Parameter?.Value) return null;
			return JSON.parse(response.Parameter.Value);
		} catch (error) {
			if (error instanceof __aws_sdk_client_ssm.ParameterNotFound) return null;
			throw error;
		}
	}
	async write(stage, state) {
		const parameterName = this.getParameterName(stage);
		state.lastDeployedAt = (/* @__PURE__ */ new Date()).toISOString();
		await this.client.send(new __aws_sdk_client_ssm.PutParameterCommand({
			Name: parameterName,
			Value: JSON.stringify(state),
			Type: "SecureString",
			Overwrite: true,
			Description: `GKM deployment state for ${this.workspaceName}/${stage}`
		}));
	}
};

//#endregion
exports.SSMStateProvider = SSMStateProvider;
//# sourceMappingURL=SSMStateProvider-CT8tjl9o.cjs.map