var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { LogError, CompositeKey } from '@memberjunction/core';
import { Arg, Ctx, Field, InputType, Int, Mutation, ObjectType, PubSub, PubSubEngine, Query, Resolver } from 'type-graphql';
import { CompositeKeyInputType, CompositeKeyOutputType } from '../generic/KeyInputOutputTypes.js';
import { z } from 'zod';
import { GetReadOnlyProvider, GetReadWriteProvider } from '../util.js';
let EntityDependencyResult = class EntityDependencyResult {
    EntityName;
    RelatedEntityName;
    FieldName;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], EntityDependencyResult.prototype, "EntityName", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], EntityDependencyResult.prototype, "RelatedEntityName", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], EntityDependencyResult.prototype, "FieldName", void 0);
EntityDependencyResult = __decorate([
    ObjectType()
], EntityDependencyResult);
export { EntityDependencyResult };
let EntityDependencyResolver = class EntityDependencyResolver {
    async GetEntityDependencies(entityName, { dataSource, userPayload, providers }, pubSub) {
        try {
            const md = GetReadOnlyProvider(providers);
            return md.GetEntityDependencies(entityName);
        }
        catch (err) {
            LogError(err);
            const ctx = z.object({ message: z.string() }).catch(null).parse(err)?.message ?? JSON.stringify(err);
            throw new Error(ctx);
        }
    }
};
__decorate([
    Query(() => [EntityDependencyResult]),
    __param(0, Arg('entityName', () => String)),
    __param(1, Ctx()),
    __param(2, PubSub()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, PubSubEngine]),
    __metadata("design:returntype", Promise)
], EntityDependencyResolver.prototype, "GetEntityDependencies", null);
EntityDependencyResolver = __decorate([
    Resolver(EntityDependencyResult)
], EntityDependencyResolver);
export { EntityDependencyResolver };
let RecordDependencyResult = class RecordDependencyResult {
    EntityName;
    RelatedEntityName;
    FieldName;
    CompositeKey;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], RecordDependencyResult.prototype, "EntityName", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], RecordDependencyResult.prototype, "RelatedEntityName", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], RecordDependencyResult.prototype, "FieldName", void 0);
__decorate([
    Field(() => CompositeKeyOutputType),
    __metadata("design:type", CompositeKey)
], RecordDependencyResult.prototype, "CompositeKey", void 0);
RecordDependencyResult = __decorate([
    ObjectType()
], RecordDependencyResult);
export { RecordDependencyResult };
let RecordDependencyResolver = class RecordDependencyResolver {
    async GetRecordDependencies(entityName, ckInput, { dataSource, userPayload, providers }, pubSub) {
        try {
            const md = GetReadOnlyProvider(providers);
            const ck = new CompositeKey(ckInput.KeyValuePairs);
            const result = await md.GetRecordDependencies(entityName, ck);
            return result.map(dep => ({
                EntityName: dep.EntityName,
                RelatedEntityName: dep.RelatedEntityName,
                FieldName: dep.FieldName,
                CompositeKey: dep.PrimaryKey
            }));
        }
        catch (e) {
            LogError(e);
            throw e;
        }
    }
};
__decorate([
    Query(() => [RecordDependencyResult]),
    __param(0, Arg('entityName', () => String)),
    __param(1, Arg('CompositeKey', () => CompositeKeyInputType)),
    __param(2, Ctx()),
    __param(3, PubSub()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, CompositeKeyInputType, Object, PubSubEngine]),
    __metadata("design:returntype", Promise)
], RecordDependencyResolver.prototype, "GetRecordDependencies", null);
RecordDependencyResolver = __decorate([
    Resolver(RecordDependencyResult)
], RecordDependencyResolver);
export { RecordDependencyResolver };
let FieldMapping = class FieldMapping {
    FieldName;
    Value;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], FieldMapping.prototype, "FieldName", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], FieldMapping.prototype, "Value", void 0);
FieldMapping = __decorate([
    InputType()
], FieldMapping);
let FieldMappingOutput = class FieldMappingOutput {
    FieldName;
    Value;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], FieldMappingOutput.prototype, "FieldName", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], FieldMappingOutput.prototype, "Value", void 0);
FieldMappingOutput = __decorate([
    ObjectType()
], FieldMappingOutput);
let RecordMergeRequest = class RecordMergeRequest {
    EntityName;
    SurvivingRecordCompositeKey;
    RecordsToMerge;
    FieldMap;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], RecordMergeRequest.prototype, "EntityName", void 0);
