"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelationshipPersistence = void 0;
const __1 = require("..");
const internal_1 = require("../../internal");
const Logger_1 = require("../../Utilities/Logging/Logger");
class RelationshipPersistence {
    static fromData(ctx, obj, entitySchemaByName) {
        return __awaiter(this, void 0, void 0, function* () {
            if (obj.$type !== 'SingleKeyRelationship') {
                // We don't have any other type of a relationship yet!
                return;
            }
            if (!entitySchemaByName.has(obj.fromAttribute.entityName)) {
                Logger_1.Logger.warning(ctx, this.TAG, this.fromData.name, undefined, internal_1.cdmLogCode.WarnPersistRelUndefinedSourceEntity, obj.fromAttribute.entityName);
                return;
            }
            if (!entitySchemaByName.has(obj.toAttribute.entityName)) {
                Logger_1.Logger.warning(ctx, this.TAG, this.fromData.name, undefined, internal_1.cdmLogCode.WarnPersistRelUndefinedTargetEntity);
                return;
            }
            const relationship = ctx.corpus.MakeObject(internal_1.cdmObjectType.e2eRelationshipDef, obj.name);
            yield __1.ModelJson.utils.processAnnotationsFromData(ctx, obj, relationship.exhibitsTraits);
            relationship.name = obj.name;
            relationship.explanation = obj.description;
            relationship.fromEntity = entitySchemaByName.get(obj.fromAttribute.entityName);
            relationship.toEntity = entitySchemaByName.get(obj.toAttribute.entityName);
            relationship.fromEntityAttribute = obj.fromAttribute.attributeName;
            relationship.toEntityAttribute = obj.toAttribute.attributeName;
            return relationship;
        });
    }
    static toData(instance, resOpt, options, ctx) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = {};
            result.$type = 'SingleKeyRelationship';
            result.fromAttribute = {
                entityName: RelationshipPersistence.getEntityName(instance.fromEntity),
                attributeName: instance.fromEntityAttribute
            };
            result.toAttribute = {
                entityName: RelationshipPersistence.getEntityName(instance.toEntity),
                attributeName: instance.toEntityAttribute
            };
            result.description = instance.explanation;
            result.name = instance.name;
            yield __1.ModelJson.utils.processTraitsAndAnnotationsToData(instance.ctx, result, instance.exhibitsTraits);
            return result;
        });
    }
    static getEntityName(corpusPath) {
        const lastSlashIndex = corpusPath.lastIndexOf('/');
        if (lastSlashIndex !== -1) {
            return corpusPath.slice(lastSlashIndex + 1);
        }
        return corpusPath;
    }
}
exports.RelationshipPersistence = RelationshipPersistence;
RelationshipPersistence.TAG = RelationshipPersistence.name;
//# sourceMappingURL=RelationshipPersistence.js.map