import { t as debug_default } from "./debug-Dtkb5y8O.js";
import string from "@adonisjs/core/helpers/string";
import { MessageBuilder } from "@adonisjs/core/helpers";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
import { DeleteItemCommand, GetItemCommand, PutItemCommand, UpdateItemCommand } from "@aws-sdk/client-dynamodb";
//#region src/stores/dynamodb.ts
/**
* @adonisjs/session
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* DynamoDB store to read/write session data to AWS DynamoDB.
* Provides highly scalable, managed session storage with automatic expiry.
*
* @example
* const dynamoStore = new DynamoDBStore(dynamoClient, '2 hours', {
*   tableName: 'Sessions',
*   keyAttribute: 'sessionId'
* })
*/
var DynamoDBStore = class {
	/**
	* DynamoDB client instance
	*/
	#client;
	/**
	* DynamoDB table name
	*/
	#tableName;
	/**
	* Attribute name for the session key
	*/
	#keyAttribute;
	/**
	* Time-to-live in seconds for session expiry
	*/
	#ttlSeconds;
	/**
	* Attribute name for the session value
	*/
	#valueAttribute = "value";
	/**
	* Attribute name for the expiry timestamp
	*/
	#expiresAtAttribute = "expires_at";
	/**
	* Creates a new DynamoDB store instance
	*
	* @param client - DynamoDB client instance
	* @param age - Session age in seconds or time expression (e.g. '2 hours')
	* @param options - Configuration options
	* @param options.tableName - DynamoDB table name (defaults to "Session")
	* @param options.keyAttribute - Key attribute name (defaults to "key")
	*/
	constructor(client, age, options) {
		this.#client = client;
		this.#tableName = options?.tableName ?? "Session";
		this.#keyAttribute = options?.keyAttribute ?? "key";
		this.#ttlSeconds = string.seconds.parse(age);
		debug_default("initiating dynamodb store");
	}
	/**
	* Reads session data from DynamoDB
	*
	* @param sessionId - Session identifier
	*
	* @example
	* const data = await store.read('sess_abc123')
	*/
	async read(sessionId) {
		debug_default("dynamodb store: reading session data %s", sessionId);
		const command = new GetItemCommand({
			TableName: this.#tableName,
			Key: marshall({ [this.#keyAttribute]: sessionId })
		});
		const response = await this.#client.send(command);
		if (!response.Item) return null;
		if (!response.Item[this.#valueAttribute]) return null;
		const item = unmarshall(response.Item);
		const contents = item[this.#valueAttribute];
		const expiresAt = item[this.#expiresAtAttribute];
		/**
		* Check if the item has been expired and return null (if expired)
		*/
		if (Date.now() > expiresAt) return null;
		/**
		* Verify contents with the session id and return them as an object. The verify
		* method can fail when the contents is not JSON.
		*/
		try {
			return new MessageBuilder().verify(contents, sessionId);
		} catch {
			return null;
		}
	}
	/**
	* Writes session values to DynamoDB with expiry
	*
	* @param sessionId - Session identifier
	* @param values - Session data to store
	*
	* @example
	* await store.write('sess_abc123', { userId: 123 })
	*/
	async write(sessionId, values) {
		debug_default("dynamodb store: writing session data %s, %O", sessionId, values);
		const message = new MessageBuilder().build(values, void 0, sessionId);
		const command = new PutItemCommand({
			TableName: this.#tableName,
			Item: marshall({
				[this.#keyAttribute]: sessionId,
				[this.#valueAttribute]: message,
				[this.#expiresAtAttribute]: Date.now() + this.#ttlSeconds * 1e3
			})
		});
		await this.#client.send(command);
	}
	/**
	* Removes session data from DynamoDB
	*
	* @param sessionId - Session identifier to remove
	*
	* @example
	* await store.destroy('sess_abc123')
	*/
	async destroy(sessionId) {
		debug_default("dynamodb store: destroying session data %s", sessionId);
		const command = new DeleteItemCommand({
			TableName: this.#tableName,
			Key: marshall({ [this.#keyAttribute]: sessionId })
		});
		await this.#client.send(command);
	}
	/**
	* Updates the session expiry time in DynamoDB
	*
	* @param sessionId - Session identifier
	*
	* @example
	* await store.touch('sess_abc123')
	*/
	async touch(sessionId) {
		debug_default("dynamodb store: touching session data %s", sessionId);
		const command = new UpdateItemCommand({
			TableName: this.#tableName,
			Key: marshall({ [this.#keyAttribute]: sessionId }),
			UpdateExpression: "SET #expires_at = :expires_at",
			ExpressionAttributeNames: { "#expires_at": this.#expiresAtAttribute },
			ExpressionAttributeValues: marshall({ ":expires_at": Date.now() + this.#ttlSeconds * 1e3 })
		});
		await this.#client.send(command);
	}
};
//#endregion
export { DynamoDBStore };