__decorate([
    Field(() => CompositeKeyInputType),
    __metadata("design:type", CompositeKey)
], RecordMergeRequest.prototype, "SurvivingRecordCompositeKey", void 0);
__decorate([
    Field(() => [CompositeKeyInputType]),
    __metadata("design:type", Array)
], RecordMergeRequest.prototype, "RecordsToMerge", void 0);
__decorate([
    Field(() => [FieldMapping], { nullable: true }),
    __metadata("design:type", Array)
], RecordMergeRequest.prototype, "FieldMap", void 0);
RecordMergeRequest = __decorate([
    InputType()
], RecordMergeRequest);
export { RecordMergeRequest };
let RecordMergeRequestOutput = class RecordMergeRequestOutput {
    EntityName;
    SurvivingRecordID;
    RecordsToMerge;
    FieldMap;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], RecordMergeRequestOutput.prototype, "EntityName", void 0);
__decorate([
    Field(() => Int),
    __metadata("design:type", Number)
], RecordMergeRequestOutput.prototype, "SurvivingRecordID", void 0);
__decorate([
    Field(() => [Int]),
    __metadata("design:type", Array)
], RecordMergeRequestOutput.prototype, "RecordsToMerge", void 0);
__decorate([
    Field(() => [FieldMappingOutput], { nullable: true }),
    __metadata("design:type", Array)
], RecordMergeRequestOutput.prototype, "FieldMap", void 0);
RecordMergeRequestOutput = __decorate([
    ObjectType()
], RecordMergeRequestOutput);
export { RecordMergeRequestOutput };
let RecordMergeDetailResult = class RecordMergeDetailResult {
    CompositeKey;
    Success;
    RecordMergeDeletionLogID;
    Message;
};
__decorate([
    Field(() => CompositeKeyOutputType),
    __metadata("design:type", CompositeKeyOutputType)
], RecordMergeDetailResult.prototype, "CompositeKey", void 0);
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], RecordMergeDetailResult.prototype, "Success", void 0);
__decorate([
    Field(() => Int, { nullable: true }),
    __metadata("design:type", Number)
], RecordMergeDetailResult.prototype, "RecordMergeDeletionLogID", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], RecordMergeDetailResult.prototype, "Message", void 0);
RecordMergeDetailResult = __decorate([
    ObjectType()
], RecordMergeDetailResult);
export { RecordMergeDetailResult };
let RecordMergeResult = class RecordMergeResult {
    Success;
    OverallStatus;
    RecordMergeLogID;
    RecordStatus;
    Request;
};
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], RecordMergeResult.prototype, "Success", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], RecordMergeResult.prototype, "OverallStatus", void 0);
__decorate([
    Field(() => Int, { nullable: true }),
    __metadata("design:type", Number)
], RecordMergeResult.prototype, "RecordMergeLogID", void 0);
__decorate([
    Field(() => [RecordMergeDetailResult]),
    __metadata("design:type", Array)
], RecordMergeResult.prototype, "RecordStatus", void 0);
__decorate([
    Field(() => RecordMergeRequestOutput),
    __metadata("design:type", RecordMergeRequestOutput)
], RecordMergeResult.prototype, "Request", void 0);
RecordMergeResult = __decorate([
    ObjectType()
], RecordMergeResult);
export { RecordMergeResult };
let RecordMergeResolver = class RecordMergeResolver {
    async MergeRecords(request, { dataSource, userPayload, providers }, pubSub) {
        try {
            const md = GetReadWriteProvider(providers);
            const options = {};
            const result = await md.MergeRecords(request, userPayload.userRecord, options);
            return result;
        }
        catch (e) {
            LogError(e);
            throw e;
        }
    }
};
__decorate([
    Mutation(() => RecordMergeResult),
    __param(0, Arg('request', () => RecordMergeRequest)),
    __param(1, Ctx()),
    __param(2, PubSub()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [RecordMergeRequest, Object, PubSubEngine]),
    __metadata("design:returntype", Promise)
], RecordMergeResolver.prototype, "MergeRecords", null);
RecordMergeResolver = __decorate([
    Resolver(RecordMergeResult)
], RecordMergeResolver);
export { RecordMergeResolver };
export default EntityDependencyResolver;
//# sourceMappingURL=MergeRecordsResolver.js.map