"use strict";
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseService = void 0;
const workbench_core_base_1 = require("@aws/workbench-core-base");
class DatabaseService {
    constructor() {
        this._dynamoDbService = new workbench_core_base_1.DynamoDBService({
            region: process.env.AWS_REGION,
            table: process.env.STACK_NAME
        });
        this._metadataService = new workbench_core_base_1.MetadataService(this._dynamoDbService);
    }
    async storeAssociations(entity, relations) {
        const relationTypeMap = new Map();
        for (const relation of relations) {
            const relationType = relation.type;
            const relations = relationTypeMap.get(relationType) || [];
            relations.push(relation);
            relationTypeMap.set(relationType, relations);
        }
        for (const relationType of relationTypeMap.keys()) {
            const relations = relationTypeMap.get(relationType) || [];
            await this._metadataService.updateRelationship(entity.type, { id: entity.id, data: entity.data }, relationType, relations.map((item) => {
                return { id: item.id, data: item.data };
            }));
        }
    }
    async listAssociations(entity, relationType, queryParams) {
        const response = await this._metadataService.listDependentMetadata(entity.type, entity.id, relationType, workbench_core_base_1.RelationshipDDBItemParser, queryParams);
        const associables = response.data.map((item) => {
            return {
                type: relationType,
                id: item.id,
                data: {
                    pk: item.pk,
                    sk: item.sk
                }
            };
        });
        return {
            data: associables,
            paginationToken: response.paginationToken
        };
    }
    async removeAssociations(entity, relations) {
        const relationTypeMap = new Map();
        for (const relation of relations) {
            const relationType = relation.type;
            const relations = relationTypeMap.get(relationType) || [];
            relations.push(relation);
            relationTypeMap.set(relationType, relations);
        }
        for (const relationType of relationTypeMap.keys()) {
            const relations = relationTypeMap.get(relationType) || [];
            await this._metadataService.deleteRelationships(entity.type, entity.id, relationType, relations.map((item) => item.id));
        }
    }
    async getAssociation(entity, relationship) {
        const association = await this._metadataService.getMetadataItem(entity.type, entity.id, relationship.type, relationship.id, workbench_core_base_1.RelationshipDDBItemParser);
        if (association === undefined) {
            return undefined;
        }
        return {
            type: relationship.type,
            id: relationship.id,
            data: {
                pk: association.pk,
                sk: association.sk
            }
        };
    }
}
exports.DatabaseService = DatabaseService;
//# sourceMappingURL=databaseService.js.map