"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.MockDatabaseService = void 0;
class MockDatabaseService {
    constructor() {
        this._associations = new Map();
    }
    storeAssociations(entity, relations) {
        this._updateAssociations(entity, relations);
        for (const relation of relations) {
            this._updateAssociations(relation, [entity]);
        }
        return Promise.resolve();
    }
    removeAssociations(entity, relations) {
        this._deleteAssociations(entity, relations);
        for (const relation of relations) {
            this._deleteAssociations(relation, [entity]);
        }
        return Promise.resolve();
    }
    getAssociations(type, id) {
        const entityKey = `${type}#${id}`;
        const associables = this._associations.get(entityKey) || [];
        return Promise.resolve(associables);
    }
    async getAssociation(entity, relationship) {
        const associations = await this.getAssociations(entity.type, entity.id);
        for (const association of associations) {
            const isMatch = association.type === relationship.type && association.id === relationship.id;
            if (isMatch) {
                return Promise.resolve(association);
            }
        }
        return Promise.resolve(undefined);
    }
    async listAssociations(entity, relationType, queryParams) {
        const associations = await this.getAssociations(entity.type, entity.id);
        const response = {
            data: associations
        };
        return Promise.resolve(response);
    }
    _keyForAssociable(associable) {
        return `${associable.type}#${associable.id}`;
    }
    _updateAssociations(entity, relations) {
        const key = this._keyForAssociable(entity);
        const associations = this._associations.get(key) || [];
        this._associations.set(key, associations.concat(relations));
    }
    _deleteAssociations(entity, relations) {
        const key = this._keyForAssociable(entity);
        let associations = this._associations.get(key) || [];
        associations = associations.filter((item) => {
            return item.type === entity.type && item.id === entity.id;
        });
        this._associations.set(key, associations);
    }
}
exports.MockDatabaseService = MockDatabaseService;
//# sourceMappingURL=mockDatabaseService.js.map